1.2.9Introduction to Programming (Python)

Bitwise operators — &, - , ^, ~, - , -

1,811 words8 min readdifficulty · medium3 backlinks

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: aa=0a \oplus a = 0 and a0=aa \oplus 0 = a.

4. NOT ~ — "flip every bit"

5. Left shift << — slide bits left

6. Right shift >> — slide bits right

Figure — Bitwise operators — &,  - , ^, ~,  - ,  -

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?
Sets bits — output 1 if at least one input bit is 1
What is special about ^ (XOR)?
1 when bits differ; satisfies aa=0a\oplus a=0 and a0=aa\oplus 0=a (toggles)
Formula for ~x in Python?
x=x1\sim x = -x-1 (two's complement bit flip)
x << k equals?
x2kx\cdot 2^k (left shift multiplies by powers of two)
x >> k equals?
x/2k\lfloor x/2^k\rfloor (right shift = integer divide by 2k2^k, floors)
Difference between & and and?
& is bitwise (per-bit); and is logical (truthiness, short-circuits, returns an operand)
12 & 10 = ?
8 (1100 & 1010 = 1000)
12 ^ 10 = ?
6 (1100 ^ 1010 = 0110)
3 << 4 = ?
48 (3163\cdot16)
Why does XOR swap work?
aa=0a\oplus a=0 cancels values so each variable recovers the other's original
Precedence trap: how does 1 & 3 == 3 parse?
As 1 & (3==3) because == binds tighter than &; use parentheses

Connections

Concept Map

base 2 sum

operate bit-by-bit

both on

at least one

bits differ

flip all bits

slide left

slide right

masks bits

sets bits

toggles bits

twos complement

derived

derived

Integer as bits

Bit positions

Bitwise operators

AND &

OR pipe

XOR ^

NOT ~

Left shift

Right shift

Flags and masks

~x = -x - 1

x times 2^k

floor of x over 2^k

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 x×2kx \times 2^k (number double hota jaata hai), aur x >> k matlab x/2k\lfloor x/2^k \rfloor (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.

Test yourself — Introduction to Programming (Python)

Connections