Gamified challenge · Bit tricks
A gate opens for n only if n is a power of two: 1, 2, 4, 8, …, i.e. n = 2^k for some k ≥ 0. n is any 32-bit signed integer, so it can be 0 or negative (those never open the gate).
Write isPower(n) (Python: is_power(n)) that returns true if the gate opens and false otherwise. 16 → true, 6 → false, 1 → true (it's 2^0).
A power of two has exactly one 1-bit. Can you test that without a loop?
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google