Left and Right Shift: Bitwise Operators in Python
Tags: python, programming, computerscience
Published: 2024-04-11 00:25:18

Last week while browsing on X (Twitter), I came across the following [quiz](https://twitter.com/Python_Dv/status/1774488075537936425):

![Quiz. a = 4. print(a >> 2). Options: 1, 2, True, Erro](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qh0u5j6dmm3q1c5kojqf.png)

What do you think the correct answer would be? Do you know what the `>>` sign means?

This sign means that the language will perform a **bitwise operation**. But what exactly is that?

Don't worry if you're not familiar with this operation. At the time, reading through the tweet's comments, most people had responded `True`. According to them, the operation to be executed would be "4 greater than 2". Since 280 characters are not enough to cover everything I'd like to address in solving this quiz, I decided to write this text to introduce a concept that seems to be little known.

## Bitwise Operations: Left and Right Shift

Let's start with the basics. As the name of the operation suggests, bitwise is an operation done directly on the bit, meaning it's an operation directly understood and supported by the processor, making it extremely fast.

There are various bitwise operators, but we will only discuss two of them focusing on solving the quiz: **left shift** and **right shift**. The idea of these operations is to move the digits of a certain set of bits to the right or left. It's as simple as that!

Let's suppose we have the following binary: '0b100111' ('0b' at the beginning is just a binary representation indicating that every bit to the left is 0). When we perform a left shift on it, we will move the bits one place to the left, adding a new bit to the right of the binary. This new bit will always be 0 for this operation, resulting in a new binary.

![Binary 0 1 0 0 1 1 1 with arrows pointing to the bits moved to the left resulting in 1 0 0 1 1 1 0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfj32aups3olf15663v3.png)

For right shift, the idea is quite similar, the difference is that we will move the bits one place to the right. Thus, the bit farthest to the right of the old binary will be removed from the new binary.

![Binary 1 0 0 1 1 1 with arrows pointing to the bits moved to the right resulting in 0 1 0 0 1 1 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1uw1ypbc9v8mk0usvk4.png)

## Python Syntax

Great, now that we know what the shift operators are, let's move on to the syntax in Python. The binary used in the previous examples ('0b100111') is the representation of the number 39, so, to perform a left shift of just one bit from this decimal we use the following command:

```python
>>> 39 << 1
78
>>> bin(78)
'0b1001110'
```

The first element of the operation is the number whose bits we want to move (39), the operator `<<` represents left shift, and the second element (1) is the number of bits that will be moved in the direction of the operator (left <<).

*"Hold on, does that mean we can perform more than one shift at the same time?"*

Exactly! For example:

```python
>>> 39 << 2
156
>>> bin(156)
'0b10011100'
```

The same syntax applies to right shift: the first element is the number whose bits we want to move, the operator `>>` represents right shift, and the second element is the number of bits that will be moved in the direction of the operator (>> right).

```python
>>> 39 >> 1
19
>>> bin(19)
'0b10011'
>>> 39 >> 2
9
>>> bin(9)
'0b1001'
```

Notice, whenever we perform a left shift operation the binary increases. Consequently, its decimal representation also increases. On the other hand, when performing a right shift operation, the binary always decreases, as does its decimal representation.

## Back to the Quiz

Now that the concepts of left and right shift are clearer, returning to the quiz we realize it's not that difficult after all. The operation to be done is a right shift of the binary representation of the number 4 ('0b100') moving the bits twice to the right.

```python
>>> 4 >> 2
1
>>> bin(1)
'0b1'
```

Therefore, the correct answer to the quiz is 1.

So, did you get it right on the first try? I hope you're now more familiar with this type of operation. Comment here if you want to know more about other bitwise operations.