Shift Operators

There are three shift operators in Java:

  • Left Shift: <<

  • Signed Right Shift: >>

  • Unsigned Right Shift: >>>

The shift operators move (shift) all the bits in a number to the left or the right. The left operand is the value to be shifted and the right operand specifies the number of bit positions to shift. You can apply these operators to any integer type, but both operands are promoted to at least an int. In fact, you should limit your use of the shift operators to variables of type int or long to avoid any bit truncation effects resulting from promotions of intermediate values in your expressions.

Note

The right operand of a shift operator can be any integer value, but this can be misleading. Any shift value larger than the number ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.