Finding the bitwise complement
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the bitwise complement of a given integer. One way to appr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the bitwise complement of a given integer. One way to approach this is to iterate through each bit of the integer, flip it, and construct the complement.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nI\'ll iterate through each bit of the integer using a while loop. Within each iteration, I\'ll extract the least significant bit using a bitwise AND operation. Then, I\'ll flip the bit using a bitwise XOR operation with 1. After that, I\'ll construct the complement integer by adding the flipped bit to the answer, which is calculated as 2 raised to the power of the position of the bit. Finally, I\'ll increment the position counter.\n\n# Complexity\n- Time complexity: O(log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int num) {\n\n        if(num == 0) \n        {\n            return 1;\n        }\n        else\n        {\n            int i = 0, ans = 0;\n            while(num!=0)\n            {\n                int bit = num&1;\n                num = num>>1;\n                int complement_bit = bit ^ 1;\n                if(complement_bit == 1)\n                {\n                    ans += pow(2,i) ;\n                }\n                i++;\n            }\n            return ans;\n        }\n    }\n};\n```
['C++']