Java Code with Explanation
Code\n\nclass Solution {\n    public int bitwiseComplement(int n) {\n        // edge Case \n        if(n == 0) return 1;\n\n        // ~n or can be said as NOT 
# Code\n```\nclass Solution {\n    public int bitwiseComplement(int n) {\n        // edge Case \n        if(n == 0) return 1;\n\n        // ~n or can be said as NOT of n reverses all the bits of the number\n        // we will write shift until our no. is not equal to zero so that we can find how many bits have our answer\n        // to create mask so that all the 0 bits in left side which got 1 when we NOT them, must be done 0 again \n        // and if we know how many bits have our answer than remaining bits can be AND with 0\n        // Example:- 5 :- (representing in 16 bit) 0000000000000101\n        // mask:- 3 digits, how to calculate:- use right shift until number is zero like\n        // 0000000000000010 >> 1\n        // 0000000000000001 >> 1\n        // 0000000000000000\n\n        // so to make our final answer starting bit as zero we need\n        // mask right most 3 bits to be 1 and remaining zero\n        // 000000000000000 << 1 OR 1\n        // 000000000000001 << 1 OR 1\n        // 000000000000011 << 1 OR 1\n        // 000000000000111, 3 time because we calculated masked digit to be 3 \n        \n        // and in last do AND with mask;\n\n        int m = n;\n        int mask = 0;\n\n        while(m != 0){\n            mask = (mask << 1) | 1; // calculating mask \n            m = m >> 1; \n        }\n        int ans = ~n & mask;\n        return ans;\n    }\n}\n```
['Bit Manipulation', 'Java']