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.
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 << number | It moves all the bits of the operand towards the left by a given number. |
Signed Right Shift | >> | Operand >> number | It moves all the bits of the operand towards the right by a given number. |
Unsigned Right Shift | >>> | Operand >>> number | It 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.