Python 4 ways
Flip bit by bit.\n\nclass Solution(object):\n    def findComplement(self, num):\n        i = 1\n        while num >= i:\n            num ^= i\n            i <<=
1. Flip bit by bit.\n```\nclass Solution(object):\n    def findComplement(self, num):\n        i = 1\n        while num >= i:\n            num ^= i\n            i <<= 1\n        return num\n```\n2. Find the bit length (say L) and flip num by **num ^ 11...1**   (L ones).\n```\n    def findComplement(self, num):\n        return num ^ ((1<<num.bit_length())-1)\n```\n3. Again find the bit length first.\n```\n    def findComplement(self, num):\n        return num ^ ((1 << len(bin(num)) - 2) - 1)\n```\n4.\n```\ndef findComplement(self, num):\n        return num ^ ((2<<int(math.log(num, 2)))-1)\n```\nWe can also flip num first (including the leading zeros) using ```~num``` and then get the last L bits by ```& 11...1``` (L ones).\n\nFor example,\n```\n    def findComplement(self, num):\n        return ~num & ((1<<num.bit_length())-1)\n```
[]