-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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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= 5to
0000 0000 0000 0010= 2Logical 5>>1
0000 0000 0000 0101= 5to
0000 0000 0000 0010= 2however with a negative number (2’s comp)
Arithmetic -5>>1
1111 1111 1111 1011= -5to
1111 1111 1111 1101= -3Logical -5>>1
1111 1111 1111 1011= -5to
0111 1111 1111 1101= 32,765or at least, this is how i understand it