Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created August 9, 2025 16:59
Show Gist options
  • Save tatsuyax25/dc33ee898d7ba35d2d72b10d1dc34b9a to your computer and use it in GitHub Desktop.
Save tatsuyax25/dc33ee898d7ba35d2d72b10d1dc34b9a to your computer and use it in GitHub Desktop.
Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2x.
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfTwo = function(n) {
// Powers of two must be positive integers greater than zero
if (n < 1) {
return false;
}
// Use bitwise AND to check if n has only one bit set
return (n & (n -1)) === 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment