What is the difference between >>>
and >>
operators in Java?
The right shift logical operator (>>> N
) shifts bits to the right by N positions, discarding the sign bit and padding the N left-most bits with 0's. For example:
-1 (in 32-bit): 11111111111111111111111111111111
after a >>> 1
operation becomes:
2147483647: 01111111111111111111111111111111
The right shift arithmetic operator (>> N
) also shifts bits to the right by N positions, but preserves the sign bit and pads the N left-most bits with 1's. For example:
-2 (in 32-bit): 11111111111111111111111111111110
after a >> 1
operation becomes:
-1: 11111111111111111111111111111111