Simple and Easy C++ Code 💯☠️
\n\n# Code\ncpp []\nclass Solution {\npublic:\n    int findComplement(int num) {\n        bitset<32> binary(num);\n        string ans = binary.to_string();\n   
\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int findComplement(int num) {\n        bitset<32> binary(num);\n        string ans = binary.to_string();\n        string res = "";\n        bool f1 = false;\n        for (int i = 0; i < ans.size(); ++i) {\n            if (ans[i] == \'1\') {\n                f1 = true;\n            }\n            if (f1) {\n                if (ans[i] == \'0\') {\n                    res += \'1\';\n                } else {\n                    res += \'0\';\n                }\n            }\n        }\n        return stoi(res, NULL, 2);\n    }\n};\n```\n# Problem Understanding\nThe goal is to find the bitwise complement of a given integer num. The bitwise complement flips all the bits of the binary representation of num. For example:\n\nFor num = 5, which in binary is 00000000000000000000000000000101, the bitwise complement is 11111111111111111111111111111010, which in decimal is 2.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**Binary Representation:**\n\nFirst, convert the given integer num to its binary representation. This can be efficiently done using bitset<32>, which represents the number as a 32-bit binary string.\n\n**String Manipulation:**\n\nConvert this binary representation to a string using binary.to_string(). This string will be 32 characters long, with leading zeros.\n\n**Finding the Leading Bit:**\n\nIterate through the string to find the first occurrence of 1 (f1 flag is used to indicate this). All the leading zeros before the first 1 are irrelevant for the complement calculation.\n\n**Flipping the Bits:**\n\nStart from the first 1 found and flip the bits: convert 0 to 1 and 1 to 0.\n\n**Forming the Result:**\n\nConstruct the resulting string after flipping the relevant bits.\n\n**Convert Back to Integer:**\n\nConvert the resulting binary string back to an integer using stoi(res, NULL, 2) where res is the string of flipped bits.\n\n# Complexity\n- Time complexity: O(1)\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)$$ -->
['C++']