[Python3] Easy One line Code -with Explanation
Here\'s the code:\npython\nclass Solution:\n    def findComplement(self, num: int) -> int:\n        return num^(2**(len(bin(num)[2:]))-1)\n\nWhat does it mean?\
Here\'s the code:\n```python\nclass Solution:\n    def findComplement(self, num: int) -> int:\n        return num^(2**(len(bin(num)[2:]))-1)\n```\nWhat does it mean?\nThe complement of a number is computable by "flipping" all the bits in his binary form.\nIn other words, that\'s nothing more than a **bitwise XOR with all 1**.\n\nFollowing the truth table:\n\n![image](https://assets.leetcode.com/users/xhon9/image_1588579305.png)\n\nWe can easily notice what I said before.\n\nSo, now?\nNow the idea is to **transform in binary** form the input number, taking the number of bit necessary for his representation. That\'s done with `len(bin(num)[2:])`, [2:] cause the bin() function in Python3 add also an initial \'0b\' (ex: `bin(5) = \'0b101\'`, but `bin(5)[2:] = \'101\'`).\n\nAfter taking the len, **compute** `2**len` taking the first pow of 2 that is >= our number, and finally compute the `-1`.\nReasoning: doing `2**len` we get a number with leading 1 and all zeros, subtracting 1 we finally get the number with the same len of our input, formed by all 1.\n\nReturn the XOR of input and this number, and **win**!\n\n-That\'s my first post, feel free to ask and upvote :D
['Python3']