simple C++ solution


  • 0

    get the highest effect 1 in num.

    int findComplement(int num) 
    {
        int hpos = 31;
        while ((hpos >= 0) && !((1 << hpos) & num))
            hpos--;
        
        return (1 << (hpos + 1)) - 1 - num;
    }

Log in to reply
 

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