Bit Manipulation Visualizations

Bit manipulation is ordinary Boolean logic performed eight, thirty-two, or sixty-four times in parallel. The notation is compact; the bookkeeping is where people get burned. These visualizations keep bit positions aligned and the word boundary visible, so every 1 has an address and every discarded bit has somewhere obvious to fall.

The two lessons split the subject at the right seam. Bitwise Basics teaches what AND, OR, XOR, NOT, and shifts do to aligned columns. Bit Tricks assumes those rules and uses them to build masks, count set bits, and recognize powers of two.

Those are not puzzle-only moves. Masks read permission and feature-flag bits without touching their neighbors. Clearing the lowest 1 visits sparse flag sets without scanning every position, and popcount(a ^ b) measures how many bits differ. The guarded power-of-two test is useful before code relies on a capacity or alignment having exactly one set bit.

Compare the lessons

LessonMain questionOperations in focusResult to watch
Bitwise BasicsWhat does each operator do?AND, OR, XOR, NOT, <<, >>one output column or moved bit at a time
Bit TricksWhat useful invariant can the operators enforce?masks, x & (x - 1)one selected or lowest set bit disappears

The basics lesson reads columns. The tricks lesson reads structure. In one, AND decides eight independent truth-table rows; in the other, the same AND becomes a test, a clearing tool, and the engine of a loop whose iteration count is the answer.

A recommended order

Start with Bitwise Basics. Keep your eye on the bit labels rather than the decimal values. AND preserves shared ones, OR accepts either one, XOR marks disagreement, and shifts move positions instead of comparing them. The fixed 8-bit rail also explains why raw NOT needs a mask.

Then open Bit Tricks. Build a one-hot mask and follow it through test, set, and clear. After that, watch the borrow pattern in x - 1: the lowest 1 flips to 0, the suffix below it flips to 1, and AND removes exactly one set bit. The default ending compares 40, 32, and 0, so you see a normal false case, a true power of two, and the zero trap without changing presets.

What to watch

  • Keep bit index and mask value separate. Bit 2 is a position; 00000100 is the mask that selects it.
  • Ask whether an operation is idempotent or reversible. OR-setting twice is harmless; XOR toggling twice is reversible; clearing throws information away.
  • Count state changes rather than bit positions. Kernighan's loop pays for the number of ones, not the width of the word.
  • Check the premise behind a shortcut. A power of two has one set bit and is positive; zero satisfies only the first algebraic test.

All bit manipulation visualizations

Choose a lesson below, change the value or target bit, and step through the trace beside its highlighted Python or JavaScript code.