Practice Discrete Math

Algorithms / Subset Enumeration Bitmask

Least You Need to Know: Bitmask Subset Enumeration and Used-Set State

Enumerating masks from `0` to `2^n - 1` gives every subset of an `n`-element set. Interviews use this for subset generation, used-element state, and small-state dynamic programming where each bit records a chosen item.

The least you need to know

Key notation

0 ... 2^n - 1 all subset masks for n items
mask >> i & 1 whether item i is included
full_mask mask with the low n bits all set

Tiny worked example

  • For three items, masks `000` through `111` cover all subsets.
  • Mask `101` means take items 0 and 2 but not 1.
  • A loop over all masks therefore enumerates all subsets.
  • The same idea tracks which jobs or cities have already been used in a state-space search.

Common mistakes

How to recognize this kind of problem

Start practice