The idea is that, given a subset (x) of a mask (M) the next largest lexicographic subset (x') is:
x’ = (x – M) & M
Combined with the C# "yield return" statement, this allows a simple enumeration of the power set of a mask. You simply seed the subset with zero, and iterate until it is once again zero.
public static IEnumerable<int> PowerSet (
int xSet = 0;
yield return xSet;
xSet = (xSet - maskIn) & maskIn;
For example, calling PowerSet(0x19) returns an enumerator which yields (in binary):
I can’t think of a specific use for it right off hand, but if a project uses enough bit manipulation, I’m sure something would arise.