Easy intuition, approach and code in C++
Intuition\nThe basic logic is that we will make a mask and make it bits 1 , equal to the number of bits in the given number and then take bitwise AND with the c
# Intuition\nThe basic logic is that we will make a mask and make it bits 1 , equal to the number of bits in the given number and then take bitwise AND with the compliment of the given number. \n\n# Approach\n- First of all, we will make a mask variable and initialize it with 0.\n- Then store the given number to another variable arp to count the number of bits in the given number.\n- After counting the number of bits by right shifting, we will take bitwise OR of mask variable with 1.\n- Then, run a loop till (number of digits - 1) and inside it left shift the mask variable by 1 and again take bitwise OR with 1.\n- By doing so we will store 1\'s at the positions of the given number.\n- Then, take compliment of the given number and return the bitwise AND of mask and complimented number.\n\n\n# Complexity\n- Time complexity:\n$$O(number of bits)$$\n\n- Space complexity:\n$$O(1)$$\n# Code\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int n) {\n        int mask=0;\n        int arp=n;\n        int c=0;\n        while (arp) {\n            c++;\n            arp>>=1;\n        }\n        mask=mask|1;\n        for (int i=0;i<c-1;i++) {\n            mask<<=1;\n            mask|=1;\n        }\n        n=~n;\n        return mask&n;\n    }\n};\n\n```
['C++']