def next_power_of_2(n):
    """ Return next power of 2 greater than or equal to n """
    n -= 1
    shift = 1
    while n + 1 & n:
        n |= n >> shift
        shift *= 2
    return max(4, n + 1)
