Signed right shift operator . Can anyone tell the below problem ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Signed right shift operator . Can anyone tell the below problem ?

8>>1 =4 it's ok -8>>1=-4 it's also ok but if I write -8>>-1 then output is -1 why ??

20th Sep 2017, 4:44 PM
Vishesh
1 Answer
+ 5
I'm guessing here, but it looks to me as you completely shift the bits out filling the result with the sign bit: -8>>-1 = -8>>63 = -1. Note that 8>>-1 = 8>>63 = 0. I couldn't find anything in the Java docs to verify the issue. If you truly want to left shift on negative, test for it and handle it.
20th Sep 2017, 5:27 PM
John Wells
John Wells - avatar