- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Reverse the order of the bits in a given integer
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
Advertisements