def twos_comp(val, bits):
    """
    Compute the 2's complement of int value val.
    
    :param val: Bytes to complement
    :param bits:
    :return: 2's complement of int value val
    """
    if val & 1 << bits - 1 != 0:
        val = val - (1 << bits)
    return val
