Bitwise operators work directly on the binary digits (bits) of whole numbers. You will not need them in every program, but they show up in interviews, competitive programming, permissions and flags, low-level hardware code and some quick tricks such as checking whether a number is even.
To follow this lesson, it helps to see numbers in binary. Python’s bin() function does that: bin(12) is '0b1100', where 0b just marks a binary number.
Python Bitwise Operators Table
| Operator | Name | What it does |
|---|---|---|
& |
AND | A bit is 1 only if it is 1 in both numbers |
| |
OR | A bit is 1 if it is 1 in either number |
^ |
XOR | A bit is 1 if the two bits are different |
~ |
NOT | Flips every bit; in Python ~n equals -(n + 1) |
<< |
Left shift | Moves bits left; n << k equals n × 2k |
>> |
Right shift | Moves bits right; n >> k equals n // 2k |
Example with 12 and 10
a = 12
b = 10
print(bin(a), bin(b))
print(a & b, bin(a & b))
print(a | b, bin(a | b))
print(a ^ b, bin(a ^ b))
print(~a)
print(a << 2)
print(a >> 2)
Output:
0b1100 0b1010
8 0b1000
14 0b1110
6 0b110
-13
48
3
Here is the working, bit by bit:
12 = 1 1 0 0
10 = 1 0 1 0
-----------------
AND = 1 0 0 0 = 8
OR = 1 1 1 0 = 14
XOR = 0 1 1 0 = 6
12 << 2 shifts 1100 two places left to get 110000, which is 48 (12 × 4). 12 >> 2 shifts it two places right to get 11, which is 3 (12 // 4).
Why Is ~12 Equal to -13?
Python integers have no fixed size, and negative numbers behave as if they were stored in “two’s complement” form. In that system, flipping every bit of n always gives -(n + 1). So ~12 is -13, ~0 is -1, and ~-1 is 0. You don’t need to work through the binary every time; just remember the formula.
Shifts as Fast Multiplication and Division
print(5 << 1, 5 << 3, 40 >> 1, 40 >> 3)
print(-9 >> 1, -9 // 2)
Output:
10 40 20 5
-5 -5
Left shift by k multiplies by 2k, and right shift by k floor-divides by 2k. For negative numbers the right shift rounds down (towards minus infinity), just like //.
Practical Uses
1. Check Even or Odd
The last bit of an odd number is always 1, so n & 1 tells you whether a number is odd:
for n in (7, 10):
print(n, "odd" if n & 1 else "even")
Output:
7 odd
10 even
In everyday Python code, n % 2 == 0 is clearer and just as fast, but the bitwise version is common in interview questions.
2. Check if a Number Is a Power of Two
for n in (8, 12, 64):
print(n, n > 0 and (n & (n - 1)) == 0)
Output:
8 True
12 False
64 True
A power of two has exactly one 1-bit (8 is 1000). Subtracting 1 turns that bit off and all the bits below it on (7 is 0111), so ANDing them gives 0.
3. Permissions with Flags
Many systems, including Linux file permissions, store several yes/no settings in one number. Each setting gets its own bit:
READ = 4 # 100
WRITE = 2 # 010
EXECUTE = 1 # 001
perm = READ | WRITE # give read and write
print(perm)
print(bool(perm & WRITE)) # has write?
print(bool(perm & EXECUTE))
perm = perm | EXECUTE # add execute
print(perm)
perm = perm & ~WRITE # remove write
print(perm)
Output:
6
True
False
7
5
This is exactly why a Linux permission like 7 means read + write + execute, and 5 means read + execute.
4. Swap Two Numbers with XOR
x, y = 5, 9
x = x ^ y
y = x ^ y
x = x ^ y
print(x, y)
Output:
9 5
This is a classic interview trick. In real Python code, simply write x, y = y, x.
Converting Between Binary and Decimal
print(format(12, "08b"))
print(int("1100", 2))
Output:
00001100
12
format(n, "08b") shows a number as 8 binary digits with leading zeros, and int(text, 2) reads a binary string back into a number.
Bitwise vs Logical Operators
& and | are not the same as and and or. Bitwise operators always evaluate both sides and work bit by bit, while logical operators stop early (short-circuit). With plain True/False values they give the same answer, but be careful with brackets, because & binds more tightly than comparisons:
print(True & False, True | False)
print((5 > 3) & (2 > 1))
Output:
False True
True
Without the brackets, 5 > 3 & 2 > 1 would be read as 5 > (3 & 2) > 1. For conditions, use and / or.
Practice Questions
print(9 & 5, 9 | 5, 9 ^ 5)print(1 << 10)print(~0)
Answers: 1) 1 13 12, because 9 is 1001 and 5 is 0101. 2) 1024, which is 210. 3) -1, because ~n is -(n + 1).
Frequently Asked Questions
What are bitwise operators in Python?
Operators that work on the individual bits of integers: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift) and >> (right shift).
Why does ~5 give -6 in Python?
Because Python follows two’s complement rules for negative numbers, so ~n always equals -(n + 1).
What is the difference between & and and?
& compares numbers bit by bit and always evaluates both sides. and works on truth values and stops as soon as the result is known.
Where are bitwise operators used in real life?
In permission flags, network addresses and subnet masks, graphics and colour values, compression, encryption and hardware programming.
Next: Membership operators in Python · Previous: Python comparison operators · All operators: Python operators overview