Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise Operator on positive and negative numbers

    -5 / 2 = -2

    -5 >> 1 = -3

I learn from my teacher that >>1 divides the number by 2. It works on positive number but it does not work on negative numbers. Can someone explain to me??

Thanks

like image 701
Computernerd Avatar asked Dec 13 '12 11:12

Computernerd


People also ask

Can bitwise and of two positive numbers be negative?

In general - Yes.

How do you do bitwise and negative?

Thus b & (-b) works. Here the & operator is applied to the binary representation of 12 and -12. The binary representation of 12 is 1100 and that of -12 is 0100 (it is obtained by negating 1100 and adding 1 to it ie 0011+1 =0100, also known as 2's complement).…


4 Answers

As BЈовић & mystical states, using bit shift operators on negative numbers is implementation defined.
The reason for this is C doesn't distinguish between logical and arithmetic bit shifting.
(Arithmetic pads with the most significant bit, logical pads with 0's)
for positive numbers this doesn't matter, for both arithmetic and logical bit shifts would keep the most significant bit as a 0:
Arithmetic 5>>1
0000 0000 0000 0101 = 5
to
0000 0000 0000 0010 = 2

Logical 5>>1
0000 0000 0000 0101 = 5
to
0000 0000 0000 0010 = 2

however with a negative number (2's comp)
Arithmetic -5>>1
1111 1111 1111 1011 = -5
to
1111 1111 1111 1101 = -3

Logical -5>>1
1111 1111 1111 1011 = -5
to
0111 1111 1111 1101 = 32,765

or at least, this is how i understand it

like image 194
Daboyzuk Avatar answered Oct 14 '22 03:10

Daboyzuk


It works on positive number but it does not work on negative numbers.

Using shift operator on negative integer numbers is implementation defined.


[expr.shift]/3 tells this :

The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.

like image 37
BЈовић Avatar answered Oct 14 '22 02:10

BЈовић


First of all,
5 in binary is 0000 0000 0000 0101 but what about -5 ? Here it is :

  1. Change 1 to 0 and 0 to 1 ,then we get 1111 1111 1111 1010
  2. Then take that number + 1 , we get 1111 1111 1111 1011

Now we get: -5= 1111 1111 1111 1011 ( it's in 2's complement form)
So here is how to calculate -5>>1 :

  1. Move each bit of -5 from left to right ( >> ) we get 111 1111 1111 1101 (only 15 bits left)
  2. Because -5 is negative number, so we have to fill '1' to the first bit to make it become 16 bits Then we get 1111 1111 1111 1101 ( it's still in 2's complement form)
  3. Now convert it to a normal binary form by change 0 to 1 , 1 to 0 (except the first bit because it defines negative number in 2's complement), then plus '1' . so we get 1000 0000 0000 0011 = -3
like image 37
SKRUY Avatar answered Oct 14 '22 02:10

SKRUY


I learn from my teacher that >>1 divides the number by 2.

It doesn't divide the integer by two, but it performs (depending on the value) a logical or an arithmetic shift by one bit to the right. It happens to be equal to a division by two under some circumstances.

It works on positive number but it does not work on negative numbers.

It works in both cases, but the exact behavior is not mandated by the standard, but rather implementation-defined. It usually divides by two and truncates the result towards negative infinity, in constrast to towards zero as a normal division would do.

For reference:

  • http://en.cppreference.com/w/cpp/language/operator_arithmetic
like image 37
moooeeeep Avatar answered Oct 14 '22 03:10

moooeeeep