Power of Two Check Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Power of Two Check runs in O(1) time and O(1) space, in JavaScript as in every other language.
- ✓There is no power-of-two helper on Number or Math. The relevant primitives are the bitwise operators, specified to convert their operands with ToInt32, and Math.clz32(x), which counts leading zeros of the 32-bit representation, so 31 - Math.clz32(n) is an exact integer log2 for values below 2**32.
- ✓A reviewer insists on the parentheses in (n & (n - 1)) === 0, because === binds tighter than & in JavaScript and dropping them ANDs n with a boolean.
- ✓The JavaScript trap: Bitwise operators are specified to run on signed 32-bit integers, so any operand above 2**31 - 1 is truncated first and the answer becomes a fact about the low 32 bits only.
- ✓Engines keep small integers as tagged SMIs, so n & (n - 1) on a value inside that range stays in registers and costs nothing measurable; a value held as a float64, which includes anything above the SMI range or produced by arithmetic that spilled, forces a float-to-int32 truncation on every bitwise op.
What Power of Two Check actually is
Power of Two Check in JavaScript: what you actually reach for
JavaScript implementation
// Bitwise operators truncate to signed 32-bit, so range-check before using them.
function isPowerOfTwo(n) {
if (!Number.isSafeInteger(n) || n <= 0) return false;
if (n > 0xFFFFFFFF) return isPowerOfTwoBig(BigInt(n));
return (n & (n - 1)) === 0; // parens required: '===' binds tighter than '&'
}
function isPowerOfTwoBig(n) {
return n > 0n && (n & (n - 1n)) === 0n;
}
// Exact integer log2 for values below 2**32, no floats involved.
const log2 = (n) => 31 - Math.clz32(n);
console.log(isPowerOfTwo(1024)); // true
console.log(isPowerOfTwo(4294967297)); // false, via the BigInt branch
console.log((4294967297 & 4294967296) === 0); // true -- the truncation bug
console.log(isPowerOfTwoBig(1n << 200n)); // true
console.log(log2(1024)); // 10
console.log(2147483648 | 0); // -2147483648
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Power of Two Check?
There is no power-of-two helper on Number or Math. The relevant primitives are the bitwise operators, specified to convert their operands with ToInt32, and Math.clz32(x), which counts leading zeros of the 32-bit representation, so 31 - Math.clz32(n) is an exact integer log2 for values below 2**32. Number.isInteger and Number.isSafeInteger do the input validation. Above 32 bits the correct tool is BigInt: n > 0n && (n & (n - 1n)) === 0n, with no width limit and no truncation.
What is the most common JavaScript mistake when implementing Power of Two Check?
Bitwise operators are specified to run on signed 32-bit integers, so any operand above 2**31 - 1 is truncated first and the answer becomes a fact about the low 32 bits only. isPowerOfTwo(4294967297), that is 2**32 + 1, truncates the operands to 1 and 0, ANDs to 0, and returns true. Nothing catches it: the n > 0 guard ran on the untruncated float64. Values at 2**31 also flip sign, since 2147483648 | 0 is -2147483648. Range-check below 2**32 or route the whole test through BigInt.
How does Power of Two Check actually perform in JavaScript?
Engines keep small integers as tagged SMIs, so n & (n - 1) on a value inside that range stays in registers and costs nothing measurable; a value held as a float64, which includes anything above the SMI range or produced by arithmetic that spilled, forces a float-to-int32 truncation on every bitwise op. The BigInt path is a different machine: each operand is a heap-allocated arbitrary-precision object and every & and - allocates a fresh one, typically an order of magnitude slower than the Number path even for single-limb values. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Replit, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.