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

Subsets as Bitmasks

Subsets as Bitmasks 28 minCounting from 0 to 2ⁿ − 1 visits every subset, one machine word each

dsa / Subsets as Bitmasks

Explore each scene at your own pace.

01 · A subset is a number

A subset is a number

Subsets as Bitmasks

Definition
Subset enumeration by bitmask gives each of n items a bit position, so every integer from 0 to 2ⁿ − 1 represents a different subset, and set operations become bitwise operations on those integers.
In plain words
Imagine a row of light switches, one per item. Each pattern of on and off is one possible selection, and counting upward in binary steps through every selection exactly once. Combining two selections means combining their switches.

In short: give each of n items a bit position, and every number from 0 to 2ⁿ−1 is a different subset — bit i is 1 when item i is in it. Counting upward visits every subset exactly once in O(n · 2ⁿ) time, and set algebra becomes bit operations: union is |, intersection is &, difference is a & ~b, and membership is a >> i & 1. It's O(1) extra space per mask, since a whole subset lives in one machine word, so this is a tool for small n, not for n = 40.

Eight rows, mask 0 through mask 7 in three bit columns labelled epoch/shard/id, each row's binary digits matching the subset it names

Quarry's query planner has a handful of candidate indexes it could keep in memory: by id, by shard, by epoch, by type. Each costs memory and each speeds up some queries. Which combination should it keep? With a few candidates, the honest answer is to try every combination and keep the best. That's 2ⁿ subsets for n candidates, and this lesson shows the cheapest way to walk them: count.

In the algorithm-design section you generated subsets with backtracking, a recursive tree of "take it or leave it" choices. Bitmasks flatten that whole tree into a single for loop over integers. There's no recursion and no call stack, and each subset is stored in one machine word.

A subset is a number

Give each of the n items a bit position. A subset is then an n-bit number whose bit i is 1 when item i is in it. Every n-bit number from 0 to 2ⁿ − 1 is a different subset, and every subset is one of those numbers. Counting upward visits them all:

watch it move · array

Counting from 0 to 7 in three bits visits every subset of three indexes exactly once.

mask 0 = 000 selects nothing. That's the empty set, keeping no indexes at all. 3 cells. At index 2.

1 / 8

In code, turning a mask back into items is one comprehension:

python
What does this print?
mask = 0b1101
print([i for i in range(4) if mask >> i & 1])

The cost is the number of masks times the work per mask, O(n · 2ⁿ) to list every subset. That's fine for n = 20, which is about 20 million steps, and impossible for n = 40. Bitmask enumeration is a tool for small n. When n is small, it's often the simplest correct solution there is.

The classic slip is precedence again. This planner should see all 8 subsets:

broken — fix it

It prints 4 ['id', 'shard']. Subtraction binds tighter than <<, so the range stops at 1 << 2 and half the subsets never appear.

Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the record encoder
This piece — pick_indexes(): Chooses which indexes to keep in memory by trying every subset.
Scenario Quarry has room in memory for only some of its candidate indexes. Each time the workload shifts, the index picker tries every combination that fits and keeps the one that saves the most query time.
pick_indexes.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
pick_indexes(sizes, gains, budget) → listIndices of the best subset of indexes that fits in memory.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.