Enconding Sets as Booleans
We can use booleans to encode sets. Consider this:
The 1 toggles whether a number is present or not on the set.
Boolean Operations
One nit trick of using the above representation is that to perform union, intersection, and complement we can use boolean operators.
Intersection
110
= {1,2}
010
= {1}
110 & 010
= 010
= {1}
Union
110
= {1,2}
101
= {0, 2}
110 | 101
= 111
= {0, 1, 2}
Complement
110
= {1, 2}
~110
= 001
= {0}