I have an integer with a value 7 (0b00000111) And I would like to replace it with a function to 13 (0b00001101). What is
7
0b00000111
13
0b00001101
These work for integers of any size, even greater than 32 bit:
def set_bit(value, bit): return value | (1<
If you like things short, you can just use:
>>> val = 0b111 >>> val |= (1<<3) >>> '{:b}'.format(val) '1111' >>> val &=~ (1<<1) '1101'