Java, O(N) solution, easy to understand with detailed explanation and comments
idea is simple, check each bit from right to leftmost bit 1, if bit is 1, continue, if bit is 0, set result's corresponding bit to 1\n```\npublic class Solution
idea is simple, check each bit from right to leftmost bit 1, if bit is 1, continue, if bit is 0, set result's corresponding bit to 1\n```\npublic class Solution {\n    public int findComplement(int num) {\n        int result = 0;\n        //use set to set the value of result in each bit\n        int set = 1;\n        while(num != 0) {\n            //if last bit is 0 , set corresponding position of result to 1\n            if((num&1)== 0) {\n                result |= set;\n            }\n            //if last bit of num is 1, then do not need to set result\n            set <<= 1; // left shift 1 bit\n            num >>= 1;//right shift num, move right 1 bit\n        }\n        return result;\n    }\n}
['Bit Manipulation', 'Java']