Binary to Decimal Conversion

beginner
24 min

Binary to Decimal Conversion

Welcome to CodeYourCraft's tutorial on Binary to Decimal Conversion! In this lesson, we'll dive into the fascinating world of binary numbers, learn how to convert binary to decimal, and understand the practical applications of this conversion in real-world projects. ๐ŸŽฏ

What is Binary Number System?

Before we start, let's understand what binary numbers are. The binary number system is a base-2 system, meaning it uses only two digits: 0 and 1. This system is crucial in computing and digital electronics because it represents the off and on states of electronic switches (transistors). ๐Ÿ“

Understanding Binary and Decimal Numbers

Binary numbers are usually longer than decimal numbers. To convert binary to decimal, we need to understand the place value of each binary digit. Here's a simple breakdown:

  • The rightmost digit is the ones place (2^0)
  • The digit to its left is the twos place (2^1)
  • The digit to its left is the fours place (2^2)
  • And so on...

Let's see a quick example:

Binary: 1011

Decimal: 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 1 * 2^0 = 8 + 0 + 2 + 1 = 11

Converting Binary to Decimal: Step-by-Step Guide

  1. Write down the binary number you want to convert.
  2. Identify the place value of each digit starting from the rightmost digit (ones place).
  3. Multiply each binary digit by its corresponding place value.
  4. Add up all the results to get the decimal equivalent.

Here's another example to help you understand better:

Binary: 10101

Decimal: 1 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 16 + 0 + 4 + 0 + 1 = 21

Converting Decimal to Binary (Bonus Section!)

Though this tutorial focuses on converting binary to decimal, it's essential to know that we can also convert decimal to binary. To do this, we follow the "divide by 2 and remainder" method:

  1. Write down the decimal number you want to convert.
  2. Divide the number by 2 and record the remainder.
  3. Repeat step 2 with the quotient until you get a quotient of 1.
  4. Write down the remainders in reverse order to get the binary equivalent.

For example:

Decimal: 13

Binary: 1101

(13 รท 2 = 6 remainder 1, 6 รท 2 = 3 remainder 0, 3 รท 2 = 1 remainder 1, 1 รท 2 = 0 remainder 1)

Practical Application and Quiz

Now that you've learned the basics of binary to decimal conversion, it's time to apply this knowledge in a practical context.

Quick Quiz
Question 1 of 1

What is the decimal equivalent of the binary number 1101?

Code Examples

Let's see some code examples that demonstrate binary to decimal conversion using Python:

python
def bin_to_dec(binary): decimal = 0 for i, digit in enumerate(reversed(binary)): decimal += int(digit) * 2 ** i return decimal # Example usage: binary_number = "1101" decimal_number = bin_to_dec(binary_number) print(f"Binary number: {binary_number} converts to Decimal: {decimal_number}")
javascript
function binToDec(binary) { let decimal = 0; for (let i = binary.length - 1; i >= 0; i--) { decimal += parseInt(binary[i]) * Math.pow(2, i); } return decimal; } // Example usage: const binaryNumber = "1101"; const decimalNumber = binToDec(binaryNumber); console.log(`Binary number: ${binaryNumber} converts to Decimal: ${decimalNumber}`);

Wrapping Up

In this comprehensive lesson, we've covered the basics of binary to decimal conversion, explored the practical applications of this conversion, and learned to code our own binary to decimal conversion functions in both Python and JavaScript. With this knowledge, you're well-equipped to tackle binary to decimal conversions in your own projects. Keep learning, and happy coding! ๐Ÿ’ก