[leetcode]Number Complement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int findComplement(int num) {
        int countDigit = 0;
        int numBak = num;
        while (0 < num) {
            num = num >> 1;
            countDigit++;
        }
        return (int)(pow(2, countDigit) - 1 - numBak);
    }
};
Licensed under CC BY-NC-SA 4.0