Reputation: 7766
-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
Upvotes: 8
Views: 12605
Reputation: 32502
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:
Upvotes: 1
Reputation: 69
First of all,
5 in binary is 0000 0000 0000 0101
but what about -5 ? Here it is :
Now we get: -5= 1111 1111 1111 1011 ( it's in 2's complement form)
So here is how to calculate -5>>1 :
Upvotes: 1
Reputation: 520
In c the right shift operator preserves the sign bit.hence the right shifting the bits with preserving the sign bit again yields a negative number,which is in two complements form.
Upvotes: 0
Reputation: 779
I guess the answer to -5>>1 = -3. In case of a positive number, say 5, division by 2 gives 2.5 rounding off to the nearest smallest integer i.e. 2
But when we consider a negative number, -5, division by 2 gives -2.5. Its rounding off to the nearest integer gives -3.
Upvotes: 0
Reputation: 439
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
Upvotes: 6
Reputation: 735
I think answer is correct. As '/' (division) operator generate quotient (result of division).
-5/2 = -3(quotient) and 1(remainder ).
So this is ok with both positive and negative number.
5/2 = 2(quotient) and 1(remainder ).
So it is fine with positive Number.
Remainder never be a negative number. It is always positive Number.
Upvotes: 0
Reputation: 64213
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.
Upvotes: 5