This is explained in the Java Tutorials .
In particular:
The unsigned right shift operator "→>" shifts the zero to the leftmost position, and the leftmost position after "→" depends on the expansion of the sign.
Java uses 2s add-on variables. The only aspect about 2s that you are interested in is that if the leftmost bit is 1, the number is negative. The signed bitshift supports the sign, so if the code starts with a negative value, it remains negative after the shift.
To fix your program, use >>> instead, this is a logical bit-break, ignoring the sign
source share