Shift operators in Java are used to shift the bits of a number left or right. They work directly on the binary representation of integers and are commonly used in low-level programming, performance optimization, and bit manipulation.
Java provides three shift operators. They move bits to the left or right and fill empty positions depending on the operator type.
// Demonstrating Java shift operators
public class ShiftOperators {
public static void main(String[] args) {
int a = 8;
System.out.println(a << 1);
System.out.println(a >> 1);
System.out.println(a >>> 1);
}
}
16
4
4
Left shift doubles the value. Right shift divides the value by 2. Unsigned right shift behaves the same for positive numbers.