Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of Shift Operators in Java
2.1.
Signed Left Shift Operator in Java
2.2.
Signed Right Shift Operator in Java
2.3.
Unsigned Left Shift Operator in Java
3.
Difference between >> and >>> operator
4.
Frequently Asked Questions
4.1.
What is bit manipulation?
4.2.
What is >> and >>> in Java?
4.3.
What is the use of shift operator?
5.
Conclusion
Last Updated: Apr 29, 2024
Easy

Shift Operators in Java

Author Yashesvinee V
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

Operators are an integral part of any programming language. They help us work with data and variables to perform a particular task. Every data stored within a variable exists as bits or bytes in the computer. This allows the programmer to perform bit manipulation operations to solve a variety of problems in an optimised manner. The Shift operator in Java focuses on shifting the bit patterns of an operand in a particular direction. Moving the bits of a value tends to change the existing value to a new one.

Shift Operators in Java

Recommended Topic-  Iteration Statements in Java, Hashcode Method in Java, and Duck Number in Java.

Types of Shift Operators in Java

Name

Operator

Syntax

Description

Signed Left Shift

<<

Operand << numberIt moves all the bits of the operand towards the left by a given number.
Signed Right Shift

>>

Operand >> numberIt moves all the bits of the operand towards the right by a given number.
Unsigned Right Shift

>>>

Operand >>> numberIt moves all the bits of the operand towards the right by a given number, and the vacant leftmost position is filled with 0 instead of the sign bit.

 

Signed Left Shift Operator in Java

The left shift operator shifts the bits of the binary representation of a number to the left. It is equivalent to multiplying the operand with 2 raised to the number of shifting bits i.e., if n is the number of positions to be shifted, then the resulting number will be x * 2^n, where x is the operand.

public class Main
{   
    public static void main (String[] args)
  {
      int x = 5;
      int n = 2;
      int answer = x << n;
      System.out.println("Left shift " + x + " by " + n + " positions : " + answer);
    }
}

 

Output:

Left shift 5 by 2 positions : 20

 

5 in binary form is 101. By the formula, on applying left shift, the number will be 5 * 2^2 = 20. After shifting the bits towards the left, the right-most vacant spaces are filled with 0. This works similar to an unsigned shift operator, and hence, unsigned left shift operator is not specially defined.

Signed Right Shift Operator in Java

The right shift operator shifts the bits of the binary representation of a number to the right. It is equivalent to the floor division of the operand and 2 raised to the number of shifting bits, i.e. if n is the number of positions to be shifted, then the resulting number will be floor(x / 2^n), where x is the operand. (Note: floor division gives the quotient of a division rounded off to the nearest small integer.)

public class Main
{   
    public static void main (String[] args)
  {
      int x = 5, y = -5;
      int n = 2;
      int answer1 = x >> n;
      int answer2 = y >> n;
      System.out.println("Right shift " + x + " by " + n + " positions : " + answer1);
      System.out.println("Right shift " + y + " by " + n + " positions : " + answer2);
    }
}

 

Output:

Right shift 5 by 2 positions : 1
Right shift -5 by 2 positions : -2

 

5 in binary form is 101. On applying right shift, the number will be 5 / 2^2 = 1. After shifting the bits towards the right, the leftmost vacant spaces are filled with the sign bit, which in the first case is 0.

-5 in binary form is 1010. On applying right shift, the number will be -5 / 2^2 = -2. After shifting the bits towards the right, the leftmost vacant spaces are filled with the signed bit 1.

Also see, Swap Function in Java

Unsigned Left Shift Operator in Java

In unsigned right shift, all bits are shifted to the right and the vacant spots on the left are filled with 0.

public class Main
{   
    public static void main (String[] args)
  {
      int x = 7, y = -7;
      int n = 2;
      int answer1 = x >>> n;
      int answer2 = y >>> n;
      System.out.println("Right shift " + x + " by " + n + " positions : " + answer1);
      System.out.println("Right shift " + y + " by " + n + " positions : " + answer2);
    }
}

 

Output:

Right shift 7 by 2 positions : 1
Right shift -7 by 2 positions : 1073741822

 

Practice it on online java compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Difference between >> and >>> operator

OperatorDescriptionExample
>>Signed right shift operator. Shifts bits to the right, preserving the sign of the number (sign-extension).x = -8; x >> 1; // Result: -4
>>>Unsigned right shift operator. Shifts bits to the right, filling with zeros from the left.x = -8; x >>> 1; // Result: 2147483644

The >> operator preserves the sign of the number during shifting (sign-extension), while the >>> operator fills with zeros from the left, effectively treating the number as unsigned.

Frequently Asked Questions

What is bit manipulation?

Bit manipulation techniques allow us to solve different problems using the bit representation of an operand. Data encryption and data compression use these operations to encode, decode or compress a given data. They are also used in computer networking applications that involve the framing and transfer of packets across a network.

What is >> and >>> in Java?

In Java, >> and >>> are bitwise right shift operators. >> is the signed right shift operator, which shifts the bits of a number to the right by a specified number of positions, preserving the sign bit. >>> is the unsigned right shift operator, which shifts the bits of a number to the right by a specified number of positions, filling the leftmost bits with zeros.

What is the use of shift operator?

Shift operators are used in Java for bitwise manipulation of integers. They allow you to efficiently perform operations such as division or multiplication by powers of 2, extraction or insertion of specific bits, and compact representation of data. Additionally, they are commonly used in low-level programming, cryptography, and optimization tasks.

Conclusion

Every operator in Java has a unique and special use. Shift operators find their use in generation hashes, error detection using CRC, and other embedded system software. This blog covers the types and working of Shift operators in Java. It also shows a few examples to explore their implementation.

Related Links:

Operators in C++

Operators in Python

Check out the official Coding Ninjas Blog site and visit our Library for more.

Previous article
Bitwise Operator
Next article
Java Error “bad operand types for binary operator
Live masterclass