![]() |
VOOZH | about |
Shift operators in Java are bitwise operators that shift the binary bits of a number left or right. They work directly on binary data and are commonly used for fast arithmetic operations and low-level bit manipulation.
Java provides the following three shift operators:
The left shift operator shifts the bits of a number to the left by a specified number of positions. Zeros are added to the right side.
left_operand << number
Original value of a: 64 i and b: 256 0
Explanation:
The right shift operator shifts bits to the right. The sign bit (MSB) is copied to fill vacant positions, preserving the number’s sign.
left_operand >> number
2
Explanation:
b = 0xf1
Explanation:
The unsigned right shift operator shifts bits to the right and fills the leftmost bits with 0, regardless of the sign.
left_operand >>> number
2 1073741822
Explanation:
Java does not provide an unsigned left shift operator (<<<) because: