⚡ Bitwise Operators
Last Updated: 16th August 2025
Bitwise operators work at the binary level (0s and 1s).
They directly operate on the bits of integers.
Hinglish Tip 🗣: Ye operators number ke bits par kaam karte hain (0-1 ke form me).
| Operator | Name | Meaning | Example | Result |
|---|---|---|---|---|
| & | AND | 1 if both bits are 1 | 6 & 3 | 2 |
| | | OR | 1 if at least one bit is 1 | 6 | 3 | 7 |
| ^ | XOR | 1 if bits are different | 6 ^ 3 | 5 |
| ~ | NOT | Inverts all bits | ~6 | -7 |
| << | Left Shift | Shift bits to left, fills 0 on right | 6 << 1 | 12 |
| >> | Right Shift | Shift bits to right | 6 >> 1 | 3 |
✏ Syntax & Examples
a = 6 # binary: 110
b = 3 # binary: 011
print(a & b) # 2 -> 010
print(a | b) # 7 -> 111
print(a ^ b) # 5 -> 101
print(~a) # -7 -> two's complement
print(a << 1) # 12 -> 1100
print(a >> 1) # 3 -> 11
💡 Quick Practice
- Calculate 12 & 5 (hint: 12 = 1100, 5 = 0101).
- Try 7 | 4 and predict output.
- Check 9 ^ 5 → kya hoga?
- What will ~10 return?
- Use
<< to multiply 3 by 8 using shifting. - Use >> to divide 40 by 4 using shifting.
- Exercises