Check it out — Bitwise & Set ops
For a finite bit-set defined as the indices of 1-bits in a non-overlapping area of memory or in a number variable, set and bitwise operations are equivalent.
That is to say the set would be converted-to like so:
s = set() a = 26 i = 0 while (a): if (a & 1): #if the index we're at is 1 s.add(i) i+=1 a>>=1 # >>> s # {1, 3, 4} # >>> bin(26) # 0b11010 # ^^-^--- 4, 3, 1 indices respectively
And that implies:
bitwise -> set AND -> Intersection OR -> Union XOR -> Difference INV -> Compliment
This interesting property is actually an often-used technique for lightweight flags and tight number sets to save lots of space.
For example, you can encode your commandline program flags as:
unsigned flag_a = 1<<0 unsigned flag_c = 1<<1 ... unsigned flag_x = 1<<7 /* or even a tighter-packed struct with bitfields */
And then implement all kinds of interesting set logic to distinguish good flag combinations from bad ones and implement special logic for the existemce of certain flags together i.e.
case flag_a | flag_x: /* Union! */
or for mutual exclusion or any-of kinds of flags etc..
Hope this was useful!











