Python equivalent to Java's BitSet

Viewed 25034

Is there a Python class or module that implements a structure that is similar to the BitSet?

4 Answers

I wouldn't recommend that in production code but for competitive programming, interview preparation and fun, one should make themselves familiar with bit fiddling.

b = 0          # The empty bitset :)
b |= 1 << i    # Set
b & 1 << i     # Test
b &= ~(1 << i) # Reset
b ^= 1 << i    # Flip i
b = ~b         # Flip all
Related