var findComplement = function (num) {
const binary = (num >>> 0).toString(2);
let complement = '';
for (let i = 0; i < binary.length; i++) {
if (binary[i] === '0') complement += '1';
else complement += '0';
}
return parseInt(complement, 2);
};
JavaScript solution ~ have a better way?




@chengsieuly Give a simple example:
1001 + 0110 = 1111 = (2^4)  1.
So, you will find that num + result = 2^(num.toString(2).length)  1.
Did I make myself clear?

Find out my solution in javascript
https://discuss.leetcode.com/topic/84346/javascript3linessolution
I use bit operator to achieve this