Python Bitwise Operators
Table of Contents
In the last lesson, we learned identity operators. Now letโs learn how Python works with the binary bits inside integers.
Computers store whole numbers using binary digits. A binary digit is called a bit, and each bit is either 0 or 1.
Bitwise operators compare or move these bits directly.
Understanding Binary Numbers
We normally write numbers using digits from 0 to 9. This is called decimal. Binary uses only 0 and 1.
For example, decimal 5 is binary 101.
Decimal: 5Binary: 101Python can show the binary form of an integer with bin().
print(bin(5))print(bin(10))Output
0b1010b1010The 0b at the beginning tells us that the value is written in binary.
Python Bitwise Operators
Python has six main bitwise operators.
| Operator | Name | Example |
|---|---|---|
& | Bitwise AND | 5 & 3 |
| | Bitwise OR | 5 | 3 |
^ | Bitwise XOR | 5 ^ 3 |
~ | Bitwise NOT | ~5 |
<< | Left shift | 5 << 1 |
>> | Right shift | 5 >> 1 |
Let us learn them one by one.
Bitwise AND Operator (&)
Bitwise AND gives 1 only when both bits are 1.
5 = 1013 = 011 --- 001 = 1print(5 & 3)Output
1Only the last column contains 1 in both numbers, so the answer is 001.
Bitwise OR Operator (|)
Bitwise OR gives 1 when at least one of the two bits is 1.
5 = 1013 = 011 --- 111 = 7print(5 | 3)Output
7Every column has at least one 1, so the answer is 111, which is decimal 7.
Bitwise XOR Operator (^)
XOR means exclusive OR. It gives 1 when the two bits are different.
5 = 1013 = 011 --- 110 = 6print(5 ^ 3)Output
6If both bits are the same, XOR gives 0. If they are different, it gives 1.
Bitwise NOT Operator (~)
Bitwise NOT flips the bits of a number. Each 1 becomes 0, and each 0 becomes 1.
print(~5)Output
-6The negative answer may look surprising. Python stores negative integers in a form called twoโs complement.
For beginners, the easiest rule is:
~x = -(x + 1)So ~5 is -(5 + 1), which gives -6.
Left Shift Operator (<<)
The left shift operator moves bits to the left and adds zeros on the right.
5 = 1015 << 1 = 1010 = 10print(5 << 1)print(5 << 2)Output
1020For positive integers, shifting left by one position usually multiplies the number by 2.
Right Shift Operator (>>)
The right shift operator moves bits to the right. Bits that move past the end are removed.
10 = 101010 >> 1 = 0101 = 5print(10 >> 1)print(10 >> 2)Output
52For positive integers, shifting right by one position usually performs floor division by 2.
Real-Life Example: Permission Flags
Bitwise operators are often used when several yes-or-no settings are stored inside one number.
READ = 1 # 001WRITE = 2 # 010DELETE = 4 # 100
permissions = READ | WRITE
print(permissions)print(bool(permissions & READ))print(bool(permissions & DELETE))Output
3TrueFalseREAD | WRITE combines the two permissions. Then & checks whether a particular permission is present.
We will not need this pattern in every beginner program, but it is common in permissions, colors, hardware control, and low-level data work.
Common Mistakes with Bitwise Operators
Mistake 1: Confusing & with and
and is a logical operator. It works with truthy and falsy values.
& is a bitwise operator. It works with the individual bits of integers.
print(True and False) # Falseprint(5 & 3) # 1They have different jobs, so we should not use them as if they were the same operator.
Mistake 2: Expecting ^ to Mean Power
In Python, ^ means bitwise XOR. It does not mean exponentiation.
print(2 ^ 3) # 1print(2 ** 3) # 8We use ** when we want to raise a number to a power.
Mistake 3: Forgetting Parentheses
Bitwise expressions can become difficult to read when they are mixed with comparisons or arithmetic.
result = (5 & 3) == 1print(result)Output
TrueParentheses make the order clear for us and for anyone reading the code.
Next, we will learn about the if Statement so we can start making decisions in our code.