[Python, C++] LEGENDARY INNOVATIVE MAVERICK GENIUS SOLUTION; beats 100%, O(1)
We need to XOR the number with a mask having as many 1s as the minimum no. of bits needed to represent the original number.\n\nFor instance:\n\n10 in binary wou
We need to XOR the number with a mask having as many 1s as the minimum no. of bits needed to represent the original number.\n\nFor instance:\n\n10 in binary would be -> 1010\nnotice that the no. of bits is  "log2(10) + 1" \n\nThis holds true in general, for any decimal number x, number of bits needed to represent it in binary would be log2(x) + 1.\n\nNow we know how long our mask needs to be, next up we need to create one with all bits set as 1, how do we do it???\n\nObserve!\n\nfor 10, mask needs to be 1111 (decimal 15) which is one less than 10000 (decimal 16), again, this holds in general.\n\nso we can simply create an integer mask = 2^(p+1) - 1, where p = log2(N) \n\nPython:\n```\nclass Solution:\n    def bitwiseComplement(self, N: int) -> int:\n        if N==0:\n            return 1\n        p = int(log2(N))\n        mask = int(2**(p+1) - 1)\n        return N^mask\n```\n\nC++:\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int N) {\n        if(N ==0 ){\n            return 1;\n        }\n        int p = log2(N);\n        int mask = pow(2,p+1) - 1;\n        return N^mask;\n    }\n};\n```\n\nTime Complexity: O(1)\nSpace Complexity: O(1)\n
['C', 'Python3']