Monday, 25 January 2016

The Left Shift Operator in Java

How to use the Java Left Shift Operator


               The left shift operator, <<, shifts all of the bits in a value to the left a specified number of times. For each shift left, the high-order bit is shifted out (and lost), and a zero is brought in on the right.

Syntax of Left Shift Operator in Java:


value << num

num: number of positions to left-shift the value in value.

Example Program for Left Shift Operator


// A Java Program to demonstrate how the Left Shift Operator works

public class LeftShift {

    public static void main(String args[]){    

      int x=12;
      int y = x << 2
      System.out.println("The value before Left Shift: " + x);
      System.out.println("The value after Left Shift: " + y);

   }
}

Output:


The value before Left Shift: 12
The value after Left Shift: 48

No comments :

Post a Comment