← DiffPush

Check for the power of 2

Baseline Bit Manipulation · Learn Bit Manipulation O(1) · O(1)

Certifying the Cell Tower's Channel Count

A telecom planner can only deploy tower clusters whose channel counts are exact powers of two, because the switching fabric splits and merges channels in halves. A cluster size qualifies precisely when its binary form carries a single 1 bit. Subtracting one from such a number flips that lone bit and all lower bits, so one AND against the original exposes any impostor.

Input: An integer n.

Output: true when n is a positive power of two, false otherwise.

Constraints

Examples

Example 1

Input: {"n":1}
Output: true
2^0 = 1 carries a single set bit.

Example 2

Input: {"n":16}
Output: true
16 is 10000 — one bit, four zeros, a clean 2^4.

Solve this in your browser →

Also on LeetCode ↗