Recognize powers of two
Return True exactly when integer n is a positive power of two. Zero and negatives return False.
for n in [0, 8, 12]:
print(n, n > 0 and n & (n-1) == 0)Reason about the state
A positive power of two has only one set bit. Clearing its lowest bit must leave zero. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return True exactly when integer n is a positive power of two. Zero and negatives return False.
The explanation opens here once every check passes.