Converting binary to decimal is a common task in computer programming. Binary is a base-2
number system that uses only two digits, 0 and 1. Decimal, on the other hand, is a base-10
number system that uses ten digits, 0 through 9. Converting binary to decimal involves multiplying each digit of the binary number by its corresponding power of 2 and adding up the results.
Conversion
Here's a step-by-step guide on how to convert binary to decimal:
- Choose the binary number you want to convert to decimal.
- Write down the powers of 2 from right to left, starting with .
- Multiply each digit of the binary number by its corresponding power of 2.
- Add up the results from step 3 to get the decimal value.
Example
Let's walk through an example to illustrate the process. Suppose we want to convert the binary number 1011
to decimal.
- The binary number we want to convert is 1011.
- The powers of 2 from right to left are , , , and
- We multiply each digit of the binary number by its corresponding power of 2. The results are: , , , and
- We add up the results from step 3 to get the decimal value.
Therefore, the decimal value of the binary number 1011 is 11.
Binary-Decimal Table
Here's a table that shows the decimal values of the first 16 binary numbers:
Binary | Decimal |
---|---|
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
1000 | 8 |
1001 | 9 |
1010 | 10 |
1011 | 11 |
1100 | 12 |
1101 | 13 |
1110 | 14 |
1111 | 15 |
Sample Code
This code requires @ilihub/code
npm package to run.
import { BinaryToDecimal } from "@ilihub/code";
const binary = "10000000";
const decimal = BinaryToDecimal(binary);
console.log(decimal);
// Output
// 128
Top comments (0)