Arithmetic and Logical Right Shifts

The logical right shift means shifting the bits to the right, and MSB(most significant bit) becomes 0. The arithmetic right shift means shifting the bits to the right, and MSB is the same as in the original number.

Logical right shift (>>>)

The logical right shift operator is written as >>>.

Integers are stored in memory as a series of bits. For example, the number 12 stored as a 32-bit int would be:

12 = 00000000 00000000 00000000 00001100

When we shift it one position (12 >>> 1), the answer is 6.

12 >>> 1 = 1221\frac{12}{2^1} = 122\frac{12}{2} = 6.

The binary representation of the number 6 is as follows.

(12 >>> 1) = 00000000 00000000 00000000 00000110

Let’s see how to represent this in a mathematical formula.

Formula

a >>> b = a2b\frac{a}{2^b}

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.