Saturday, 7 Mar 2026

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

  1. Divide the decimal number by 2
  2. Record the remainder
  3. Update the number to the quotient
  4. Repeat until quotient is 0
  5. 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):

DecimalBinary
00
11
210
311
4100
5101
6110
7111
81000
91001
101010

Handling Negative Numbers: Two's Complement

Computers represent negatives using two's complement:

  1. Convert absolute value to binary
  2. Invert all bits (one's complement)
  3. Add 1 to LSB (Least Significant Bit)

Example: -8

  1. 8 in binary: 1000
  2. One's complement: 0111
  3. Add 1: 0111 + 1 = 1000 → -8

Key Insight

The MSB (Most Significant Bit) indicates sign: 0=positive, 1=negative. To convert negative binary back:

  1. Invert bits
  2. Add 1
  3. 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

  1. Memory Storage: All data reduces to binary in RAM
  2. Bitwise Operations: AND/OR/XOR rely on binary manipulation
  3. Error Detection: Parity bits use binary math
  4. Data Compression: Huffman coding leverages binary trees

Pro Tip: Odd decimals always end with 1 in binary due to LSB requirement.

Practice Exercises

  1. Convert 37 to binary using division method
  2. Translate 110110 to decimal
  3. Find two's complement of -12
  4. Identify errors in this conversion: 25 → 11001

Check your answers:

  1. 100101
  2. 54
  3. 11110100
  4. Correct (25 = 16+8+1 = 11001)

Final Takeaways

  1. Binary conversion underpins low-level programming and hardware design
  2. Two's complement solves negative representation efficiently
  3. Positional power understanding is non-negotiable for DSA
  4. 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!

PopWave
Youtube
blog