def twos_comp(val, bits):
    """compute the 2's complement of val.

    :param int val:
        the value to compute the two's complement for

    :param int bits:
        size of val in bits
    """
    if val & 1 << bits - 1 != 0:
        val = val - (1 << bits)
    return val
