Best Solution
Approach\nUsing Bitwise Operator\n\n# Complexity\n- Time complexity:\nO(logn)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n    int bitwi
# Approach\nUsing Bitwise Operator\n\n# Complexity\n- Time complexity:\n$$O(logn)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int n) {\n        int num = n;\n        int mask = 0;\n        if (n == 0)\n            return 1;\n        while (num != 0) {\n            mask = (mask << 1) | 1;\n            num = num >> 1;    \n        }\n        int ans = (~n) & mask;\n        return ans;\n    }\n};\n```
['C++']