Python Bitwise Operators

BITWISE OPERATORS IN PYTHON

Now, we are going to learn about Python bitwise operators. As the name suggests, these operators are going to be used, when we need to perform some operations on bit’s. So, whenever we need to work with bit’s, we would make use of the bitwise operators. We would see some different examples, which would demonstrate to us the bitwise operators.

Note: For the operations on bit’s (binary digit’s), we have given some logical tables which give the values and explanations to some operations like AND, OR, NOT, XOR, which would help you understand the bitwise and logical operators. These tables can be found below.

Python Bitwise Operators

OperatorDescriptionExample
&This operator performs AND operation between the two valuesFor an operation 5&3, the output is 1, as the AND operation between 101 and 011 gives 001, which is binary of 1.
|This operator performs OR operation between the two valuesFor an operation 5 | 3, the output is 7, as the OR operation between 101 and 011 gives 111, which is binary of 7.
^This operator performs XOR operation between the two valuesFor an operation 5 ^3, the output is 6, as the XOR operation between 101 and 011 gives 110, which is binary of 6.
~This operator returns the 1’s complement of the value. One’s complement means inverting of the bit’s, which means that the bit’s 1 become 0, and 0 become 1.If we do ~5, the output is simply going to be -6.
the bitwise inversion of the number can be considered as -(x + 1)
so, ~5 is -(5 + 1) = -6.
<<This operator performs left shift on the value by specified number of bit’s.8<<2 (read as 8 left shift by 2 bit’s), which means that we are shifting the bit’s which describe 8(1000), towards left by two bit’s, so it becomes 10000, which is 32.
>>This operator performs right shift on the value by specified number of bit’s.10>>1 (read as 10 right shift by 1), which means that we are shifting the bit’s which describe 10 (1010), towards right by 1 bit, so it becomes 0101, which is 5.

So, these were some of the bitwise operators. Also, you can find the logic tables for the bitwise operators, like bitwise AND operator, bitwise OR operator, bitwise XOR operator, etc..

Table for AND (&)

ABA&B
000
010
100
111

Table for OR ( | )

ABA&B
000
011
101
111

Table for XOR ( ^ )

ABA^B
000
011
101
110

Left shift

In the left shift operation, we are shifting the bit’s towards left by a specified number. For example, Let’s consider that we are performing the operation 8<<2, which simply means 8 left shift by 2. Now, the binary form of 8 comes out to be 1000. Since we are shifting the bit’s to the left, the output comes out to be 10000, which is 32. So, the operation 8<<2 results into 32 as an output.

Right shift

In the right shift operation, we are shifting the bit’s towards the right, by a specified number. For example, let’s consider that we are performing the operation 10>>1, which simply means 10 right shift by 1. Now, the binary form of 10 comes out to be 1010. Since we are shifting the bit’s to the left, the output comes out to be 0101, which is simply 5.

Now, let’s have a look at a simple example, which tries to demonstrate the bitwise operators.

As you can see in the above program, we have two variables, num1 and num2 with some assigned values, and then we are performing some different bitwise operations here. You can simply understand the outputs just by looking at the operations.

But the thing is that you need to know some basics about the binary digit’s, and their operations as well, related to which, some tables are given above, but still you are supposed to know how to calculate binary equivalent of some number. Have a look at the output of the above program –
1
7
6
-6
32
4
We hope that you got the basic concept of the bitwise operators here. We can make use of the bitwise operators, as and when required.