= Submask enumeration
A <submask> of a <bit mask> $m$ has no set <bits> outside $m$. Start with $s=m$ and repeatedly replace a positive $s$ by
$$
s\leftarrow(s-1)\mathbin{\&}m.
$$
Process each positive $s$ before this update and stop when it becomes zero. To include the empty <submask>, process zero once and stop before updating it; otherwise the update can return to $m$.
Subtracting one clears the lowest set <bit> and sets the lower positions. The <bitwise AND> with $m$ removes forbidden positions, producing the largest permitted <submask> strictly below the previous one. This proves decreasing order, no duplicates and no skipped <submask>.
For $m=1010_2$, the order is $1010_2,1000_2,0010_2,0000_2$. A <bit mask> with $k$ set <bits> has $2^k$ <submasks>, because each allowed position is independently selected or omitted. Enumeration therefore takes $O(2^k)$ updates under constant-cost <bitwise operations>; $m=0$ has only the empty <submask>.
Back to article page