C# Solution for Number Complement Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to flip all the bits in the binary representation of an integer, effectivel
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to flip all the bits in the binary representation of an integer, effectively calculating its complement. The approach involves generating a bitmask that has the same number of bits as the input integer but with all bits set to 1. By XORing this mask with the original number, the bits are flipped to achieve the complement.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tMask Construction:\n\t\u2022\tBegin with a mask initialized to 1.\n\t\u2022\tGradually build a mask that has all bits set to 1 for the length of the binary representation of num.\n\t\u2022\tThis is done by repeatedly left-shifting the mask (which effectively multiplies it by 2) and then adding 1 using the OR operation (| 1). This process continues until the mask is greater than or equal to num.\n2.\tXOR Operation:\n\t\u2022\tOnce the mask is constructed, XOR the original num with the mask. The XOR operation will flip each bit in num where the corresponding bit in the mask is 1.\n\t\u2022\tThis results in the complement of num.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tO(1):\n\t\u2022\tThe mask construction loop runs at most 31 times, as the maximum number of bits in the integer (within the constraints) is 31. This is considered constant time because the number of iterations is fixed and does not grow with the input size.\n\t\u2022\tThe XOR operation is also O(1).\n\t\u2022\tThus, the overall time complexity is constant, O(1).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\u2022\tO(1):\n\t\u2022\tThe solution uses a constant amount of additional space, only requiring a few integer variables (mask and num).\n\t\u2022\tThere are no extra data structures or dynamically allocated memory, so the space complexity remains constant.\n\n# Code\n```csharp []\npublic class Solution {\n    public int FindComplement(int num) {\n        // Initialize mask as 1\n        int mask = 1;\n\n        // Generate a mask with all bits set to 1 that covers the length of num\'s binary representation\n        while (mask < num) {\n            mask = (mask << 1) | 1;\n        }\n\n        // XOR num with the mask to get the complement\n        return num ^ mask;\n    }\n}\n```
['C#']