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.

  1. Return true if n is a power of two.
  2. Return false otherwise.
  3. Requirement: Solve this in one line using bitwise operators (&) and without loops.

01EXAMPLE 1

Inputn = 16
Outputtrue

Explanation: 16 is 2 to the power of 4.

Constraints

  • Ignore inputs <= 0.
  • Use bitwise & arithmetic.
BitwiseMathProfessional
JavaScript
Loading...
4 Hidden

Input Arguments

n16

Expected Output

true

Click RUN to test your solution