Skip to the workspace
Pebellslearn by building
Bits & Machine Numbers  ›  Lesson

Bit Tricks That Earn Their Keep

Bit Tricks 28 minSubtracting 1 and negating change only the lowest bits, so one expression can find, clear or count them

dsa / Bit Tricks That Earn Their Keep

Explore each scene at your own pace.

01 · What x - 1 does to the bits

What x - 1 does to the bits

Bit Tricks That Earn Their Keep

Definition
Bit tricks are short expressions such as x & (x - 1), which clears the lowest set bit, and x & -x, which isolates it, built on the fact that subtracting 1 flips the lowest set bit and all the zeros below it.
In plain words
Take away one from a number and the lowest 1 switch turns off while the zeros beside it turn on. Comparing the number with that result lets you pick out or remove the lowest 1 in a single step, which also makes counting the 1s quick.

In short: subtracting 1 turns a number's trailing zeros into ones and clears its lowest set bit, and that one fact powers a handful of O(1) one-liners: x & (x-1) clears the lowest set bit, x & -x isolates it, and ~x & (x+1) isolates the lowest zero bit instead. Clearing one set bit per pass (Kernighan's loop) counts the 1s in O(popcount) time instead of O(width), which matters once a bitmap is mostly zeros.

Four 8-bit rows for x=40: the bits of x, of ~x, of -x = ~x+1, and of x & -x, with only bit 3 surviving in green

Quarry stores records in fixed-size pages inside one big file, and it has to know which pages are free. Its answer is an allocation bitmap: one bit per page, 1 for used and 0 for free. A file with a million pages needs a bitmap of just 125 KB. But to allocate a page, Quarry has to find a 0 bit quickly, and to report usage it has to count the 1s. A loop over every bit can do both, but it's slow.

This lesson covers the handful of one-line tricks that make those questions cheap. Each works because of how subtracting 1, and negating in two's complement, change the lowest bits of a number. Once you see why, you won't need to memorise any of them.

What x - 1 does to the bits

Subtracting 1 borrows. Starting at bit 0, every trailing 0 becomes a 1 as the borrow passes through it, until the borrow reaches the lowest 1, which becomes 0. Above that, nothing changes:

x      = 0 0 1 0 1 1 0 0     (44)
x - 1  = 0 0 1 0 1 0 1 1     (43)

So x & (x - 1) keeps every bit above the lowest 1 and clears the lowest 1 and everything below it. The net effect is that the lowest set bit is switched off.

What does this print?
print(40 & -40, 40 & 39)
Scene 1 of 8
Project context and further resources
You're building a piece ofQuarry — the record encoder
This piece — claim_page(): Finds the lowest free page in an allocation bitmap and claims it.
Scenario A write needs somewhere to go. Quarry's page allocator looks at the file's allocation bitmap, finds the lowest free page with one bit trick, and marks it used, without scanning page by page.
claim_page.py
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.