We are required to write a JavaScript program that reverses the order of the bits in a given integer.
For example −
56 -> 111000 after reverse 7 -> 111
Another example,
234 -> 11101010 after reverse 87 -> 1010111
Example
const num1 = 789;
const num = 43
const reverseBits = (num = 1) => {
const str = num.toString(2);
const arr = str.split('').reverse();
const arrStr = arr.join('');
const reversedNum = parseInt(arrStr, 2);
return reversedNum;
}
console.log(reverseBits(num));
console.log(reverseBits(num1));Output
And the output in the console will be −
53 675