What is left shift << and Unsigned right shift >>> operator in Java? How are these useful?

By | July 8, 2020

In this post, We will talk and learn about What is left shift <<, right shift >> and Unsigned right shift >>> operator in Java? How are these useful?

All Integer in Java are of signed type (negative numbers are represented in 2’s complementary notation), hence Java provides both signed and unsigned bit shift operators to support signed and unsigned shift of bits

Left Shift Operator

It usually shifts the underlying bits of an integer to left by the given distance filling the rightmost bits with zeros.

X = a << b means the same as X = a*2^b

a is given Number and b is the shift amount.

Here is an example of 8-bit representation of number 5. and when we left shift it’s bit by 3 then the right most 3 bits are filled by zero.

And the number becomes 5*23 = 40

The same thing happens for negative numbers which are represented in 2’s complementary notation. For example, -5 becomes -40 as follow 11111011 becomes 11011000

Right Shift Operator

Shifts the bits to left by specified amount maintaining the sign of underlying integer i.e. It fills the leftmost bits with 0 if the number is positive otherwise with bit 1.

X = a >> b means same as arithmetic operation X = a / (2^b )

Unsigned right shift Operator >>> (does not respect sign of a Number, does not preserve the 1st bit as well)

We use Unsigned right shift operator >>> same as >> except that it is unsigned, it fills the leftmost

Unsigned right shift operator

Positions with bit 0 always. (Irrespective the sign of the underlying number)

For example:

1100 1100 >>> 1 becomes 0110 0110 (shown in diagram)

10000000 >>> 3 becomes 10000 in binary

256 >> 3 becomes 256 / 2^3 = 16.

 Notes :

  • Usually, Eight-bit type byte gets promoted to int in shift-expressions. To mitigate such effects we can use bit masking to get the result as a byte for example, (b & 0xFF) >>> 2. Casting can also help to achieve the same.
  • Why there is no need for an unsigned left shift?

       Because there is no need to have that. The sign bit is the rightmost bit of an integer and shifting bits to right only        requires the decision of sign. Logical and arithmetic left-shift operations are identical so << signed solves the purpose of unsigned left shift as well.

  • Uses of bitwise operators: bitwise operators are used for few very efficient mathematical calculations in Big O(1). Bloom Filter, fast mathematical calculations, hashing functions of HashMap are some of the applications.

You May Also Like:

Why should I choose Java for Software Development? What are Pros and Cons of Java?
Can we cast an int value into byte type variable? what if the value of int is larger than byte?
Can We store a double value in a long type variable without casting?
How many ways we can create an object in Java?
Why Java is not 100% Object-oriented language?
What are the differences between 32-bit and 64-bit versions of Java?

That’s all about the What is left shift <> and Unsigned right shift >>> operator in Java? How are these useful?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *