Bit manipulation in C


  • -1
    int findComplement(int num) {
        int comp = 0;
        int shift = 0;
        while(num){
            int bit = num%2;
            num >>= 1;
            comp += (bit^1)<<shift++;
        }
        
        return comp;
    }
    

Log in to reply
 

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