Power of Two

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Power of Two

Approach

A power of two in binary has exactly one set bit. Check that n is positive, then verify n & (n - 1) equals zero. This expression clears the single set bit, leaving zero only for powers of two.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Bit Manipulation

Why It Works

Powers of two are the only positive integers with a single 1-bit, so n & (n - 1) which clears the lowest set bit will produce zero exclusively for them.

Updated Feb 2026