Python equivalent to Java's BitSet

前端 未结 4 2185
眼角桃花
眼角桃花 2020-12-16 10:16

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

4条回答
  •  情书的邮戳
    2020-12-16 11:21

    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
    

提交回复
热议问题