 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reversing the bits of a decimal number and returning new decimal number in JavaScript
Problem
We are required to write a JavaScript function that takes in a decimal number, converts it into binary and reverses its 1 bit to 0 and 0 to 1 and returns the decimal equivalent of new binary thus formed.
Example
Following is the code −
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)); Output
19878
Advertisements
 