javascript solution with bit shift


  • 0
    R
    var isPowerOfTwo = function (n) {
        if (n < 1) return false;
        let tmp = n;
        let power = 1;
        while (tmp !== 0) {
            tmp >>= 1;
            power <<= 1;
            power >>>= 0;
        }
        power >>>= 1;
        return (n ^ power) === 0;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.