Binary to Decimal Conversion: A Complete Guide
Converting binary (base-2) to decimal (base-10) is a fundamental skill for anyone working with computers. This guide covers both directions with clear explanations and working code examples.
Understanding Place Values
In a decimal number like 235, the positions represent powers of 10:
Hundreds Tens Ones
10² 10¹ 10⁰
(100) (10) (1)
2 3 5
= 200 + 30 + 5 = 235
Binary works the exact same way, but with 2 as the base:
2³ 2² 2¹ 2⁰
(8) (4) (2) (1)
Binary to Decimal: Adding Up the Powers
The Method
To convert a binary number to decimal:
- Write down the binary number
- Assign powers of 2 to each digit, starting from the right (beginning at 1)
- Add up the values only where a “1” appears
Example: Convert 1011 to Decimal
| Binary Digit | 1 | 0 | 1 | 1 |
|---|---|---|---|---|
| Weight (2ⁿ) | 2³ (8) | 2² (4) | 2¹ (2) | 2⁰ (1) |
| Calculation | 1 × 8 | 0 × 4 | 1 × 2 | 1 × 1 |
| Result | 8 | 0 | 2 | 1 |
Total: 8 + 0 + 2 + 1 = 11
So, binary 1011 is 11 in decimal.
The “Magic Row” Trick for 8-bit Numbers
For any byte (8 bits), memorize this sequence:
128, 64, 32, 16, 8, 4, 2, 1
If you see 00010101, simply look at which “slots” are active:
Position: 128 64 32 16 8 4 2 1
Binary: 0 0 0 1 0 1 0 1
Active: ✓ ✓ ✓
Total: 16 + 4 + 1 = 21
Python Implementation
def binary_to_decimal(binary_str):
decimal_value = 0
# Reverse the string to start from the right (2^0)
reversed_binary = binary_str[::-1]
for i in range(len(reversed_binary)):
if reversed_binary[i] == '1':
# Add 2 raised to the power of the current index
decimal_value += 2**i
return decimal_value
# Test the function
binary_input = "1011"
print(f"The decimal version of {binary_input} is {binary_to_decimal(binary_input)}")
# Output: The decimal version of 1011 is 11
How it works:
- Reverse the binary string so we process from right to left (least significant bit first)
- For each position that contains a ‘1’, add 2 raised to that position’s power
- Return the total
Decimal to Binary: The Division Method
Going from decimal to binary involves breaking a number down into powers of 2. The most common technique is the “Repeat Division-by-2” method.
The Method
- Divide the decimal number by 2
- Write down the remainder (always 0 or 1)
- Take the quotient and repeat until the quotient is 0
- Read the remainders from bottom to top
Example: Convert 13 to Binary
| Division | Quotient | Remainder |
|---|---|---|
| 13 ÷ 2 | 6 | 1 (Least Significant Bit) |
| 6 ÷ 2 | 3 | 0 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 (Most Significant Bit) |
Result: Reading from bottom to top → 1101
Verification
1 1 0 1
│ │ │ │
│ │ │ └─ 1 × 1 = 1
│ │ └─── 0 × 2 = 0
│ └───── 1 × 4 = 4
└─────── 1 × 8 = 8
Total: 8 + 4 + 0 + 1 = 13 ✓
Decimal to Binary: The Subtraction Method
If you know your powers of 2, this method can be faster for small numbers.
Convert 21 to Binary
Powers of 2: 128, 64, 32, 16, 8, 4, 2, 1
| Step | Question | Answer | Action | Remaining |
|---|---|---|---|---|
| 1 | Does 16 fit in 21? | Yes | Put 1 in 16s place | 21 - 16 = 5 |
| 2 | Does 8 fit in 5? | No | Put 0 in 8s place | 5 |
| 3 | Does 4 fit in 5? | Yes | Put 1 in 4s place | 5 - 4 = 1 |
| 4 | Does 2 fit in 1? | No | Put 0 in 2s place | 1 |
| 5 | Does 1 fit in 1? | Yes | Put 1 in 1s place | 1 - 1 = 0 |
Result: 10101
Python Implementation
def decimal_to_binary(n):
if n == 0:
return "0"
binary_digits = []
while n > 0:
# Get the remainder (0 or 1)
remainder = n % 2
binary_digits.append(str(remainder))
# Update n to the integer quotient
n = n // 2
# Reverse the list because we calculated from least to most significant
binary_digits.reverse()
return "".join(binary_digits)
# Test the function
decimal_input = 13
print(f"The binary version of {decimal_input} is {decimal_to_binary(decimal_input)}")
# Output: The binary version of 13 is 1101
How it works:
- Repeatedly divide by 2 and collect the remainders
- The remainders (0 or 1) are the binary digits
- Reverse at the end because we collected them least-significant-first
Quick Reference
Powers of 2 to Memorize
| Power | Value |
|---|---|
| 2⁰ | 1 |
| 2¹ | 2 |
| 2² | 4 |
| 2³ | 8 |
| 2⁴ | 16 |
| 2⁵ | 32 |
| 2⁶ | 64 |
| 2⁷ | 128 |
| 2⁸ | 256 |
| 2⁹ | 512 |
| 2¹⁰ | 1024 |
Common Byte Values
| Binary | Decimal | Hex |
|---|---|---|
| 00000000 | 0 | 0x00 |
| 00000001 | 1 | 0x01 |
| 00001010 | 10 | 0x0A |
| 00001111 | 15 | 0x0F |
| 11111111 | 255 | 0xFF |
Practice Examples
Try these yourself:
| Binary | Decimal | Your Answer |
|---|---|---|
| 1100 | 12 | |
| 0101 | 5 | |
| 1111 | 15 | |
| 10000 | 16 |
| Decimal | Binary | Your Answer |
|---|---|---|
| 7 | 111 | |
| 9 | 1001 | |
| 31 | 11111 | |
| 100 | 1100100 |
Summary
| Direction | Method | Key Idea |
|---|---|---|
| Binary → Decimal | Add powers of 2 | Sum the weights of all ‘1’ bits |
| Decimal → Binary | Division by 2 | Collect remainders, read bottom-to-top |
| Decimal → Binary | Subtraction | Find largest power that fits, repeat |