def remaining_bytes(char):
    """
    Calculate the remaining bytes of a UTF-8 sequence
    if given the first character.

    UTF-8 ONLY!
    """
    num = ord(char)
    remaining = -1
    while num > 127:
        remaining += 1
        num = (num << 1) % 256
    if remaining == 0 or remaining > 3:
        raise RuntimeError('Not valid UTF-8 leading byte: {}'.format(bin(
            ord(char))))
    assert remaining in (-1, 1, 2, 3)
    return remaining
