☕️ [C++,Java,Python ] | Easy Solution | MASK+XOR | 100%
C++ | Mask + XOR\nLogic: In order to find the complement of an integer we have to flip all the 0\'s to 1\'sand all the 1\'s to 0\'s. We know that XOR means one 
# C++ | Mask + XOR\n**Logic:** In order to find the complement of an integer we have to flip all the ``0\'s`` to ``1\'s ``and all the ``1\'s`` to ``0\'s``. We know that XOR means one or the other is true but not both. So if we XOR our input with a mask ``11111...``, flipping will happen and we will get our the desired result (complement of that number).\n\n``N ^ bitwiseComplement(N) = 11....11 = Mask``\n``bitwiseComplement(N) = N ^ Mask ``\n\n**Approach:** we will create a mask of all ones until it\'s larger or equal to the number. For example, for ``1010``, the mask will be ``1111``. Once the mask is created, we will XOR it with Number to get the complement.\n```\n 1010\n^1111\n------\n 0101\n------\n```\n\n**We have 2 Method based on ways of creating mask:**\n\n**Method 1:** \n\nCreating mask by using n<<2+1:\n```\n(number) => (how-number-is-derived)=>binary-string\nn = 1 => 1 => 1\nn = 3 => (2*1 + 1) => 11\nn = 7 => (3*2 + 1) => 111\nn = 15 => (7*2 + 1) => 1111\n```\n\n*Complexity*\nTime: O(logn) i.e, O(no of digits in n)\nSpace: O(1)\n\n*Code*\n<iframe src="https://leetcode.com/playground/YqGUiZuv/shared" frameBorder="0" width="400" height="300"></iframe>\n\n**Method 2:**\nCreate mask by bits count:\n```\nfor example, if we take 1010\n(int)(log2(n)+1=> total bits in n =>4\n(int) pow(2, (int)(log2(n)+1) )-1=>  binary representation consist of (no of bits) ones=>1111\n```\n\n*Complexity*\nTime: O(log N) since log function has time complexity of log N. \nSpace: O(1)\n\n*Code:*\n```\nint bitwiseComplement(int n) {\n    return n ? n ^ (int) pow(2, (int)(log2(n)+1) )-1 : 1;\n}\n```\n\nPlease Upvote! if find helpful \nGuys, if you downvote the post make sure to leave a reason so that I can correct those points, suggestion are welcomed :)\nFind me on: https://coderaky.com\n
['Bit Manipulation']