def twocs_to_int(val, bits):
    """compute the int value of a two compliment int"""
    assert len(bin(val)) - 2 <= bits, (val, bits)  # check if it fits
    if (val & (1 << (bits - 1))) != 0:  # if sign bit is set -> 8bit: 128-255
        val = val - (1 << bits)  # compute negative value
    return val