JavaScript solutions, bits and strings
The bit version:\n\nvar findComplement = function(num) {\n    let mask = 1;\n    while (mask < num) mask = (mask << 1) | 1;\n    return num ^ mask;\n};\n\nThe s
The bit version:\n```\nvar findComplement = function(num) {\n    let mask = 1;\n    while (mask < num) mask = (mask << 1) | 1;\n    return num ^ mask;\n};\n```\nThe string version:\n```\nvar findComplement = function(num) {\n    return parseInt(num.toString(2).split('').map(d => +!+d).join(''), 2);\n};\n```\nA combination of bits and strings that is fast:\n```\nvar findComplement = function(num) {\n    return parseInt((~num ^ 1 << 31).toString(2).substr(-num.toString(2).length), 2);\n};\n```\nIn the last solution we negate the bits, including leading zeros, and get rid of the sign bit. Then we take the right side that we need which has the same number of digits as `num`.
['JavaScript']