Bitwise operators — &, - , ^, ~, - , -
What are we even operating on?
HOW to read it in Python: bin(13) → '0b1101'. The 0b just means "binary follows".
Bitwise operators work bit-by-bit, position by position — position 0 of the answer depends only on position 0 of the inputs (except shifts, which move positions).
The six operators
1. AND & — "both on"
WHY useful: it masks — keeps only the bits you ask about, zeros the rest.
2. OR | — "at least one on"
WHY useful: it sets bits (turns chosen switches on).
3. XOR ^ — "exactly one on / they differ"
WHY useful: it toggles bits and detects differences. Key identity: and .
4. NOT ~ — "flip every bit"
5. Left shift << — slide bits left
6. Right shift >> — slide bits right

Derive the shift formulas from scratch
Worked examples
Common mistakes
Recall Feynman: explain to a 12-year-old
Imagine a row of light switches. A number is just which lights are ON.
- AND keeps a light on only if it's on in both rooms.
- OR turns it on if it's on in either room.
- XOR turns it on only if the two rooms disagree.
- NOT flips every switch.
- Shift left slides all switches left, sticking new OFF switches on the right (which doubles the number each slide); shift right slides them right and lets switches fall off the edge (halving).
Flashcards
What does & output for a bit position?
1 only if both input bits are 1 (mask / keep bits)What does | do conceptually?
1 if at least one input bit is 1What is special about ^ (XOR)?
1 when bits differ; satisfies and (toggles)Formula for ~x in Python?
x << k equals?
x >> k equals?
Difference between & and and?
& is bitwise (per-bit); and is logical (truthiness, short-circuits, returns an operand)12 & 10 = ?
12 ^ 10 = ?
3 << 4 = ?
Why does XOR swap work?
Precedence trap: how does 1 & 3 == 3 parse?
1 & (3==3) because == binds tighter than &; use parenthesesConnections
- Binary and Number Systems — base-2 representation underlies all of this
- Boolean Logic and Truth Tables —
&|^~are the bit-level versions of AND/OR/XOR/NOT - Two's Complement and Signed Integers — explains
~x = -x-1 - Bit Masks and Flags — packing many booleans into one int
- Operator Precedence in Python — the parentheses trap
- Powers of Two — why shifts multiply/divide
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, har integer asal me bits ka ek row hai — sirf 0 aur 1 ke switches. Bitwise operators in switches ko seedha control karte hain. & (AND) tab hi bit 1 deta hai jab dono taraf 1 ho — ye "mask" lagane ke liye perfect hai, yaani sirf chuni hui bits rakho. | (OR) bit set karta hai (on karta hai), aur ^ (XOR) tab 1 deta hai jab dono bits alag hon — isliye XOR "toggle" aur "difference detect" ke liye famous hai.
~ (NOT) saari bits flip kar deta hai, lekin Python me two's complement ke kaaran ek mast rule banta hai: ~x = -x - 1. Matlab ~5 = -6, na ki -5. Yaad rakho ye, warna exam me galti pakki. Shifts bahut simple hain: x << k matlab (number double hota jaata hai), aur x >> k matlab (number aadha hota jaata hai, floor ke saath).
Sabse common galti: & ko and samajh lena. and/or logical hain (true/false ke saath kaam, short-circuit), jabki &/| bit-by-bit kaam karte hain. Aur precedence trap: 1 & 3 == 3 actually 1 & (3==3) hai, kyunki == pehle chalta hai — isliye hamesha bracket lagao.
Ye topic isliye important hai kyunki hardware khud bits par chalta hai — bitwise ops fast hote hain, aur ek hi number me bahut saare true/false flags pack kiye ja sakte hain (permissions, settings, graphics). 80/20 funda: AND keeps, OR sets, XOR toggles, NOT flips, left doubles, right halves — bas yahi rat lo.