Created
August 9, 2025 16:59
-
-
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.
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
/** | |
* @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