The bitwise operators assume their operands are 32-bit signed integers.
00000000000000000000000000001000 // in base 2
8 // in base 10
- In
8 >> 2
, the sign-propagating shift-right operator (>>
) shifts the binary number two places, preserving the sign (which is the first bit):00000000000000000000000000000010 // in base 2 2 // in base 10
- In
8 >>> 2
, the zero-fill right-shift operator (>>>
) shifts the binary number two places, filling in the left bits with 0s:00000000000000000000000000000010 // in base 2 2 // in base 10
These are identical, simply because the first bit for positive binary numbers is a zero.
For negative numbers, however, things look different:
11111111111111111111111111111000 // in base 2
-8 // in base 10
- In
-8 >> 2
, the sign-propagating shift-right operator (>>
) shifts the binary number two places, preserving the sign:11111111111111111111111111111110 // in base 2 -2 // in base 10
- In
-8 >>> 2
, the zero-fill right-shift operator (>>>
) shifts the binary number two places, filling in the left bits with 0s:00111111111111111111111111111110 // in base 2 1073741822 // in base 10