Bitwise Shift Operators in Java
In Java, bitwise shift operators are used to move bits of a number to the left or right. They are
mainly of three types: Left Shift (<<), Right Shift (>>), and Unsigned Right Shift (>>>).
1■■ Left Shift Operator ( << )
- Shifts all bits to the left by the given number of positions.
- Fills 0s on the right side.
- Each shift to the left multiplies the number by 2.
Example: 5 << 1
Original value 0000 0101 (decimal 5)
Shift left by 1 0000 1010
Result Decimal 10
Explanation: 5 << 1 shifts all bits one place left and inserts 0 on the right. Hence, 5 × 2¹ = 10.
2■■ Right Shift Operator ( >> )
- Shifts all bits to the right by the given number of positions.
- Preserves the sign bit (copies leftmost bit).
- Each right shift divides the number by 2.
Example: 20 >> 2
Original value 0001 0100 (decimal 20)
Shift right by 2 0000 0101
Result Decimal 5
Explanation: 20 >> 2 shifts bits two places right and keeps the sign bit 0. Hence, 20 ÷ 2² = 5.
3■■ Unsigned Right Shift Operator ( >>> )
- Shifts all bits to the right and fills 0s on the left side.
- Does not preserve the sign bit (ignores the sign).
- Used mainly for unsigned shifting.
Example: -4 >>> 1
Original value 11111111 11111111 11111111 11111100
Shift right by 1 01111111 11111111 11111111 11111110
Result Decimal 2147483646
Explanation: -4 >>> 1 shifts bits one place right and fills 0 on the left. Since the sign bit is ignored,
the result becomes a large positive number.
■ Summary Table
Operator Name Effect Example Output
<< Left Shift Multiplies by 2■ 5 << 1 10
>> Right Shift (signed) Divides by 2■, keeps sign 20 >> 2 5
>>> Unsigned Right Shift Right shift with 0-fill -4 >>> 1 2147483646