C++ 3 line bit manipulation


  • 0
    M
    int findComplement(int num) {   
        int ans = 0x80000000;
        while(!(num & n)) n >>= 1;
        return ((~n) << 1) + 1 - num;
    }
    

    For example,
    num = 00000101
    n = 11111100
    ~n = 00000100


Log in to reply
 

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