BITWISE OPERATORS SOLUTIONS
PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT\n# CONNECT WITH ME\n### https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n#### https://www.instagram.com/pratay_nand
# **PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT**\n# **CONNECT WITH ME**\n### **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n#### **[https://www.instagram.com/pratay_nandy/]()**\n\n# Approach\nHere\'s a step-by-step explanation of the approach used in this code:\n\n1. Initialize `m` to the value of `n`. `m` will be used to calculate the length of the binary representation of `n`.\n\n2. Check if `m` is equal to 0. If `n` is 0, its bitwise complement is 1. In this case, the function directly returns 1.\n\n3. Initialize a variable `mask` to 0. The `mask` variable is used to create a bit mask of 1s with the same length as the binary representation of `n`.\n\n4. Use a `while` loop to calculate the `mask`:\n   - In each iteration, `mask` is left-shifted by 1 bit and then bitwise ORed with 1. This effectively adds a \'1\' to the `mask`.\n   - The loop continues until `m` becomes 0. This loop calculates a bit mask with all 1s, and the length of the mask is equal to the number of bits required to represent `n`.\n\n5. Calculate the bitwise complement of `n`:\n   - `(~n)` computes the bitwise negation of `n`, changing all 0s to 1s and all 1s to 0s.\n   - `& mask` performs a bitwise AND operation between the negated value and the mask. This operation sets all bits in `n` to 0 except for the least significant bits that are part of the binary representation of `n`.\n\n6. Return the result, which is the bitwise complement of the input integer `n`.\n\nThis code successfully calculates the bitwise complement of an integer `n` by creating a mask with the same length as the binary representation of `n` and then applying the negation and bitwise AND operations.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:**0(N)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:**0(1)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int n) {\n        int m = n;\n        if(m==0)return 1;\n        int mask = 0 ;\n        while(m!=0)\n        {\n            mask = (mask << 1) | 1;\n            m = m >> 1;\n        }\n        int ans = (~n) & mask; \n        return ans;\n    }\n};\n```
['Bit Manipulation', 'C', 'C++']