How to modify bits in an integer?
These work for integers of any size, even greater than 32 bit:
def set_bit(value, bit): return value | (1<<bit)def clear_bit(value, bit): return value & ~(1<<bit)
If you like things short, you can just use:
>>> val = 0b111>>> val |= (1<<3)>>> '{:b}'.format(val)'1111'>>> val &=~ (1<<1)'1101'
You just need:
def set_bit(v, index, x): """Set the index:th bit of v to 1 if x is truthy, else to 0, and return the new value.""" mask = 1 << index # Compute mask, an integer with just bit 'index' set. v &= ~mask # Clear the bit indicated by the mask (if x is False) if x: v |= mask # If x was True, set the bit indicated by the mask. return v # Return the result, we're done.>>> set_bit(7, 3, 1)15>>> set_bit(set_bit(7, 1, 0), 3, 1)13
Note that bit numbers (index
) are from 0, with 0 being the least significant bit.
Also note that the new value is returned, there's no way to modify an integer "in place" like you show (at least I don't think so).
You can use bitwise opertions.http://wiki.python.org/moin/BitwiseOperators
if you want to set a given bit to 1 you can use bitwise 'or' with 1 on given position:
0b00000111 | 0b00001000 = 0b00001111
to set a given bit to 0 you can use bitwise 'and'
0b00001111 & 0b11111011 = 0b00001011
Note that 0b prefix is for binary numbers and 0x is for hexadecimal.