# 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.

## Introduction

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`

= 12/2^{1} = 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 = a/2

^{b}

## Arithmetic right shift (>>)

### Right shift

The right shift operator is written as `>>`

.

Integers are stored in memory as a series of bits. For example, the number `6`

stored as a 32-bit `int`

would be:

`6 = 00000000 00000000 00000000 00000110`

Shifting this bit pattern to the right one position `(6 >> 1)`

would result in the number `3`

:

` 6 >> 1 = 00000000 00000000 00000000 00000011`

As you can see, the digits have shifted to the right by one position, and the last digit on the left is filled with a zero. You might also note that shifting right is equivalent to dividing by powers of 2.

So,

6 >> 1 => 6/2^{1} = 6/2 = 3

6 >> 3 => 6/2^{3} = 6/8 = 0

A good optimization compiler will replace division with shifts when possible.

```
1011 >> 1 → 1101
1011 >> 3 → 1111
0011 >> 1 → 0001
0011 >> 2 → 0000
```

The first two numbers had a `1`

as the most significant bit, so more `1's`

were inserted during the shift. The last two numbers had a `0`

as the most significant bit, so the shift inserted more `0's`

.

If a number is encoded using two’s complement, then an arithmetic right shift preserves the number’s sign, while a logical right shift makes the number positive.

```
// Arithmetic shift
1011 >> 1 → 1101
1011 is -5
1101 is -3
```

So, in simple terms, `>>`

(right shift) takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

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

### Formula

x >> y = x/2

^{y}

For example, if we interpret this bit pattern as a negative number.

`10000000 00000000 00000000 01100000`

we have the number -2,147,483,552. Shifting this to the right 4 positions with the arithmetic shift (-2,147,483,552 >> 4) would give us:

`11111000 00000000 00000000 00000110`

or the number -134,217,722

So we see that we have preserved the sign of our negative numbers by using the arithmetic right shift rather than the logical right shift. Moreover, once again, we see that we are performing division by powers of 2.

a >> b = a/2

^{b}

## Solution

### Java

```
class RightShift {
private static int helper(int number, int i) {
return number >> i;// divides `number` with 2^i times.
}
public static void main(String[] args) {
int number = 100;
System.out.println(number + " shifted 1 position right, yields to " + helper(number, 1));
System.out.println(number + " shifted 2 positions right, yields to " + helper(number, 2));
System.out.println(number + " shifted 3 positions right, yields to " + helper(number, 3));
System.out.println(number + " shifted 4 positions right, yields to " + helper(number, 4));
}
}
```

### Python

```
def helper(number,i):
return number >> i
number=100
print(number , "shifted 1 position left,yields to ",helper(number,1))
print(number , "shifted 2 position left,yields to ",helper(number,2))
print(number , "shifted 3 position left,yields to ",helper(number,3))
print(number , "shifted 4 position left,yields to ",helper(number,4))
```

### JavaScript

```
const RightShift = (number, i) => number >> i;
let number = 100;
console.log (number + " shifted 1 position right, yields to " + RightShift (number, 1));
console.log (number + " shifted 2 positions right, yields to " + RightShift (number, 2));
console.log (number + " shifted 3 positions right, yields to " + RightShift (number, 3));
console.log (number + " shifted 4 positions right, yields to " + RightShift (number, 4));
```

### C++

```
#include <iostream>
using namespace std;
int helper(int number, int i) {
return number >> i;
}
int main() {
int number = 100;
cout << number << " shifted 1 position left , yields to " << helper(number, 1) << endl;
cout << number << " shifted 2 position left , yields to " << helper(number, 2) << endl;
cout << number << " shifted 3 position left , yields to " << helper(number, 3) << endl;
cout << number << " shifted 4 position left , yields to " << helper(number, 4) << endl;
return 0;
}
```

### TypeScript

```
export const RightShift = (n: number, i: number): number => n >> i;
let n: number = 100;
console.log(n + " shifted 1 position right, yields to " + RightShift(n, 1));
console.log(n + " shifted 2 positions right, yields to " + RightShift(n, 2));
console.log(n + " shifted 3 positions right, yields to " + RightShift(n, 3));
console.log(n + " shifted 4 positions right, yields to " + RightShift(n, 4));
```

So, we see that shifting to the right is equivalent to division by powers of 2.

## Gopi Gorantala Newsletter

Join the newsletter to receive the latest updates in your inbox.