Skip to content

numerical

is_power_of_two(number)

Check if a number is a power of two.

Parameters:

Name Type Description Default
number int

The candidate

required

Returns:

Type Description
bool

Whether or not it is a power of two

Source code in staff/numerical.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def is_power_of_two(number: int) -> bool:
    """Check if a number is a power of two.

    Args:
        number: The candidate

    Returns:
        Whether or not it is a power of two
    """
    if not isinstance(number, int):
        return False
    return (number & (number - 1) == 0) and number != 0