import math


def _down_to_power_of_two(n):
    """Returns the power-of-2 closest to n."""
    if n < 2:
        raise ValueError("N should be >= 2: %d" % n)
    log_n = math.log(n, 2)
    p = int(log_n)
    # If n is exactly power of 2 then 2**p would be n, decrease p by 1.
    if p == log_n:
        p -= 1
    return 2**p