Java Right Shift Operator Example

This Java example demonstrates how to use the right shift operator (>>) in Java with an example.

Java Right Shift Operator

The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand. Its general form is shown here:
value >> num
Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num.

Java Right Shift Operator Example

Here is the complete example:
class OperatorExample {
    public static void main(String args[]) {
        System.out.println(10 >> 2); //10/2^2=10/4=2  
        System.out.println(20 >> 2); //20/2^2=20/4=5  
        System.out.println(20 >> 3); //20/2^3=20/8=2  
    }
}
Output:
2
5
2

Comments