| Viewed 226 times

How to reverse a Binary String in Javascript?


Write a function that takes an integer n, reverses the binary representation of that integer, and returns the new integer from the reversed binary.

1 Answer
Mohammad Qandeel
Answered
5

Using the parseInt(num, flag) function that receives the text and the type you want to convert to, you can get it

function reversedBinaryInteger(num) {
 return parseInt(num.toString(2).split('').reverse().join(''), 2);
}

//Example
reversedBinaryInteger(13) // 11  => the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11


Related Questions