How to convert binary to decimal in JavaScript?
A binary number is a number expressed in the binary numeral system, which uses only two symbols 0 and 1. Decimal numbers are base 10 in a JavaScript number system that uses 10 digits from 0 to 9. We are given a number as Binary input we have to convert it into decimal in JavaScript and print the result.
Binary to Decimal Conversion
Let’s first understand, how to convert binary to decimal with examples.
Input: 1011 (Binary)
Output: 11 (Decimal)
Explanation:
To illustrate, let’s convert the binary number 1011
to decimal:
- Binary:
1011
- Calculation:
- 1×23+0×22+1×21+1×201×23+0×22+1×21+1×20
- 1×8+0×4+1×2+1×11×8+0×4+1×2+1×1
- 8+0+2+1=118+0+2+1=11
- Decimal:
11
Binary to Decimal Conversion Using JavaScript
Below are the approaches to converting binary numbers to decimal numbers using js:
- Iterative Approach
- Using parseInt() Function
- Using Bitwise operation
1. Iterative Approach
Create a function and initialize a variable decimal with value 0 to determine the length of binary string. Iterate through the binary digits from right to left using a for loop, with the loop variable i starting from 0 and ending at n – 1. Convert the character at index n – i – 1 of the binary string to a number using parseInt(). This will give us current binary digit (0 or 1). Add the contribution of this digit to the decimal value.
Example: Demonstration of converting of binary to decimal using iterative approach
function binaryToDecimalIterative(binary) {
let decimal = 0;
const n = binary.length;
// Iterate through the binary digits
for (let i = 0; i < n; i++) {
const digit = parseInt(binary[n - i - 1]);
decimal += digit * Math.pow(2, i);
}
return decimal;
}
const binaryNumber = "10000";
console.log("result is : ",
binaryToDecimalIterative(binaryNumber));
Output
result is : 16
Time Complexity: O(n).
Space Complexity: O(1).
2. Using parseInt() Function
In this approach, there is an inbuilt function parseInt which will convert the binary number into its decimal equivalent. Use the ‘parseInt’ function with base 2 to convert Binary to decimal to convert the binary string to its decimal equivalent.
Example: Demonstration of converting of binary to decimal using the inbuilt parseInt()
function binaryToDecimalParseInt(binary) {
return parseInt(binary, 2);
}
const binaryNumber = "10000";
console.log("result is : ",
binaryToDecimalParseInt(binaryNumber));
Output
result is : 16
Time Complexity: O(n).
Space Complexity: O(1).
3. Using Bitwise operation
Create a function and initialize a variable decimal with value 0. Now determine the length of binary string binary. Now we Iterate through the binary digits of the input string from left to right. If the current digit is ‘1’, perform a left shift operation by (n – i – 1) positions, where n is the length of the binary string, and bitwise OR with the current value of decimal. It return decimal.
Example: Demonstration of converting of binary to decimal using Bitwise operation
function binaryToDecimalBitwise(binary) {
let decimal = 0;
const n = binary.length;
for (let i = 0; i < n; i++) {
if (binary[i] === "1") {
decimal |= 1 << (n - i - 1);
}
}
return decimal;
}
const binaryNumber = "10000";
console.log("Result is : ",
binaryToDecimalBitwise(binaryNumber));
Output
Result is : 16
Time Complexity: O(n)
Space Complexity: O(1)
Contact Us