[C++, Python] 3 line with Explanation, Faster than 100%
C++\n\nint findComplement(int num) {\n\tint k = int(log2(num));\n\tint mask = pow(2,k+1) - 1;\n\treturn num^mask;\n}\n\n#### Python3\n\ndef findComplement(self,
#### C++\n```\nint findComplement(int num) {\n\tint k = int(log2(num));\n\tint mask = pow(2,k+1) - 1;\n\treturn num^mask;\n}\n```\n#### Python3\n```\ndef findComplement(self, num: int) -> int:\n\tk = int(log2(num))\n\tmask = int(2**(k+1) - 1)\n\treturn num^mask;\n```\n#### Explanation\nWe want to flip every bit. One way to flip a bit is by taking its xor with 1. Because\n```\n0 xor 1 = 1\n1 xor 1 = 0\n```\n\n\nWith this in mind, all we have to do is take the XOR of all bits with 1.\nLet\'s say our number is 5 ,ie, 101\nso we need to take the xor with 111\nSince\n```\n1   0   1  xor\n1   1   1\n_________\n0   1   0     \n```\n\nHow to generate 111 ?\nAs you can observe, 111 is the number 7, which is 8 - 1, or 2^3 - 1.\nTo get the desired number with all bits as 1,\n\n1) Take the log of that number, say k (Eg: (```log2(5)  = 2```))\n2) Take Power of ```2``` raised to ```k+1``` (2^3=8 or ```1000```) and subtract 1 (7 or ```111```)\n\n\n
['Bit Manipulation', 'C']