Python Bitwise Operators – Start operating on the bits of numbers
Today, we are going to understand bitwise operators. Python bitwise operators include And, Or, Xor, 1’s complement, Left-shift, and Right-shift. These operators are called bitwise because they operate on bits of the number.
A number in binary is represented in the form of zeroes ‘0’ and ones ‘1’. For example: the number 1234 in binary is represented as ‘10011010010’.
The bitwise operators work on these stream of bits which means that they work directly at the binary level. It may sound scary, but actually, it’s very easy to understand once you know how binary numbers are represented.
Before moving ahead, first get yourself acquainted with the Python operators.
Types of Python Bitwise Operators
Let us go through each of them in a little bit more detail.
1. Bitwise And (&)
The bitwise AND operator takes two arguments and performs AND operation on the operands bit by bit.
As you can observe, the operation will only be 1 when both operands are 1 otherwise it remains 0.
2. Bitwise Or (|)
The bitwise OR operator takes two arguments and performs OR operation on each bit of the value of the operand.
3. Bitwise Xor (^)
The bitwise Xor also takes two operands and performs Xor operation on the binary digits.
One property of bitwise Xor is that it will return 0 only if both the numbers are the same.
5^5 = 0
128^128 = 0
Any doubts in Python bitwise operators till now? Mention them in the comment section.
4. Bitwise 1’s complement (~)
The bitwise 1’s complement only works on one operand. It is used to get the 1’s complement of a number.
Let’s see the example of how 1’s complement works.
~5 = -6
It flips all the bits until it reaches first zero from the right, so ~x is the same as -(x+1)
~10 = -11
~258 = -259
5. Left-Shift (<<)
The left shift operator shifts the bits of the numbers by a specified number of places. It adds zeroes to the empty least significant places.
digit << number_of_places_to_shift
This will be better understood with the example.
1<<2 = 4
1 is represented as ‘1’
We shift the digits to left up to two places. So, it will be ‘100’ in binary and 4 in decimal.
10<<4 = 160
10 in binary is ‘1010’
Shifting 4 digits to the left will make it ‘1010 0000’ which is 160 in decimal.
6. Right-Shift (>>)
This is similar to the left shift, but instead of shifting digits to the left we are shifting them to the right. In this process, some of the bits are lost.
10>>2 = 2
Let’s see how this works.
So, 10 in binary is “1010”. By shifting “1010” to 2 places to the right we will lose our 2 rightmost digits and we will only be left with “10”. In decimal, “10” represents 2 and that’s our answer.
25>>2 = 6
25 in binary is “11001”. We right shift them up to 2 places so, we are now left with “110” which is 6.
That was all about the bitwise operators in Python. Usually, they are not used in normal programming but in some cases, they are very useful like bit manipulations, encryptions, and compressions.
Operations on Bit operators are faster than the other operators. Knowing how to use them effectively will make you a better programmer.
If you liked TechVidvan’s Python bitwise operators article, don’t forget to share our article on social media.