def twocs_to_int(val, bits):
    """compute the int value of a two compliment int"""
    assert len(bin(val)) - 2 <= bits, (val, bits)
    if val & 1 << bits - 1 != 0:
        val = val - (1 << bits)
    return val
