XOR mask method that beats 100% of users
Intuition\nUse XOR to flip the bits.\n\n# Approach\nMake a 111... mask with number n\'s bits length.\nXOR number n with the 111... mask to flip the bits.\n\n# C
# Intuition\nUse XOR to flip the bits.\n\n# Approach\nMake a 111... mask with number n\'s bits length.\nXOR number n with the 111... mask to flip the bits.\n\n# Complexity\n![image.png](https://assets.leetcode.com/users/images/84bfdf1b-3624-4318-8976-2264ba5f5c2a_1717224655.0986028.png)\n\n\n# Code\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int n) {\n        if(n == 0) return 1;\n\n        int flip = 0;\n        int ans = n;\n        while(n){\n            n >>= 1;                // divided by 2 until it\'s zero\n            flip = flip << 1 | 1;   // make a n.length 111... mask\n        }\n        ans ^= flip;    // XOR 111... mask to flip the bits\n        return ans;\n    }\n};\n```
['C++']