def patchCode(code, value, bits):
    """ patch the last bits of <code> to <value> """
    assert(type(code) == type(value) == type(bits) == int)
    mask = 2**bits-1
    return (code & ~mask) | (value & mask)