def fls(val: int, v6: bool) ->int:
    """Find last set - returns the index, counting from 0 (from the right) of the
    most significant set bit in `val`."""
    if val == 0:
        return 0
    max_power_of_2 = 7 if v6 else 5
    n = val | val >> 1
    for i in range(1, max_power_of_2 + 1):
        n |= n >> 2 ** i
    n += 1
    n >>= 1
    pos = 0
    while n & 1 == 0:
        n >>= 1
        pos += 1
    return pos
