문제
우리는 10진수를 받아 2진수로 변환하고 1비트를 0으로, 0을 1로 변환하고 이렇게 형성된 새로운 2진수에 해당하는 10진수를 반환하는 JavaScript 함수를 작성해야 합니다.
예
다음은 코드입니다 -
const num = 45657;
const reverseBitsAndConvert = (num = 1) => {
const binary = num.toString(2);
let newBinary = '';
for(let i = 0; i < binary.length; i++){
const bit = binary[i];
newBinary += bit === '1' ? '0' : 1;
};
const decimal = parseInt(newBinary, 2);
return decimal;
};
console.log(reverseBitsAndConvert(num)); 출력
19878