JavaScript Power of 2 Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is a Power of 2?

A positive integer is a power of 2 when it equals 2k for some integer k ≥ 0. In binary, that means exactly one bit is set.

Remember
Powers of 2: 1, 2, 4, 8, 16, 32, 64, ...

Binary (one bit set):
   1 → 1
   2 → 10
   4 → 100
   8 → 1000
  16 → 10000

Not: 0, 3, 6, 12, 18, negatives
nBinaryPower of 2?
81000Yes (23)
11Yes (20)
6110No
00No

How to Check a Power of 2 in JavaScript

Four clear steps — then pick the bitwise trick or a divide-by-2 loop.

  1. Require n > 0.
  2. Prefer the interview classic: (n & (n - 1)) === 0.
  3. Or keep dividing by 2 while n is even; end at 1 if it is a power of two.
  4. Reject zero and negatives before either test.
MethodIdeaTimeBest for
Bitwisen > 0 && (n & (n - 1)) === 0O(1)Interviews, production
Divide by 2Halve until odd / reach 1O(log n)Explaining without bits

Pseudocode

Pseudocode
function IsPowerOfTwo(n):
    return n > 0 and (n & (n - 1)) == 0

// Loop alternative:
function IsPowerOfTwoLoop(n):
    if n <= 0: return false
    while n % 2 == 0:
        n = n / 2
    return n == 1

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
One-linern > 0 && (n & (n - 1)) === 0
Why it worksPowers of two have a single set bit
12^0 — yes
0Not a power of two
NegativesNot in this tutorial
Classic checks8 yes; 6 no; 16 yes

Live Preview

Enter an integer to see the bitwise check and the power-of-two verdict.

Whole numbers only. Tap a chip or type a value.

Result
n = 8
n - 1 = 7
n & (n - 1) = 0
8 is a power of 2

JavaScript Programs

Start with the bitwise one-liner — that is the usual interview answer. List matches in a range, then keep the divide loop for explaining without bits. Each example has a Try it Yourself editor.

Example 1 — Bitwise Check

A positive power of two has exactly one set bit, so n & (n - 1) is zero.

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

const number = 8;
if (isPowerOfTwo(number)) {
  console.log(number + " is a power of 2");
} else {
  console.log(number + " is not a power of 2");
}
Try It Yourself

How It Works

  1. n > 0 rejects zero and negatives up front.
  2. n - 1 clears the lowest set bit of n and turns on all bits below it.
  3. For a single-bit number like 8 (1000), that AND becomes 0.
  4. If two or more bits are set (like 6), the AND is nonzero.
  5. Constant-time check — the default answer in interviews.

Example 2 — Powers of 2 from 1 to 20

Reuse the bitwise helper and print every match on one line.

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

console.log("Powers of 2 from 1 to 20:");
let line = "";
for (let i = 1; i <= 20; i++) {
  if (isPowerOfTwo(i)) {
    line += i + " ";
  }
}
console.log(line.trim());
Try It Yourself

How It Works

  1. Outer loop visits every integer from 1 to 20.
  2. Each value is tested with the same isPowerOfTwo helper.
  3. Matches are collected into one space-separated line.
  4. Only 1, 2, 4, 8, 16 appear in this band.
  5. Keeping the helper pure makes range listing easy.

Example 3 — Divide-by-2 Loop

Keep dividing even values by 2; a power of two ends exactly at 1.

JavaScript
function isPowerOfTwo(n) {
  if (n <= 0) {
    return false;
  }

  while (n % 2 === 0) {
    n = Math.floor(n / 2);
  }

  return n === 1;
}

console.log(isPowerOfTwo(1));  // true
console.log(isPowerOfTwo(8));  // true
console.log(isPowerOfTwo(6));  // false
console.log(isPowerOfTwo(0));  // false
Try It Yourself

How It Works

  1. Reject n <= 0 immediately.
  2. While n is even, divide by 2 with Math.floor — peel off factors of two.
  3. When the loop stops, n is odd.
  4. If that odd value is 1, every factor was a 2 — so it was a power of two.
  5. If it is any other odd number (3, 5, …), extra prime factors remain.

Edge Cases & Pitfalls

Check these before calling the solution done.

1

20 counts

1 is a power of two. Do not require n > 1.

0

Not a power of 2

0 & (-1) tricks people — always guard with n > 0.

Negatives

Reject early

Two’s-complement bits are not “powers of two” for this problem.

Even ≠ 2k

6, 12, 18…

Even numbers can still have other factors. Use the full test.

Time and Space Complexity

ProgramTimeExtra space
Bitwise checkO(1)O(1)
Range listingO(R) for range size RO(1)
Divide-by-2 loopO(log n)O(1)

Prefer bitwise in code and interviews. Use the loop when explaining the idea without bit operations.

Key Takeaways

  • Definition: n = 2k for some k ≥ 0 (one set bit).
  • Trick: n > 0 && (n & (n - 1)) === 0.
  • Loop: divide by 2 until odd; success if you land on 1.
  • Test set: 1 → yes; 8 → yes; 6 → no; 0 → no.

One line: positive integers with a single set bit — check with n & (n - 1).

Frequently Asked Questions

A positive integer of the form 2^k for some integer k ≥ 0. Examples: 1, 2, 4, 8, 16, 32.
A power of two has exactly one bit set. Subtracting 1 turns that bit off and sets all lower bits, so the bitwise AND with n is zero.
Yes. 1 = 2^0.
0 and negatives are not powers of two in this tutorial. Always require n > 0 before the bitwise test.
The bitwise check is O(1). Repeatedly dividing by 2 is O(log n).
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.

Next: Power of 3

Check whether a number is a power of three.

Power of 3 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful