Shift Operators in Java
11 Apr 2025 | 4 min read
In Java, shift operators are a special type of operator that work on the
bits of the data. These operators are used to shift the bits of the numbers
from left to right or right to left, depending on the type of shift operator
used. There are three types of shift operators in Java:
1. Signed Left Shift Operator (<<)
2. Signed Right Shift Operator (>>)
3. Unsigned Right Shift Operator (>>>)
Note: Java does not support the unsigned left shift operator (<<< ).
Operator Operator Name Uses Descrip
<< Signed Left Shift a<<b Shifts th
Operator a numbe
left as s
second
shifting
right.
>> Signed Right Shift a>>b Shifts th
Operator a numbe
right as
second
discards
>>> Unsigned Right Shift a>>>b Shifts th
Operator a numbe
right as
second
discards
and shif
the left.
Signed Left Shift Operator
The signed left shift operator is a special type of operator used to move
the bits of the expression to the left according to the number specified
after the operator.
Let's understand some examples to understand the working of the left
shift operator.
Consider x =5.
Binary equivalent of 5 is 0101.
Assume that the statement is as follows:
x<<4, let y be 4
Let's implement the above logic in a Java program.
Example
1. public class Main {
2. public static void main( String[ ] args ) {
3. int x = 5 ;
4. // shifting the value by 4 using the left shift operator
5. int y = x << 4 ;
6. // printing the left-shifted value
7. [Link]( " The value is: " +y) ;
8. }
9. }
Output:
The value is: 80
Signed Right Shift Operator
The signed right shift operator is a special type of operator used to move
the bits of the expression to the right according to the number specified
after the operator.
Let's understand some examples to understand the working of the right
shift operator.
Consider x=80.
Binary equivalent of 80 is 1010000.
Assume that the statement is as follows:
x>>4, let y be 4
Let's implement the above logic in a Java program.
Example
1. public class Main {
2. public static void main( String[ ] args ) {
3. int x = 80 ;
4. // shifting the value by 4 using the left shift operator
5. int y = x >> 4 ;
6. // printing the left-shifted value
7. [Link]( " The right-shifted value is: " +y) ;
8. }
9. }
Output:
The right-shifted value is: 5
Unsigned Right Shift Operator
The unsigned right shift operator is a special type of right shift operator
that does not use the signal bit to fill in the sequence. The unsigned sign
shift operator on the right always fills the sequence with 0.
Let's take the same example of the right-shift operator to understand the
concept of the left-shift operator.
1. x => 40 => 0000 0000 0000 0000 0000 0000 0010 1000
A negative number is the 2's complement of its positive number. So,
1. y => -40 => 1111 1111 1111 1111 1111 1111 1101 1000
Thus x >>> 2 = 0000 0000 0000 0000 0000 0000 0000 1010
And y >>> 2 = 0011 1111 1111 1111 1111 1111 1111 0110
Example
1. public class Main {
2. public static void main( String[ ] args ){
3. int x = 40 ;
4. int y = -40 ;
5. int ans = x >> 5 ;
6. int ans2 = y >> 5 ;
7. [Link]( " The right-sifted value of 40 by 5 is: "+ans
);
8. [Link]( " The right-shifted value of -40 by 5 is: " +a
ns2) ;
9. }
10. }
Output:
The right sifted value of 40 by 5 is : 1
The right shifted value of -40 by 5 is : -2
Note: Unlike the unsigned Right Shift, there is no "<<<" operator in
Java because the logical (<<) and arithmetic left-shift (<<<)
operations are identical.
Important Points to Remember
1. Left Shift (<<) Multiplies by Powers of 2: Shifting left by n
positions (x << n) is equivalent to multiplying x by 2^n. Example: 5
<< 4 is 5 * 2^4 = 80.
2. Right Shift (>>) Divides by Powers of 2: Shifting right by n
positions (x >> n) is equivalent to dividing x by 2^n, while keeping
the sign intact. Example: 80 >> 4 is 80 / 2^4 = 5.
3. Unsigned Right Shift (>>>) Fills with Zeroes: Unlike >>, which
preserves the sign bit for negative numbers, >>> always fills with
zeros.
4. Negative Numbers and >>>: When using >>> on negative
numbers, Java fills the leftmost bits with zeroes instead of sign
extension, changing the result significantly. Example: -40 >>> 2
results in a large positive number.