Java Left-shift Assignment

In Java, Left-shift Assignment Operator is used to left shift the value in a 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 Left-shift Assignment operator in Java, with examples.

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

x <<= 2

Example

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

Main.java

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

Output

x : 20
ADVERTISEMENT

Conclusion

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