Simple C++ solution


  • 0
    A
    int findComplement(int num) {
       int ans = 0;
       for (int i=0;i<=31-__builtin_clz(num);i++) // Traverse till MSB.
          ans|=(((1<<i)&num)^(1<<i));
       return ans;
     }
    

Log in to reply
 

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