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 operators, their functioning and examples.
What are Bitwise Operators?
Generally, we perform calculations on decimal values of numbers in our programs using arithmetic operators. But, we can also perform operations on the numbers in their binary form. For this, we use bitwise operators. As you can predict from the name, bitwise operators manipulate the data bit by bit. When we perform any bitwise operation on numbers that are in decimal form, the numbers are converted into binary numbers and then the bitwise operations are performed. Also, bitwise operations can only be performed on integers that may be positive or negative.
Following is the table of bitwise operators in Python.
Operation | Operator |
AND | & |
OR | | |
NOT | ~ |
XOR | ^ |
To understand the working of bitwise operators, I will advise you to understand the conversion of decimal numbers to binary. In this article, we will use two integers 8 and 14 to perform bitwise operations.
8 is represented as 1000 in binary form and 14 is represented as 1110 in binary form. In python, binary numbers are represented by a sequence of 0’s and 1’s preceded by ‘0b’. We can easily convert a decimal number to a binary number using the bin() function as shown below.
myNum1 = 8
binNum1 = bin(myNum1)
myNum2 = 14
binNum2 = bin(myNum2)
print("Binary of {} is {}.".format(myNum1, binNum1))
print("Binary of {} is {}.".format(myNum2, binNum2))
Output:
Binary of 8 is 0b1000.
Binary of 14 is 0b1110.
Bitwise AND in Python
Bitwise AND is a binary bitwise operator. In other words, the Bitwise AND operator works on two operands on their bits representation. In a Bitwise AND operation, the output bit is 1 if both the operands are 1. Otherwise, the output bit is 0. Working of the bitwise AND operator can be summarised in the following rules.
- 0 AND 0 = 0
- 0 AND 1 = 0
- 1 AND 0 = 0
- 1 AND 1 = 1
Let us understand this using the following illustration. Suppose we have to perform a bitwise AND operation on 8 and 14. We will first convert it into binary format.
- 8 in binary format is written as 1000.
- 14 in binary format is written as 1110.
To perform the bitwise AND operation on the two numbers, we will perform bitwise AND on bits of these numbers one by one starting from the rightmost bit. We will call the rightmost bit the first bit, the second rightmost bit as the second bit, the third rightmost bit the third bit and so on. As there are four bits in both 8 and 14, we will perform bitwise AND on all the bits one by one.
- Bitwise AND of the first bit of both the numbers.
0 AND 0 =0
- Bitwise AND of the second bit of both the numbers.
0 AND 1=0
- Bitwise AND of the third bit of both the numbers.
0 AND 1=0
- Bitwise AND of the fourth bit of both the numbers.
1 AND 1=1
Hence, In the output, the first bit will be 0, the second bit will be 0, the third bit will be 0 and the fourth bit will be 1. I.e. the output in binary format will be 1000 which will be the number 8 in integer format. We can verify this output using the AND operator in python using the following program.
myNum1 = 8
myNum2 = 14
andNum = myNum1 & myNum2
print("Operand 1 is:", myNum1)
print("operand 2 is:", myNum2)
print("Result of the AND operation on {} and {} is {}.".format(myNum1, myNum2, andNum))
Output:
Operand 1 is: 8
operand 2 is: 14
Result of the AND operation on 8 and 14 is 8.
Bitwise OR in Python
Bitwise OR is a binary bitwise operator. In other words, the Bitwise OR operator works on two operands on their bits representation. In a Bitwise OR operation, the output bit is 1 if either of the operands is 1. Otherwise, the output bit is 0. The working of the bitwise OR operator can be summarised in the following rules.
- 0 OR 0 = 0
- 0 OR 1 = 1
- 1 OR 0 = 1
- 1 OR 1 = 1
Let us understand this using the following illustration.
Suppose we have to perform a bitwise OR operation on 8 and 14. We will first convert it into binary format.
- 8 in binary format is written as 1000.
- 14 in binary format is written as 1110.
To perform the bitwise OR operation on the two numbers, we will perform bitwise OR on bits of these numbers one by one starting from the rightmost bit. We will call the rightmost bit the first bit, the second rightmost bit as the second bit, the third rightmost bit the third bit and so on.
As there are four bits in both 8 and 14, we will perform bitwise OR on all the bits one by one.
- Bitwise OR of the first bit of both the numbers.
0 OR 0 =0
- Bitwise OR of the second bit of both the numbers.
0 OR 1=1
- Bitwise OR of the third bit of both the numbers.
0 OR 1=1
- Bitwise OR of the fourth bit of both the numbers.
1 OR 1=1
Hence, In the output, the first bit will be 0, the second bit will be 1, the third bit will be 1 and the fourth bit will be 1. I.e. the output in binary format will be 1110 which will be the number 14 in integer format. We can verify this output using the OR operator in python using the following program.
myNum1 = 8
myNum2 = 14
orNum = myNum1 | myNum2
print("Operand 1 is:", myNum1)
print("operand 2 is:", myNum2)
print("Result of the OR operation on {} and {} is {}.".format(myNum1, myNum2, orNum))
Output:
Operand 1 is: 8
operand 2 is: 14
Result of the OR operation on 8 and 14 is 14.
Bitwise NOT in Python
Bitwise NOT is a unary bitwise operator. In other words, the Bitwise NOT operator works on one operand on its bit representation. In a Bitwise NOT operation, the output bit is 1 if the input bit is 0 and output is 0 if the input bit is 1 . Working of the bitwise NOT operator can be summarised in the following rules.
- NOT 0 = 1
- NOT 1= 0
Let us understand this using the following illustration.
Suppose we have to perform a bitwise NOT operation on 14. We will first convert it into binary format.
- 14 in binary format is written as 1110.
To perform the bitwise NOT operation on 14, we will perform bitwise NOT on each bit of the number one by one starting from the rightmost bit. We will call the rightmost bit the first bit, the second rightmost bit as second bit, the third rightmost bit the third bit and so on.
As there are four bits in 14, we will perform bitwise NOT on all the bits one by one.
- Bitwise NOT of the first bit.
NOT 0 = 1.
- Bitwise NOT of the second bit.
NOT 1 = 0.
- Bitwise NOT of the third bit.
NOT 1 = 0.
- Bitwise NOT of the fourth bit.
NOT 1 = 0.
Hence, In the output, the first bit will be 1, the second bit will be 0, the third bit will be 0 and the fourth bit will be 0. I.e. the output in binary format will be 0001 which will be the number 1 in integer format.
myNum1 = 14
notNum = ~myNum1
print("Operand 1 is:", myNum1)
print("Result of the NOT operation on {} is {}.".format(myNum1, notNum))
Output:
Operand 1 is: 14
Result of the NOT operation on 14 is -15.
Wait, the output of NOT operation is -15. But, it should be 1 according to our theory. Where did it go wrong?
Nowhere. NOT is a bitwise inversion operator and the bitwise inversion of x is defined as -(x+1) in Python. Here x is the input number. I have discussed one’s complement a bit more in the next sections.
XOR in Python
Bitwise XOR is a binary bitwise operator. In other words, the Bitwise XOR operator works on two operands on their bits representation. In bitwise XOR operation, if both the bits are the same, the result will be 0, otherwise, the result will be 1.
We can write XOR in terms of AND, OR and NOT operators as follows.
a XOR b = (b AND (NOT a)) OR (a AND (NOT b))
The working of the bitwise XOR operator can be summarised in the following rules.
- 1 XOR 0 = 1
- 1 XOR 1 = 0
- 0 XOR 0 = 0
- 1 XOR 0 = 1
Let us understand this using the following illustration.
Suppose we have to perform a bitwise XOR operation on 8 and 14. We will first convert it into binary format.
- 8 in binary format is written as 1000.
- 14 in binary format is written as 1110.
To perform the bitwise XOR operation on the two numbers, we will perform bitwise XOR on bits of these numbers one by one starting from the rightmost bit. We will call the rightmost bit the first bit, the second rightmost bit as second bit, the third rightmost bit the third bit and so on.
As there are four bits in both 8 and 14, we will perform bitwise XOR on all the bits one by one.
- Bitwise XOR of the first bit of both the numbers.
0 XOR 0 =0
- Bitwise XOR of the second bit of both the numbers.
0 OR 1=1
- Bitwise XOR of the third bit of both the numbers.
- 0 OR 1=1
- Bitwise XOR of the fourth bit of both the numbers.
1 OR 1=0
Hence, In the output, the first bit will be 0, the second bit will be 1, the third bit will be 1 and the fourth bit will be 0. I.e. the output in binary format will be 0110 which will be the number 6 in integer format. We can verify this output using the XOR operator in python using the following program.
myNum1 = 8
myNum2 = 14
xorNum = myNum1 ^ myNum2
print("Operand 1 is:", myNum1)
print("operand 2 is:", myNum2)
print("Result of the XOR operation on {} and {} is {}.".format(myNum1, myNum2, xorNum))
Output:
Operand 1 is: 8
operand 2 is: 14
Result of the XOR operation on 8 and 14 is 6.
Conclusion
In this article, we have discussed python bitwise operators, their syntax and examples. 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.