I am facing an issue while trying to reverse a 32-bit unsigned integer by converting it to a string first. The toString(2)
function is causing the zeros at the end to be omitted, leading to incorrect output.
This is my code:
var reverseBits = function(n) {
let reverserN=(n>>>0).toString(2).split('').reverse().join('');
console.log(reverserN)
return parseInt(reverserN,2)
};
Here's the current output:
Your input
00000010100101000001111010011100
stdout
00111001011110000010100101
//End zeros are missing
Output
15065253 (00000000111001011110000010100101)
Expected
964176192 (00111001011110000010100101000000)
Additionally, attempting to use BigInt
results in the character 'n' being added to the end of the reversed bits as shown here: 00111001011110000010100101n
.
Why are the zeros being omitted? And how can I prevent this from happening?