Java Left Shift Operator
Description
The left shift operator, <<, shifts all of the bits in a value to the left a specified number of times.
Syntax
It has this general form:
value << num
Example
The following code shifts byte type variable.
public class Main {
public static void main(String args[]) {
byte a = 64, b;
int i;/*w ww . j a v a 2 s. c om*/
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
The output generated by this program is shown here:
Example 2
Each left shift has the effect of doubling the original value. The following program illustrates this point:
public class Main {
public static void main(String args[]) {
int num = 0xFFFFFFF;
/* w w w.ja v a2s . c om*/
for (int i = 0; i < 4; i++) {
num = num << 1;
System.out.println(num);
}
}
}
The program generates the following output: