Skip to content

Bitwise right shift operator in Python

  • by

Python Bitwise Right shift operator >> is used to shift the binary sequence to the right side by a specified position. for example, if the number is 14.

x >> n

Then the Binary representation of 14 is 00001110 (for the sake of clarity let’s write it using 8-bit)

14 = (00001110) 2

Then 14 >> 1 will shift the binary sequence by 1 position to the right side.

Python Bitwise right shift operator

Simple example code bitwise right-shift operator to integer 32 shifting it by one position:

x = 32
# Shift by one position to the right
res = x >> 1
print(res)

# Shift by two positions to the right
res = x >> 2
print(res)

Output:

Python Bitwise right shift operator

The bit representation of decimal 32 is "00100000". If you shift it by one position to the right, you obtain a binary “00010000” (decimal 16). If you shift by two positions to the right, you obtain a binary "00001000"

Do comment if you have any doubts or suggestions on this Python Bitwise operator topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading