Bitmasking Subset Generation of Subset

Revision en2, by sar0603, 2020-11-19 14:42:44
//S = bitmask representation of available subset
//{1,2,3} ,S = 3 means only {2,3} are available since binary(3) = 0 1 1
for (int G = S; G != 0 ; G = (G - 1) & S)

This one liner generates subset of a subset in bit representation form. Would be grateful if anyone can give any intuition or explain the above line. Dont want to mug this up.

Eg: ~~~~~ The subset = 1110 The subsets of subset : 1110 1100 1010 1000 0110 0100 0010 ~~~~~

Tags #bitmask, #combination, #combinatorics, #subset

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English sar0603 2020-11-19 14:42:44 113
en1 English sar0603 2020-11-19 14:33:33 414 Initial revision (published)