Power of Two Guard
HardAcc. 35.4%
+65 XP 30
Binary Alignment
Powers of two (1, 2, 4, 8, 16...) have a unique property in binary: they only have a single 1 bit. Subtracting 1 from a power of two flips all bits.
For example: 8 is 1000, and 7 is 0111. Performing 8 & 7 results in 0000.
The Assignment
Your function receives a positive integer n.
- Return
trueifnis a power of two. - Return
falseotherwise. - Requirement: Solve this in one line using bitwise operators (
&) and without loops.
01EXAMPLE 1
Input
n = 16Output
trueExplanation: 16 is 2 to the power of 4.
Constraints
- Ignore inputs <= 0.
- Use bitwise & arithmetic.
BitwiseMathProfessional
JavaScriptSystem handles I/O — write your function only
Loading...
4 Hidden
Input Arguments
n16
Expected Output
true
Click RUN to test your solution