What is Left Shift Operator?

The left shift operator, <

Syntax

 

It has this general form:

 

value &lt;&lt; num

 

Example

 

The following code shifts byte type variable.

 

 

public class Main {

  public static void main(String args[]) 

{

    byte a = 64, b;

    int i;

    i = a &lt;&lt; 2;

    b = (byte) (a &lt;&lt; 2);

    System.out.println("Original value of a: " + a);

    System.out.println("i and b: " + i + " " + b);

  }

}

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;
for (int i = 0; i < 4; i++)
{
num = num << 1;
System.out.println(num);

}

}
}

public class Main {
public static void main(String[] a) {

System.out.println(9 &lt;&lt; 7);//1152

}
}

output
1152

// Left shifting a byte value.
public class MainClass {
public static void main(String args[]) {
byte a = 64, b;
int i;

i = a &lt;&lt; 2;
b = (byte) (a &lt;&lt; 2);

System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);

}
}

output
Original value of a: 64
i and b: 256 0