alexander pantyukhin date november 1 2022 task given a positive int number return true if this number is power of 2 or false otherwise implementation notes use bit manipulation for example if the number is the power of two it s bits representation n 0 100 00 n 1 0 011 11 n n 1 no intersections 0 return true if this number is power of 2 or false otherwise ispoweroftwo0 true ispoweroftwo1 true ispoweroftwo2 true ispoweroftwo4 true ispoweroftwo6 false ispoweroftwo8 true ispoweroftwo17 false ispoweroftwo1 traceback most recent call last valueerror number must not be negative ispoweroftwo1 2 traceback most recent call last typeerror unsupported operand types for float and float test all powers of 2 from 0 to 10 000 allispoweroftwoint2 i for i in range10000 true return true if this number is power of 2 or false otherwise is_power_of_two 0 true is_power_of_two 1 true is_power_of_two 2 true is_power_of_two 4 true is_power_of_two 6 false is_power_of_two 8 true is_power_of_two 17 false is_power_of_two 1 traceback most recent call last valueerror number must not be negative is_power_of_two 1 2 traceback most recent call last typeerror unsupported operand type s for float and float test all powers of 2 from 0 to 10 000 all is_power_of_two int 2 i for i in range 10000 true
def is_power_of_two(number: int) -> bool:
    
    if number < 0:
        raise ValueError("number must not be negative")
    return number & (number - 1) == 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()