Skip to content

Instantly share code, notes, and snippets.

@qntm
Last active April 29, 2026 21:20
Show Gist options
  • Select an option

  • Save qntm/15e57796af751676afff6767a22fcf2a to your computer and use it in GitHub Desktop.

Select an option

Save qntm/15e57796af751676afff6767a22fcf2a to your computer and use it in GitHub Desktop.
Determine whether a JavaScript value is a power of two (non-broken edition)
// Exports a function which determines whether a JavaScript value is a power of
// two or not. Unlike the npm package `is-power-of-two`, this actually returns
// the correct answer in all cases.
const powersOfTwo = new Set()
for (let i = Number.MIN_VALUE; i <= Number.MAX_VALUE; i *= 2) {
powersOfTwo.add(i)
}
export default x => powersOfTwo.has(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment