Read Time:47 Second
JS
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'flippingBits' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER n as parameter.
*/
function flippingBits(n) {
const arrbit=[0];
for(let a=0;a<32;a++){
let mask=1;
let bit= n & (mask<<a);
if(bit==0){
arrbit[a]=1;
}else {
arrbit[a]=0;
}
}
const newBinary=arrbit.reverse().join("");
return parseInt(newBinary,2);
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const q = parseInt(readLine().trim(), 10);
for (let qItr = 0; qItr < q; qItr++) {
const n = parseInt(readLine().trim(), 10);
const result = flippingBits(n);
ws.write(result + '\n');
}
ws.end();
}