Last active
April 29, 2026 21:20
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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