Spokes.wiki Search Graph Growth About

bit-manipulation-wiki

Defined Term domain updated Mon Jun 15 2026 00:00:00 GMT+0000 (Coordinated Universal Time)

Bit manipulation

Bit manipulation (bit twiddling) is the practice of operating directly on the individual bits of an integer — with AND/OR/XOR/NOT, shifts, and masks — to compute results that would otherwise use arithmetic, branches, or table lookups. It is the hub concept of this spoke.

Why it exists

Two motivations recur across the sources:

  1. Branchless / data-independent computation — replace a conditional with arithmetic on bits so there is no branch to mispredict (branchless-programming: branchless-abs, sign, min/max).
  2. Parallelism within a word (SWAR) — treat a machine word as a vector of small fields and operate on all of them at once (the parallel popcount, byte-pattern detection).

The canonical references

The standing tension

Most classic bit tricks were performance optimizations for older CPUs. On modern hardware many are matched or beaten by a dedicated instruction (POPCNT, LZCNT/TZCNT) or by what the compiler already emits (conditional-move for abs/min/max). So the durable value of bit manipulation today is more about correctness-critical bit work (flags, masks, protocols, codecs, hashing) and understanding than about hand-beating the compiler — see branchless-programming for the recurring caveat.

population-count · branchless-programming · bit-twiddling-hacks · xor-swap · branchless-abs · count-set-bits-so