Java

Shift Operators in Java Explained

The shift operators in Java belong to the bitwise category of Java operators and manipulate the bits of a number. Using a bitwise shift operator, the number of bits can be shifted to the left or right. The number of bits that are to be shifted is inputted by the user as an integer. This post would have the following learning outcomes:

  • types and working of shift operators
  • examples that demonstrate the usage of shift operators

How do shift operators work

This section serves as the first learning outcome of this guide. You would get the basic understandings and the syntax to use various types of shift operators in Java. The shift operators are divided into three types that are listed below:

Signed Left shift operator

The left shift operator shifts the specified number of bits towards lefts and the vacant bits are filled with a sign bit on the “right” side of the binary. The syntax to use the left shift operator is followed by using the syntax provided below:

variable/operand << number;

From the above syntax, it is observed that any variable or number can be used to apply the left shift operator. Whereas the number represents the "number of bits" that you want to shift.

Signed Right Shift operators

The right shift operator adds the specified number of bits from the “right” of the bit or one can say that the specified number of bits from the right is removed. The following syntax may be followed for right shift operators:

Variable/operand >> number;

Any variable or number can be used for the right shifting of bits and the number represents the bits that will be shifted

Unsigned right shift operator

The unsigned right shift operator also works the same as the signed right shift operator. The difference between both is that the signed right shift operator places the sign bit whereas the unsigned uses “0” to occupy the vacant bits. The following syntax may be used for unsigned right shift operator:

variable/operand >>> operator;

Here you go! you would have learned the working mechanism of shift operators in Java.

How to use shift operators in Java

This section demonstrates the usage of shift operators via a few examples of Java.

How to use left shift operators

The Java code provided below provides the demonstration of the left-shift operator (you may also use the name signed left-shift operator).

package newpack;

public class linux {

    public static void main(String[]args) {

        int a = 10;
       //binary of variable a
        System.out.println(Integer.toBinaryString(a));
       
        // applying left shift operator on variable a
        int b=a<<2;
       
        System.out.println(b);
       
        //binary of variable b
        System.out.println(Integer.toBinaryString(b)); 
        }
}

The code is described as:

  • variable a is created
  • binary of variable a is printed
  • applied left shift operator on a and the value is stored in b
  • prints the value of b after shifting the 2 bits from left
  • prints the binary of new variable b

The output of the above code is shown below

How to use right shift operators

The right shift operator (also known as signed right shift operator) is used in the following statement on a variable c and it shifts 3bits to the right.

int d=c>>3;

This statement is exercised in the following Java code:

package newpack;

public class linux {

    public static void main(String[]args) {

        int c = 29;
       //binary of variable c
        System.out.println(Integer.toBinaryString(c));
       
        // applying right shift operator on variable c
        int d=c>>3;
       
        System.out.println(d);
       
        //binary of variable d
        System.out.println(Integer.toBinaryString(d)); 
        }
}

The output of the above code is provided below:

How to use unsigned right shift operators

The unsigned right shift operator works alike the right shift operator, but it uses 0 to occupy vacant spaces. The following statement applies the unsigned right shift operator on a variable e and stores the result in variable f.

int f=e>>>2;

The code that uses the above statement is provided below:

package newpack;

public class linux {

    public static void main(String[]args) {

        int e = 17;
       //binary of variable e
        System.out.println(Integer.toBinaryString(e));
       
        // applying unsigned right shift operator on variable e
        int f=e>>>2;
       
        System.out.println(e);
       
        //binary of variable f
        System.out.println(Integer.toBinaryString(f)); 
        }
}

The output of the above code is displayed below:

So, these examples would have paved the concept of shift operators.

Conclusion

In Java, the shift operators allow you to shift the number of binaries to the left or right. These operators belong to the bitwise operator’s category and are categorized into three types. This article provides the working and usage of all types of shift operators. The left shift operators and right shift operators are also known as signed left shift and signed right shift. These two categories use the signed bit to occupy the vacant bits whereas the unsigned right shift operators exercised 0’s for vacant bits.

About the author

Adnan Shabbir