Master Binary-Decimal Conversion: Step-by-Step Guide & Code
Understanding Number Systems
Computers process data using binary (base-2) digits (0/1), unlike humans who use decimal (base-10). This fundamental difference impacts how information is stored in memory. While decimal uses digits 0-9, binary relies solely on 0 and 1. Other systems like hexadecimal (base-16) and octal (base-8) exist but binary remains the foundation of computing.
Key Differences Explained
- Decimal: Base-10 system (e.g., 42)
- Binary: Base-2 system (e.g., 101010 for decimal 42)
- Positional Notation: Each digit represents a power of the base (e.g., binary 101 = 1×2² + 0×2¹ + 1×2⁰ = 5)
Decimal to Binary Conversion
Step-by-Step Division Method
- Divide the decimal number by 2
- Record the remainder
- Update the number to the quotient
- Repeat until quotient is 0
- Read remainders backward
Example: Convert 42
42 ÷ 2 = 21 (rem 0) ↑
21 ÷ 2 = 10 (rem 1) ↑
10 ÷ 2 = 5 (rem 0) ↑
5 ÷ 2 = 2 (rem 1) ↑
2 ÷ 2 = 1 (rem 0) ↑
1 ÷ 2 = 0 (rem 1) → 101010
Practical Trick: Power Subtraction
Identify largest power of 2 ≤ number, subtract, and set corresponding bit:
42: 32 (2⁵) → 1 _ _ _ _ _
42-32=10 → 1 _ _ _ 1 _
10-8=2 → 1 0 1 _ 1 _
2-2=0 → 1 0 1 0 1 0
Binary to Decimal Conversion
Multiply each bit by its positional power (2ⁿ) and sum:
101010 = (1×2⁵) + (0×2⁴) + (1×2³) + (0×2²) + (1×2¹) + (0×2⁰)
= 32 + 0 + 8 + 0 + 2 + 0 = 42
Common Binary Equivalents (Memorize These):
| Decimal | Binary |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
| 8 | 1000 |
| 9 | 1001 |
| 10 | 1010 |
Handling Negative Numbers: Two's Complement
Computers represent negatives using two's complement:
- Convert absolute value to binary
- Invert all bits (one's complement)
- Add 1 to LSB (Least Significant Bit)
Example: -8
- 8 in binary: 1000
- One's complement: 0111
- Add 1: 0111 + 1 = 1000 → -8
Key Insight
The MSB (Most Significant Bit) indicates sign: 0=positive, 1=negative. To convert negative binary back:
- Invert bits
- Add 1
- Prepend minus sign
Code Implementation
C++: Decimal to Binary
int decToBin(int n) {
int ans = 0, power = 1;
while (n > 0) {
int rem = n % 2;
ans += rem * power;
power *= 10; // Positional shift
n /= 2;
}
return ans;
}
C++: Binary to Decimal
int binToDec(int bin) {
int ans = 0, power = 1;
while (bin > 0) {
int lastDigit = bin % 10;
ans += lastDigit * power;
power *= 2;
bin /= 10;
}
return ans;
}
Critical Applications
- Memory Storage: All data reduces to binary in RAM
- Bitwise Operations: AND/OR/XOR rely on binary manipulation
- Error Detection: Parity bits use binary math
- Data Compression: Huffman coding leverages binary trees
Pro Tip: Odd decimals always end with 1 in binary due to LSB requirement.
Practice Exercises
- Convert 37 to binary using division method
- Translate 110110 to decimal
- Find two's complement of -12
- Identify errors in this conversion: 25 → 11001
Check your answers:
- 100101
- 54
- 11110100
- Correct (25 = 16+8+1 = 11001)
Final Takeaways
- Binary conversion underpins low-level programming and hardware design
- Two's complement solves negative representation efficiently
- Positional power understanding is non-negotiable for DSA
- Master the 0-10 binary equivalents to accelerate debugging
Which conversion step do you find most challenging? Share your hurdles below – let's troubleshoot together!