💯Easy Solution | Mastering Complement of Base 10 | Beats 100% Solution | Bit Shifting 🚀
IntuitionThe bitwise complement of a number n means flipping all its bits (0s become 1s and 1s become 0s). Instead of directly flipping the bits, we iterate thr
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The bitwise complement of a number n means flipping all its bits (0s become 1s and 1s become 0s). Instead of directly flipping the bits, we iterate through each bit of n and construct the complement number step by step.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Handle Edge Case: If n=0, return 1 because the complement of 0 (which is just 0000...0000 in binary) is 1.
2. Iterate Through Bits:
    - Start with ans = 0 and mul = 1 (representing the least significant bit position).
    - If the current bit of n is 0, add mul to ans.
    - Shift n right (n = n >> 1) to process the next bit.
    - Double mul (mul *= 2) to move to the next binary position.
3. Return Result: The final value of ans is the bitwise complement

# Complexity
- Time complexity:
    - The number of bits in n is at most O(logn), so the while loop runs at most O(logn) times

- Space complexity:
    - We use only a few integer variables (ans, mul, and n), so extra space usage is constant.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int bitwiseComplement(int n) {
        if(n==0) return 1;
        int ans = 0;
        int mul = 1;
        while(n){
            if(!(n&1)){
                ans += mul;
            }
            mul*=2;
            n=n>>1;
        }
        return ans;
    }
};
```
['Bit Manipulation', 'C++']