Skip to content

Instantly share code, notes, and snippets.

@spencerbyw
Created June 7, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spencerbyw/b20239c2499c4bd0b89f671bd2c3c493 to your computer and use it in GitHub Desktop.
Save spencerbyw/b20239c2499c4bd0b89f671bd2c3c493 to your computer and use it in GitHub Desktop.
function nIsAPowerOfTwo(num) {
return (Math.log2(num) % 1) === 0;
}
// > nIsAPowerOfTwo(0);
// false
// > nIsAPowerOfTwo(1);
// true
// > nIsAPowerOfTwo(2);
// true
// > nIsAPowerOfTwo(3);
// false
// > nIsAPowerOfTwo(1024);
// true
// > nIsAPowerOfTwo(3.1415926535);
// false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment