The left shift operator <<
shifts all of the bits in a value to the left a specified number of times.
It has this general form:
value << num
Here, num
sets the number of positions to left-shift.
The <<
operator moves all of the bits to the left by the number of num.
For each shift left, the high-order bit is shifted out and lost.
A zero is brought in on the right.
When a left shift is applied to an int operand, bits are lost they are shifted past bit position 31.
If the operand is a long, then bits are lost after bit position 63.
Java byte and short values are promoted to int when an expression is evaluated.
The result of such an expression is an int.
The outcome of a left shift on a byte or short value will be an int.
The bits shifted left will not be lost until they shift past bit position 31.
A negative byte or short value will be sign-extended when it is promoted to int. And the high-order bits will be filled with 1's.
The following program demonstrates left shifting a byte value:
// Left shifting a byte value. public class Main { public static void main(String args[]) { byte a = 64, b; int i;/*from w w w .j a v a2 s . c o m*/ i = a << 2; b = (byte) (a << 2); System.out.println("Original value of a: " + a); System.out.println("i and b: " + i + " " + b); } }