You don't really need to specify width . Bigtins behave correctly when you do this:
>>> bin(255 & ~(1 << 3)) '0b11110111' >>> bin(65535 & ~(1 << 3)) '0b1111111111110111' >>> bin(75557863725914323419135 & ~(1 << 3)) '0b1111111111111111111111111111111111111111111111111111111111111111111111110111'
This is because negative numbers have an βinfiniteβ line of preceding ones . Therefore, when you complement a positive number (which starts with the string "infinte" of zeros), you get a negative number ( -(x + 1) , to be precise). Just don't trust the bin representation of negative numbers; it does not reflect the actual bits in memory.
So you should rewrite unset_mask like this:
def unset_mask(index): return ~(1 << index) x = 0b11111111 x &= unset_mask(3) print x == 0b11110111
source share