EasyPythonJavaScriptTypeScript
Bits — Power of Two
AlgorithmsBit ManipulationMath
solve(n) — n is a 32-bit signed integer (-2^31 <= n < 2^31). Return true whenn is a power of two (1, 2, 4, 8, …). Zero and negative numbers are not. No loops, no
division, no logarithm: a single expression is enough.
A power of two has exactly one set bit. n & (n - 1) clears the lowest set bit, so it
is 0 only when there was a single one.
Examples
solve(1) → true
solve(16) → true
solve(3) → false
solve(0) → false
solve(-8) → falseSample tests
Test #12^0
Input: [1]
Output: true
Test #22^4
Input: [16]
Output: true
Test #3Not a power
Input: [3]
Output: false