Typorithm · easy

Power of Two

▷ Interactive algorithm labpower-of-twobit-manipulationsingle-bitand-maskpositive-check
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Power of Two

0%

Bit Manipulation

A power of two has exactly one set bit, so n > 0 and n & (n-1) === 0 identifies it.

Powers of two are 1, 2, 4, 8... each with a single 1 bit in binary.

Clearing the lowest set bit of such a number leaves zero, since there is only one bit.

Guarding with n > 0 rules out zero and negatives.

Preparing algorithm lab…
01

Exactly one set bit.

02

n & (n-1) === 0 clears it to zero.

03

Require n > 0.

function isPowerOfTwo(n) {
  return n > 0 && (n & (n - 1)) === 0;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Must be positive

Zero and negatives are never powers of two.

n > 0

2. Single bit test

A power of two has exactly one set bit, so clearing it yields zero.

(n & (n - 1)) === 0

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Power of Two: Visual Walkthrough & Practice · Typorithm | ApplyDjinn