Why it matters

Bit tricks eliminate branches in hot loops. Understanding them shapes performance intuition.

Advertisement

The architecture

x & (x-1) clears lowest set bit; x & -x isolates it.

XOR swap avoids temp variable.

Parity by fold-XOR.

Classic bit tricksClear lowest bitx & (x-1)Isolate lowestx & -xParity foldXOR halvesPopular in interviews and hot loops; document intent because tricks are opaque
Bitwise tricks.
Advertisement

How it works end to end

Kernighan's popcount loop.

Parity via fold and XOR reduction.

Swap without temp using XOR.