Home » Java » Why are there two Right Shift operators and only a single Left Shift operator in Java?

Why are there two Right Shift operators and only a single Left Shift operator in Java?

Posted by: admin December 28, 2021 Leave a comment

Questions:

I am new to java, and came to know that there are two Right Shift operators in java >> and >>>, but only one left shift operator <<. Why is it so?

Answers:

Because logical and arithmetic left-shift operations are identical (from wikipedia).

Right
Left

Right
Left

Notice what happens to sign bit (left-most bit) in both left shifts.

###

The right shift operators are one with (>>) and one without (>>>) sign-extension. The left shift operation does not have a sign extension component.

For instance, the following expressions are both true:

(0x80000000 >>  1) == 0xC0000000
(0x80000000 >>> 1) == 0x40000000