easy to understand bitwise approach with explanation || c++ || JS || python || python3
Intuition\nThe complement of a number in binary is obtained by flipping all bits (i.e., converting 0s to 1s and 1s to 0s). To efficiently compute the complement
# Intuition\nThe complement of a number in binary is obtained by flipping all bits (i.e., converting `0`s to `1`s and `1`s to `0`s). To efficiently compute the complement of a number, we need to focus only on the bits that matter\u2014those that correspond to the binary representation of the number.\n\n# Approach\n1. **Identify the Most Significant Bit (MSB):**\n    - First, identify the position of the `most significant bit (MSB)` that is set to `1` in the given number. This is the highest bit position where `1` appears in the binary representation.\n2. **Flip the Bits:**\n    - To get the complement, we need to flip all the bits from the `least significant bit (LSB)` up to the `MSB`. We can achieve this by using `XOR (^)` operation with a number that has all bits set to `1` up to the position of the `MSB`.\n3. Return the Result:\n    - The result after flipping the relevant bits will be the complement of the original number.\n\n\n\n\n# Complexity\n- Time complexity: $$O(1)$$ since the algorithm only involves a few bitwise operations that run in constant time.\n\n- Space complexity: $$O(1)$$  because no extra space proportional to the input size is required.\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int findComplement(int num) \n    {\n        int ind = 0;  // Variable to store the position of the most significant bit (MSB)\n\n        // Loop to find the MSB where \'1\' is present\n        for (int i = 0; i < sizeof(int) * 8; ++i)\n        {\n            // Check if the bit at position \'i\' is set to 1\n            if (num & (1 << i))\n            {\n                ind = i;  // Update \'ind\' to the current position of the set bit\n            }\n        }\n\n        // Loop to flip all bits from the least significant bit (LSB) up to the MSB\n        for (int i = 0; i <= ind; ++i)\n        {\n            // Flip the bit at position \'i\' using XOR\n            num = num ^ (1 << i);\n        }\n\n        return num;  // Return the complement of the original number\n    }\n};\n```\n```javascript []\n/**\n * @param {number} num\n * @return {number}\n */\nvar findComplement = function(num) {\n    let ind = 0;\n\n    // Find the highest bit that is set\n    for (let i = 0; i < 32; i++) {\n        if (num & (1 << i)) {\n            ind = i;\n        }\n    }\n\n    // Flip all bits up to the highest bit set\n    for (let i = 0; i <= ind; i++) {\n        num = num ^ (1 << i);\n    }\n\n    return num;\n};\n```\n```python3 []\nclass Solution:\n    def findComplement(self, num: int) -> int:\n        ind: int = 0\n\n        # Find the highest bit that is set\n        for i in range(32):\n            if num & (1 << i):\n                ind = i\n\n        # Flip all bits up to the highest bit set\n        for i in range(ind + 1):\n            num ^= (1 << i)\n\n        return num\n        \n```\n\n\n\n\n
['Python', 'C++', 'Python3', 'JavaScript']