Javascript bit shifting


  • 0
    C

    I saw a lot of solutions using built in Integer functions, and didn't see anything for javascript. Idea is the same as the rest, using '^' to get final result.

    /**
     * @param {number} num
     * @return {number}
     */
    var findComplement = function(num) {
        var count = 0;
        var tmp = 0;
        var tmp2 = num;
        while(tmp2 > 0){
            tmp2 = tmp2 >> 1;
            tmp |= 1 << count;
            count++;
        }
        
        return num ^ tmp;
    };

Log in to reply
 

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