def from_twos_complement(val):
    """
    >>> bin(from_twos_complement(7))
    '0b111'
    >>> from_twos_complement(-7)
    249
    >>> bin(from_twos_complement(-7))
    '0b11111001'
    >>> bin(from_twos_complement(-128))
    '0b10000000'
    """
    if val & 128:
        val = -((val ^ 255) + 1)
    return val
