💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
Intuition\n\n\n\n\n\n Describe your first thoughts on how to solve this problem. \njavascript []\n//JavaScript Code\n/**\n * @param {number} n\n * @return {numb
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/28bf8180-b1d1-4eff-b5b0-1937f8f823fb_1724332944.182035.jpeg)\n![Screenshot 2024-08-22 171839.png](https://assets.leetcode.com/users/images/b2834bfe-87f4-414c-aac8-53254af697ac_1724332952.7461178.png)\n![Screenshot 2024-08-22 171304.png](https://assets.leetcode.com/users/images/5f5609fd-57f3-42e2-a95b-4c9b08ca550e_1724332962.0076113.png)\n![Screenshot 2024-08-22 170924.png](https://assets.leetcode.com/users/images/b9230c95-b86d-4b68-9127-5f953ca67813_1724332970.8389804.png)\n![Screenshot 2024-08-22 170043.png](https://assets.leetcode.com/users/images/dcab31e7-e684-4437-a356-62eccbea5946_1724332989.4488568.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n```javascript []\n//JavaScript Code\n/**\n * @param {number} n\n * @return {number}\n */\nvar bitwiseComplement = function(n) {\n    if(n === 0)\n        return 1\n    let i\n    for(i = 1; i <= n; i *= 2)\n        n = n ^ i\n    return n\n};\n```\n```C++ []\n//C++ Code\nclass Solution {\npublic:\n    int bitwiseComplement(int num) {\n        int m = num;\n        int mask = 0;\n        if(num == 0)\n            return 1;\n        \n        while(m != 0){\n            mask = (mask << 1) | 1;\n            m = m >> 1;\n        }\n        int ans = (~num) & mask;\n        return ans;\n    }\n};\n```\n```Python3 []\n#Python3 Code\nclass Solution:\n    def bitwiseComplement(self, num: int) -> int:\n        if num == 0: return 1\n        mask = 0\n        temp = num\n        while temp:\n            mask = (mask << 1) | 1\n            temp >>= 1\n        return num ^ mask\n```\n```Java []\n//Java Code\nclass Solution {\n    public int bitwiseComplement(int n) {\n        StringBuilder res = new StringBuilder("");\n        String t = Integer.toBinaryString(n);\n        for(char x : t.toCharArray()){\n            if(x == \'1\')\n                res.append(\'0\');\n            else\n                res.append(\'1\');\n        }\n\n        return  Integer.parseInt(res.toString(), 2);\n    }\n}\n```\n```C []\n//C Code\nint bitwiseComplement(int num) {\n    int m = num;\n    int mask = 0;\n    if(num == 0)\n        return 1;\n    \n    while(m != 0){\n        mask = (mask << 1) | 1;\n        m = m >> 1;\n    }\n    int ans = (~num) & mask;\n    return ans;\n}\n```\n```Python []\n#Python Code\nclass Solution(object):\n    def bitwiseComplement(self, num):\n        """\n        :type n: int\n        :rtype: int\n        """\n        if num == 0: return 1\n        mask = 0\n        temp = num\n        while temp:\n            mask = (mask << 1) | 1\n            temp >>= 1\n        return num ^ mask\n```\n```C# []\n//C# Code\npublic class Solution {\n    public int BitwiseComplement(int num) {\n        int m = num;\n        int mask = 0;\n        if(num == 0)\n            return 1;\n        \n        while(m != 0){\n            mask = (mask << 1) | 1;\n            m = m >> 1;\n        }\n        int ans = (~num) & mask;\n        return ans;\n    }\n}\n```\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n# Code\n
['Bit Manipulation', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']