Converting Decimal to Binary
Table of Contents
Decimal to Binary
If you are following this in order. Now comes the biggest question, how do we translate between binary and decimal. On the application layer we are typing the number 42 into our phone/computer, rather than the binary number.
This post is the Rosetta Stone. We will learn the algorithms to translate human numbers into computer language and vice versa.
Binary to Decimal
First, let's solidify the conversion from "Computer" to "Human." We touched on this briefly, but let's formalize the process.
The Method: Positional Expansion.
- Write down the binary number.
- Write the powers of 2 above each digit ($2^0, 2^1, 2^2...$) starting from the right.
- Multiply each binary digit by the power of 2 above it.
- Sum the results.
Example: Convert 1101 to Decimal.
| $2^3$ (8) | $2^2$ (4) | $2^1$ (2) | $2^0$ (1) |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
$$ \begin{aligned} 1 \times 8 &= 8 \\ 1 \times 4 &= 4 \\ 0 \times 2 &= 0 \\ 1 \times 1 &= 1 \\ \text{Total} &= 8 + 4 + 0 + 1 = 13 \end{aligned} $$
Result: 13
Decimal to Binary
This is the direction that usually trips people up. How do we go from a complex base-10 number to a string of 0s and 1s?
The most reliable method is the Repeated Division by 2.
The Logic: When we divide a number by 2, the remainder tells us the value of the rightmost bit (the Ones place).
- If the number is even, the remainder is 0.
- If the number is odd, the remainder is 1.
By repeating this process with the quotient (the answer to the division), we move from right to left, building our binary number.
The Method:
- Divide the decimal number by 2.
- Record the Remainder (0 or 1). This is your next binary digit.
- Take the Quotient (the whole number result) and divide it by 2 again.
- Repeat until the quotient is 0.
- Read the remainders from Bottom to Top.
Example: Convert 13 to Binary.
| Division | Quotient | Remainder |
|---|---|---|
| $13 \div 2$ | 6 | 1 |
| $6 \div 2$ | 3 | 0 |
| $3 \div 2$ | 1 | 1 |
| $1 \div 2$ | 0 | 1 |
Now, read the remainders from bottom to top: 1101.
This matches our previous calculation! $1101_2 = 13_{10}$.
Practice Problems
The best way to learn binary is to do it. Grab a piece of paper and try these.
Problem 1: Binary to Decimal
Convert the binary number 10110 to decimal.
Problem 2: Decimal to Binary Convert the decimal number 25 to binary.
Being able to convert between decimal and binary bridges the gap between human logic and machine logic.
In programming, you might encounter "bitmasks" or file permissions (like chmod 755), which rely on binary positions. In networking, IP addresses and subnet masks are calculations of binary powers. Understanding the conversion process ensures you aren't just memorizing codes, but understanding the fundamental structure of digital information.