Review the AI
Power of Two?
A one-line bit trick and two alternatives. One misses an edge case. Label them.Problem
is_power_of_two(n): n ≥ 0 is an integer. Return True if n is a power of two (1, 2, 4, 8, …).Solution A
1def is_power_of_two(n):
2 return n & (n - 1) == 0
Solution B
1def is_power_of_two(n):
2 return n > 0 and n & (n - 1) == 0
Solution C
1def is_power_of_two(n):
2 while n > 1 and n % 2 == 0:
3 n //= 2
4 return n == 1