- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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
- Related Articles
- Decimal count of a Number in JavaScript
- Swapping adjacent binary bits of a decimal to yield another decimal using JavaScript
- Retrieving the decimal part only of a number in JavaScript
- What are the steps to divide a decimal number by another decimal number?
- Decimal Number System
- How can I remove the decimal part of JavaScript number?
- How to get a decimal portion of a number with JavaScript?
- 10’s Complement of a decimal number?
- Parse and format a number to decimal in Java
- Returning number of digits in factorial of a number in JavaScript
- How to convert a decimal number to roman using JavaScript?
- Convert octal number to decimal number in Java
- Returning the expanded form of a number in JavaScript
- The decimal expansion of the rational number will $frac{14587}{1250}$ terminate after:(A) one decimal place(B) two decimal places(C) three decimal places(D) four decimal places
- Count number of digits after decimal on dividing a number in C++

Advertisements