Java left shift operator – Java Program on Bitwise Left Shift Operator

Java left shift operator: In the previous article we have discussed about Java Program on Bitwise Complement Operator

In this article we will see the use of Bitwise Left Shift operator in Java programming language.

Java Program on Bitwise Left Shift Operator

Left shift operator in java: Bitwise Left Shift operator is also called as Signed Left Shift operator which is represented by << symbol. It shifts the bits of a number towards left with specified position.

During shift the left most bit(most significant) is discarded and the right shift bit(least significant) is replaced with 0.

Note:  Unsigned left shift operator (<<<) is not supported in Java.

Syntax:

value<<position

Where

  • value represents the binary value on which shift operation will be performed
  • position refers to the specified position on which left shift will happen by shifting bits towards left with that position

For Example:

Suppose the number is 3 whose binary is 0011

We will perform 1 bit left shift then the value will become 0110 which is 6

Program-1:(1 bit left shift operation)

class Main 
{
  public static void main(String[] args) 
  {
    //number is 3
    int num = 3;
    //performing 1 bit left shift operation 
    int result = num << 1;
    //prints 6
    System.out.println("After left shift operation:"+result);    
  }
}
Output:

After left shift operation:6

Program-2:(2 bit left shift operation)

class Main 
{
  public static void main(String[] args) 
  {
    //number is 2
    int num = 2;
    //performing 2 bit left shift operation 
    int result = num << 2;
    //prints 8
    System.out.println("After left shift operation: "+result);    
  }
}
Output:

After left shift operation: 8

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: