🌟 Efficient Solution for "Number Complement" | C++ | 0ms Beats 100.00% | O(log n)
\n---\n# Intuition\n\nThe problem requires finding the complement of a given integer. The complement is obtained by flipping all bits in the binary representati
##\n---\n# Intuition\n\nThe problem requires finding the complement of a given integer. The complement is obtained by flipping all bits in the binary representation of the number.\n\n# **Approach**\n\nTo find the complement of an integer **num**, first determine the number of bits needed to represent **num** in binary. Create a bitmask with all bits set to 1, matching this length. Apply a bitwise NOT operation to **num**, and then use bitwise AND with the bitmask to obtain the complement. This method efficiently computes the result by leveraging bitwise operations.\n\n# Complexity\n- **Time Complexity** $$O(\\log n)$$ The operations are based on the number of bits in the integer.\n- **Space Complexity** $$O(1)$$ Only a few additional variables are used.\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int findComplement(int num) {\n        int number = num,bitmask = pow(2, ceil((double)log2(number))) - 1;\n        return (~number) & bitmask;\n    }\n};\n\n```\n---\n## **If you found this solution helpful, please upvote! \uD83D\uDE0A**\n---\n
['C++']