In Java, the expression a <<= 2
is a compound assignment operator that performs a left shift on the variable a
by the number of bits specified on the right side of the <<=
operator. It is equivalent to the expression a = a << 2
.
Here's an example in Java:
public class CompoundAssignmentExample {
public static void main(String[] args) {
// Declare variables
int a = 5;
// Compound assignment (left shift by 2 bits)
a <<= 2;
// Display the result
System.out.println("The value of a after a <<= 2 is: " + a);
}
}
In this example, if a
is initially 5
, after the operation, the value of a
will be 20
. This is because the binary representation of 5
is 0000 0101
, and left-shifting it by 2 bits results in 0001 0100
, which is 20
in decimal.