[Java/Python] Straightforward Bit Manipulation - Clean code - O(logN)
Complexity\n- Time: O(logN)\n- Space: O(1)\n\nPython\npython\nclass Solution(object):\n    def bitwiseComplement(self, N):\n        def setBit(x, k):\n         
**Complexity**\n- Time: `O(logN)`\n- Space: `O(1)`\n\n**Python**\n```python\nclass Solution(object):\n    def bitwiseComplement(self, N):\n        def setBit(x, k):\n            return (1 << k) | x\n        \n        if N == 0: return 1\n        i, ans = 0, 0\n        while N > 0:\n            if N % 2 == 0:\n                ans = setBit(ans, i)\n            i += 1\n            N /= 2\n        return ans\n```\n\n**Java**\n```java\nclass Solution {\n    public int bitwiseComplement(int N) {\n        if (N == 0) return 1;\n        int ans = 0, i = 0;\n        while (N > 0) {\n            if (N % 2 == 0) \n                ans = setBit(ans, i);\n            i += 1;\n            N /= 2;\n        }\n        return ans;\n    }\n    \n    int setBit(int x, int k) {\n        return (1 << k) | x;\n    }\n}\n```\n\nFeel free to ask your question in the comments, help to **vote** if this post is useful to you.
[]