C++ : Beats 100% time and 100% space
My approach invloves working from the rightmost bit of the given number.\nIf the rightmost bit is 1, we add 0. Otherwise, we add the power of two correspoding t
My approach invloves working from the rightmost bit of the given number.\nIf the rightmost bit is 1, we add 0. Otherwise, we add the power of two correspoding to the bit, which we keep track here using the variable \'pow\'. After each check we rightshift the number.\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int N) {\n        int sum = 0;\n        long pow=1;\n\t\t// Do, while is used because if N is 0, then it has to add 1 and not go directly out of the loop.\n        do{\n            if(N%2==0) sum+=pow;\n            N = (N>>1);\n            pow*=2;\n        }while(N);\n        return sum;\n    }\n};\n```\n
['C++']