def exact_log2(num):
    """Find and return an integer i >= 0 such that num == 2**i.

    If no such integer exists, this function raises ValueError.
    """
    if not isinstance(num, int):
        raise TypeError('unsupported operand type: %r' % (type(num).__name__,))
    n = int(num)
    if n <= 0:
        raise ValueError('cannot compute logarithm of non-positive number')
    i = 0
    while n != 0:
        if n & 1 and n != 1:
            raise ValueError('No solution could be found')
        i += 1
        n >>= 1
    i -= 1
    assert num == 1 << i
    return i
