There are various types of operators like arithmetic operators, comparison operators, and bitwise operators in Python. In our programs, we use these operators to control the sequence of execution and to manipulate the data. In this article, we will study different python bitwise shift operators, their functioning and examples.
What are Bitwise shift operators?
Bitwise shift operators are binary operators. These operators are used to shift bits of a binary representation of a number to left or right by certain places. Bitwise shift operators are often used for operations in which we have to multiply or divide an integer by powers of 2. Here, the Bitwise left shift operator is used for multiplying a number by powers of 2 while the bitwise right shift operator in python is used to divide a number by powers of 2.
Bitwise Right Shift Operator in Python
The bitwise right shift operator in python shifts the bits of the binary representation of the input number to the right side by a specified number of places. The empty bits created by shifting the bits are filled by 0s.
The syntax for the bitwise right shift is a >> n. Here ‘a’ is the number whose bits will be shifted by ‘n’ places to the right.
The working of bitwise right shift operation can be understood from the following illustration.
Suppose we have to shift the bits of 14 by 2 places. We will first convert it into binary format.
- 14 in binary format is written as 1110.
After shifting, the two rightmost bits 1 and 0 will be discarded and the empty leftmost bits will be filled with 0s. The output of 14 >> 2 will be 0011 in binary which converts to the value 3 in integer format.
Here you can observe that we have shifted the bits by 2 places due to which the input number has been divided by 22 i.e. 4. Similarly, if we right shift the number by n bits, the integer value of the number will be divided by 2n. We can verify this output using the right shift operator in python using the following program.
myNum1 = 14
myNum2 = 2
shiftNum = myNum1 >> myNum2
print("Operand 1 is:", myNum1)
print("operand 2 is:", myNum2)
print("Result of the right shift operation on {} by {} bits is {}.".format(myNum1, myNum2, shiftNum))
Output:
Operand 1 is: 14
operand 2 is: 2
Result of the right shift operation on 14 by 2 bits is 3.
Bitwise Left Shift Operator in Python
The bitwise left shift operator in Python shifts the bits of the binary representation of the input number to the left side by a specified number of places. The empty bits created by shifting the bits are filled by 0s.
The syntax for the bitwise left shift is a << n. Here ‘a’ is the number whose bits will be shifted by ‘n’ places to the left.
The working of bitwise left shift operation can be understood from the following illustration.
Suppose we have to shift the bits of 14 by 2 places. We will first convert it into binary format.
- 14 in binary format is written as 1110.
After shifting, the empty rightmost bits will be filled with 0s. The output of 14 << 2 will be 111000 in binary which converts to the value 56 in integer format.
Here you can observe that we have shifted the bits by 2 places due to which the input number has been multiplied by 22 i.e. 4. Similarly, if we left shift the number by n bits, the integer value of the number will be multiplied by 2n. We can verify this output using the left shift operator in python using the following program.
myNum1 = 14
myNum2 = 2
shiftNum = myNum1 << myNum2
print("Operand 1 is:", myNum1)
print("operand 2 is:", myNum2)
print("Result of the left shift operation on {} by {} bits is {}.".format(myNum1, myNum2, shiftNum))
Output:
Operand 1 is: 14
operand 2 is: 2
Result of the left shift operation on 14 by 2 bits is 56.
Conclusion
In this article, we have discussed bitwise shift operators, their syntax and examples in Python. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.