Open In App

Shift Operator in Java

Last Updated : 05 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Operators in Java are used to performing operations on variables and values.  

Examples of operators: +, -, *,  /,  >>, <<.

Types of operators:

  • Arithmetic Operator,
  • Shift Operator,
  • Relational Operator,
  • Bitwise Operator,
  • Logical Operator,
  • Ternary Operator and
  • Assignment Operator.

In this article, we will mainly focus on the Shift Operators in Java.

By shifting the bits of its first operand right or left, a shift operator performs bit manipulation on data. The shift operators available in the Java programming language are listed below. The shift operator is a java operator that is used to shift bit patterns right or left.

Types of Shift Operators in Java:

Name of operator

Sign  Description
Signed Left Shift << The left shift operator moves all bits by a given number of bits to the left.
Signed Right Shift >> The right shift operator moves all bits by a given number of bits to the right.
Unsigned Right Shift >>> It is the same as the signed right shift, but the vacant leftmost position is filled with 0 instead of the sign bit.

1. Signed Left Shift Operator in Java

This operator is represented by a symbol <<, read as double less than. 

Syntax: 

left_operand  <<  number

Illustration:

Java




//  Left Shifting a byte value
 
class GFG {
    public static void main(String[] args)
    {
        byte a = 64, b;
        int i;
 
        i = a << 2;
        b = (byte)(a << 2);
        System.out.println("Original value of a: " + a);
        System.out.println("i and b: " + i + " " + b);
    }
}


Calculating the value of number<<2 if number=2. When the value of a number is shifted to the left two places, the leftmost two bits are lost. The number has a value of two. 0010 is the binary representation of the number 2. In the following example, the method for doing a left shift is explained:

Example: 

In the below example below, the binary number 0010 (in decimal 2) becomes 1000 after shifting the bits to the left (in decimal 8).

Java




// Java Program to demonstrate
// Signed Left-Shift Operator
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
        int number = 2;
 
        // 2 bit left shift operation
        int Ans = number << 2;
 
        System.out.println(Ans);
    }
}


Output

8

2. Signed Right Shift Operator in Java

The Right Shift Operator moves the bits of a number in a given number of places to the right. The >> sign represents the right shift operator, which is understood as double greater than. When you type x>>n, you tell the computer to move the bits x to the right n places.

When we shift a number to the right, the least significant bits (rightmost) are deleted, and the sign bit is filled in the most considerable place (leftmost).

Syntax:

left_operand  >>  number

Illustration:

Calculate the value of number>>2 if number=8.

When the value of a number is shifted to the right two places, the rightmost two bits are lost. The number has a value of eight. 1000 is the binary representation of the number 8. The following is an example of how to perform the right shift:

In the example above, the binary number 1000 (in decimal 8) becomes 0010 after shifting the bits to the right (in decimal 2).

Example:

Java




// Java program to demonstrate
// the Signed right shift operator
import java.io.*;
 
class GFG
    {
    public static void main (String[] args) {
        {        
        int number = 8;
       
        // 2 bit signed right shift
        int Ans = number >> 2;
       
        System.out.println(Ans);   
        }
    }
}


Java




// Masking sign extension
 
 
class GFG {
    public static void main (String[] args) {
       char hex[]={
       '0','1','2','3','4','5',
         '6','7','8','9','a','b','c',
         'd','e','f'
       };
       
      byte b=(byte) 0xf1;
       
      System.out.println("b = 0x" + hex [(b>>4) & 0x0f] + hex[b & 0x0f]);
    }
}


3. Unsigned Right Shift Operator in Java

Unsigned Right Shift Operator moves the bits of the integer a given number of places to the right. The sign bit was filled with 0s. The Bitwise Zero Fill Right Shift Operator is represented by the symbol >>>.

Syntax:

left_operand  >>>  number

Java




// Java program to demonstrate
// the Unsigned right shift operator
import java.io.*;
 
class GFG
    {
    public static void main (String[] args)
    {
        byte num1 = 8;
        byte num2 = -8;
      
        System.out.println(num1 >>> 2);   
        System.out.println(num2 >>> 2);   
    }
}


Output

2
1073741822

Note: For negative bits, the signed and unsigned right shift operators provide different results.

4. Unsigned Left Shift Operator in Java

Unlike unsigned Right Shift, there is no “<<<” operator in Java because the logical (<<) and arithmetic left-shift (<<<) operations are identical.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads