Leetcode 476 Solution

This article provides solution to leetcode question 476 (number-complement)

https://leetcode.com/problems/number-complement

Solution

class Solution {
public:
    int findComplement(int num) {
        int a = 0;
        while (a < num)
            a = 2 * a + 1;
        return a ^ num;
    }
};