Java Right-shift Assignment

In Java, Right-shift Assignment Operator is used to right shift value in the variable (left operand) by a value (right operand) and assign the result back to this variable (left operand).

In this tutorial, we will learn how to use Right-shift Assignment operator in Java, with examples.

The syntax to right shift a value in variable x by 2 places and assign the result to x using Right-shift Assignment Operator is

x >>= 2

Example

In the following example, we take a variable x with an initial value of 5, add right shift by 2 places, and assign the result to x, using Right-shift Assignment Operator.

Main.java

public class Main {
    public static void main(String[] args) {
        int x = 5;
        //right-shift assignment
        x >>= 2;
        System.out.println("x : " + x);
    }
}

Output

x : 1
ADVERTISEMENT

Conclusion

In this Java Tutorial, we learned about Right-shift Assignment Operator in Java, with examples.