Overview
Bit manipulation integers ke bits (0/1) par directly kaam karta hai, fast aur memory-efficient processing ke liye.
Analogy
Jaise room mein light switches ki row—har switch ek bit hai, independently on/off kar sakte hain.
Step-by-step
- AND(&): dono 1 toh 1
- OR(|): koi ek 1 toh 1
- XOR(^): alag hain toh 1 (toggle)
- NOT(~), left shift (<<), right shift (>>)
Visual
5 = 0101
3 = 0011
5&3=0001=1
5|3=0111=7
5^3=0110=6
5<<1=1010=10
Common mistakes
- Python integers arbitrary precision hain—bit length variable
- Python mein ~n is -(n+1)
- C/Java se shift overflow thinking
Practice questions
- Bit operation se n power of 2 hai check karo (n&(n-1)==0)
- Bit mask se integer ke saare subsets generate karo
Time
O(1) per bit operation