Home » Unsigned Right Shift Operator in Java

Unsigned Right Shift Operator in Java

by Online Tutorials Library

Unsigned Right Shift Operator in Java

Shift operator is a special type of operator used for bit manipulation on data. The shift operators are used to shift the bits of its first operand left to right or right to left. There are three types of shift-operators available in Java, i.e., right-shift operator, unsigned right shift operator and left-shift operator. Java doesn’t support the unsigned left-shift operator.

  1. Right-shift operator(>>)
  2. Left-shift operator(<<)
  3. Unsigned Right-shift operator(>>>)

Unsigned Right Shift Operator in Java

Right-shift operator

The Right-shift operator is a special type of operator which is used to move the bits of the shift-expression to the right. The right-shift operator shifts the bits pattern to the right according to the number of positions specified the additive-expression. Let’s take an example of positive and integer numbers to understand how the right-shift operator work.

Suppose, we have two numbers, i.e., x=40, and y=-40 and the binary representation of these numbers are as follows:

x => 40 => 0000 0000 0000 0000 0000 0000 0010 1000

Negative number is the 2’s compliment of its positive number. So,

Y => -40 => 1111 1111 1111 1111 1111 1111 1101 1000

Thus x >> 2 = 0000 0000 0000 0000 0000 0000 0000 1010

AND y >> 2 = 1111 1111 1111 1111 1111 1111 1111 0110

Left-shift operator

The Left-shift operator is another type of operator which is used to move the bits of the shift-expression to the left. The left-shift operator shifts the bits pattern to the left according to the number of positions specified the additive-expression.

Let’s understand some example of the left-shift operator to understand how it actually works.

x => 40 => 0000 0000 0000 0000 0000 0000 0010 1000

A negative number is the 2’s complement of its positive number. So,

Y => -40 => 1111 1111 1111 1111 1111 1111 1101 1000

Thus x >> 2 = 0000 0000 0000 0000 0000 0000 1010 0000

AND y >> 2 = 1111 1111 1111 1111 1111 1111 0110 1111

Unsigned right-shift operator

The unsigned right-shift operator is a special type of right-shift operator that doesn’t use the sign bit for filling the trailing position. The unsigned right-shift operator always fills the trialing position by 0.

Let’s take the same example of the right-shift operator to understand the concept of the left-shift operator.

x => 40 => 0000 0000 0000 0000 0000 0000 0010 1000

A negative number is the 2’s complement of its positive number. So,

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

Unsigned Right Shift Operator in Java

ShiftOperatorExample.java

Output

Unsigned Right Shift Operator in Java


You may also like