Bitwise Operators in Python
Bitwise operators perform operations on integers at the level of individual bits. They treat the values as binary numbers (sequences of 0s and 1s) and operate on their bits.
Bitwise operations are useful in low-level programming, cryptography, networking, game development, and situations where you need fast manipulation of flags and bitfields.
List of Bitwise Operators
Operator | Symbol | Description |
---|---|---|
AND | & | Sets each bit to 1 if both bits are 1 |
OR | | | Sets each bit to 1 if one of two bits is 1 |
XOR | ^ | Sets each bit to 1 if only one bit is 1 |
NOT | ~ | Inverts all bits (bitwise complement) |
Left Shift | << | Shifts bits to the left (multiply by 2ⁿ) |
Right Shift | >> | Shifts bits to the right (divide by 2ⁿ) |
1. Bitwise AND (&
)
bitwise_and.py
a = 5 # 0101 in binary
b = 3 # 0011 in binary
result = a & b
print(result)
output.txt
1
Explanation:
- Binary of 5:
0101
- Binary of 3:
0011
- Result:
0001
(decimal 1)
2. Bitwise OR (|
)
bitwise_or.py
a = 5 # 0101
b = 3 # 0011
result = a | b
print(result)
output.txt
7
Explanation:
- Result:
0111
→ decimal7
3. Bitwise XOR (^
)
bitwise_xor.py
a = 5 # 0101
b = 3 # 0011
result = a ^ b
print(result)
output.txt
6
Explanation:
- XOR sets bits to 1 where bits are different:
- Result:
0110
→ decimal6
4. Bitwise NOT (~
)
bitwise_not.py
a = 5
result = ~a
print(result)
output.txt
-6
Explanation:
- Bitwise NOT inverts all bits and returns the two’s complement of the result.
- For 5 →
~5
=-(5 + 1)
=-6
.
5. Left Shift (<<
)
bitwise_left_shift.py
a = 5 # 0101
result = a << 1
print(result)
output.txt
10
Example 2:
bitwise_left_shift_2.py
a = 3 # 0011
result = a << 2
print(result)
output.txt
12
Explanation:
- Left shift moves bits to the left and fills with zeros — multiplying by 2ⁿ.
6. Right Shift (>>
)
bitwise_right_shift.py
a = 8 # 1000
result = a >> 1
print(result)
output.txt
4
Example 2:
bitwise_right_shift_2.py
a = 10 # 1010
result = a >> 2
print(result)
output.txt
2
Explanation:
- Right shift moves bits to the right — dividing by 2ⁿ (integer division).
Practical Example: Using Bitwise Flags
bitwise_flags.py
READ = 0b0001 # 1
WRITE = 0b0010 # 2
EXECUTE = 0b0100 # 4
permissions = READ | WRITE
print("Can read:", permissions & READ != 0)
print("Can execute:", permissions & EXECUTE != 0)
output.txt
Can read: True
Can execute: False
Explanation:
- Using bitwise flags helps in representing combinations of states efficiently.
- We can test for a specific permission using
&
.
Summary
-
Bitwise operators operate directly on bits.
-
Useful for low-level control, performance optimizations, and flags.
-
Knowing these helps you in:
- Memory-efficient coding
- Hardware programming
- Game engines
- Cryptography and encoding