Dynamic Programming21 sections · 916 units
Open in Course

Vocabulary - Bitmask

Subset as integer

A bitmask is an integer where each bit represents whether an element is included. With nn elements labeled 00 to n1n-1, bit ii being 11 means element ii is in the subset.

Example with n=4n=4: mask =11= 11 in decimal =1011= 1011 in binary. Bits 00, 11, and 33 are set, so this represents {0,1,3}\{0, 1, 3\}. The empty set is 00. The full set is 2n12^n - 1 (all bits set). This mapping is one-to-one: every subset has exactly one integer in [0,2n)[0, 2^n), and vice versa. This lets us use integers as array indices for DP (dynamic programming).