Binary to Decimal and Back: Understanding Number Systems

1/10/2025

Introduction: Why Binary?

At the heart of every computer, smartphone, and digital device lies the binary system. While we humans typically use the decimal (base-10) system with digits 0 through 9, computers operate using binary (base-2), which only uses two digits: 0 and 1. These represent the two fundamental states of electronic circuits: off (0) and on (1). Understanding how to convert between these systems is crucial for anyone interested in computer science, programming, or digital electronics.

Decimal (Base-10) Recap

Let's quickly recap our familiar decimal system. Each position in a decimal number represents a power of 10. For example, the number 345 means:

34510=(3×102)+(4×101)+(5×10)345*{10} = (3 \times 10^2) + (4 \times 10^1) + (5 \times 1^0) 34510=(3×100)+(4×10)+(5×1)345*{10} = (3 \times 100) + (4 \times 10) + (5 \times 1) 345_10=300+40+5=345345\_{10} = 300 + 40 + 5 = 345

The subscript 10 denotes that it's a base-10 number.

Binary (Base-2) Explained

The binary system works similarly, but each position represents a power of 2. The digits can only be 0 or 1.

Consider the binary number 110121101_2. The subscript 2 indicates base-2. To convert this to decimal, we multiply each digit by the corresponding power of 2, starting from the rightmost digit (which corresponds to 202^0):

11012=(1×23)+(1×22)+(0×21)+(1×20)1101*2 = (1 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0) 11012=(1×8)+(1×4)+(0×2)+(1×1)1101_2 = (1 \times 8) + (1 \times 4) + (0 \times 2) + (1 \times 1) 11012=8+4+0+1=13101101_2 = 8 + 4 + 0 + 1 = 13*{10}

So, the binary number 1101 is equivalent to the decimal number 13.

Example 2: Convert 10110210110_2 to decimal.

101102=(1×24)+(0×23)+(1×22)+(1×21)+(0×20)10110*2 = (1 \times 2^4) + (0 \times 2^3) + (1 \times 2^2) + (1 \times 2^1) + (0 \times 2^0) 11012=(1×16)+(0×8)+(1×4)+(1×2)+(0×1)1101_2 = (1 \times 16) + (0 \times 8) + (1 \times 4) + (1 \times 2) + (0 \times 1) 101102=16+0+4+2+0=221010110_2 = 16 + 0 + 4 + 2 + 0 = 22*{10}

Converting Decimal to Binary

Converting from decimal to binary involves repeatedly dividing the decimal number by 2 and recording the remainders. The binary representation is formed by reading the remainders in reverse order.

Let's convert the decimal number 13_1013\_{10} back to binary:

  1. 13÷2=613 \div 2 = 6 remainder 1
  2. 6÷2=36 \div 2 = 3 remainder 0
  3. 3÷2=13 \div 2 = 1 remainder 1
  4. 1÷2=01 \div 2 = 0 remainder 1

Now, read the remainders from bottom to top: 1101. So, 13_10=1101213\_{10} = 1101_2, which matches our previous conversion!

Example 2: Convert 22_1022\_{10} to binary.

  1. 22÷2=1122 \div 2 = 11 remainder 0
  2. 11÷2=511 \div 2 = 5 remainder 1
  3. 5÷2=25 \div 2 = 2 remainder 1
  4. 2÷2=12 \div 2 = 1 remainder 0
  5. 1÷2=01 \div 2 = 0 remainder 1

Reading the remainders from bottom to top: 10110. So, 22_10=10110222\_{10} = 10110_2.

Binary in Code

Most programming languages provide ways to work with binary numbers or perform conversions.

Python Example:

# Convert decimal to binary string
decimal_num = 22
binary_string = bin(decimal_num)
print(f"Decimal {decimal_num} is Binary: {binary_string}") # Output: Decimal 22 is Binary: 0b10110

# The '0b' prefix indicates a binary literal

# Convert binary string to decimal integer
binary_num_string = "1101"
decimal_equivalent = int(binary_num_string, 2) # The 2 indicates base-2
print(f"Binary {binary_num_string} is Decimal: {decimal_equivalent}") # Output: Binary 1101 is Decimal: 13

# You can also write binary literals directly
binary_literal = 0b10110
print(f"Binary literal 0b10110 is Decimal: {binary_literal}") # Output: Binary literal 0b10110 is Decimal: 22

Conclusion

Understanding binary-decimal conversion is fundamental to comprehending how computers store and process information. While it might seem abstract at first, the process is systematic, relying on the concept of place value based on powers of 2. Whether you're debugging code, studying computer architecture, or just curious about the digital world, mastering these conversions is a valuable skill.

Explore More Blogs

2/7/2025

Understanding Compound Interest: Make Your ...

Learn the magic of compound interest – how earning interest on your interest can...

2/21/2025

What is an Algorithm? A Beginner's Guide

Demystify the term 'algorithm'. Learn what algorithms are, why they're essential...

3/7/2025

Pythagorean Theorem Explained with Real-Wor...

The Pythagorean theorem (a² + b² = c²) explained: how this geometric principle a...