from struct import *

def bitflip(x, pos):
    fs = pack('d', x)
bval = list(unpack('BBBBBBBB', fs))
[q, r] = divmod(pos, 8)
bval[q] ^= 1 << r
fs = pack('BBBBBBBB', *bval)
fnew = unpack('d', fs)
return fnew[0]
Python: flip a precise bit in a double