How to convert from binary to decimal

In this tutorial we are going to learn about converting a binary number into a decimal number with:

  • Method 1: Multiply bits with powers of two
  • Method 2: Using Doubling
  • Scilab programming: using for loop
  • Scilab programming: using the build-in function bin2dec
  • C programming: using for loop

Before going through this article, it is recommended to have a basic understanding about:

Numbers Representation Systems – Decimal, Binary, Octal and Hexadecimal

A binary number is a series of ones (1) and zeros (1). The ones (1) and zeros (0) are called bits. Let’s take as example the binary number 111001. The extreme right bit is bit number 0, the extreme left bit is bit number 5.

Method 1: Multiply bits with powers of two

Before converting to decimal let’s write down the powers of two. We will use only 8 bits for this example:

\[2^7\] \[2^6\] \[2^5\] \[2^4\] \[2^3\] \[2^2\] \[2^1\] \[2^0\]
128 64 32 16 8 4 2 1

Under each power of two result we’ll write the corresponding bit value:

128 64 32 16 8 4 2 1
0 0 1 1 1 0 0 1

Now we’ll multiply each bit value with the corresponding power of two and add the products together:

\[0 \cdot 128 + 0 \cdot 64 + 1 \cdot 32 + 1 \cdot 16 + 1 \cdot 8 + 0 \cdot 4 + 0 \cdot 2 + 1 \cdot 1\]

The result of the sum is the decimal number:

\[32 + 16 + 8 + 1 = 57\]

The binary number converted to decimal is:

\[111001_{2} = 57_{10}\]

Method 2: Using Doubling

This method doesn’t use the power of two. For this reason it should be simpler to convert lager binary numbers into decimal.

As an example we’ll use the same binary number as in first method: 111001

This method uses a concept named previous total. For the first step the previous total is 0.

We start by taking the previous total, multiply it by 2 and add the extreme left bit (bit number 5).

\[0 \cdot 2 + 1 = 1\]

The result of the above operations is the previous total for the next step. In our case the previous total becomes 1.

Next, take the previous total, multiply it by 2 and add the following bit (bit number 4). We get:

\[1 \cdot 2 + 1 = 3\]

We do the same operations until we run out of bits:

\[ \begin{equation*} \begin{split}
3 \cdot 2 + 1 = 7\\
7 \cdot 2 + 0 = 14\\
14 \cdot 2 + 0 = 28\\
28 \cdot 2 + 1 = 57
\end{split} \end{equation*} \]

After we run out of bits, the latest previous total is our converted decimal number: 57.

As expected, the binary number converted to decimal is:

\[111001_{2} = 57_{10}\]

Scilab: using for loop

Scilab implementation of Method 1: Multiply bits with powers of two

// Binary number to be converted
binNo = '111001';

// Initialization of the decimal number
decNo = 0;

// Loop all bits in the binary number
for i=1:length(binNo)
 if part(binNo,i) == '1'
  decNo = decNo + 1 * 2^(length(binNo)-i);
 else
  decNo = decNo + 0 * 2^(length(binNo)-i);
 end
end

// Display binary and decimal numbers
mprintf("Binary number %s \nDecimal number: %d", binNo, decNo);

Scilab implementation of Method 2: Using Doubling

// Binary number to be converted
binNo = '111001';

// Initialization of the decimal number
prevTot = 0;

// Loop all bits in the binary number
for i=1:length(binNo)
 if part(binNo,i) == '1'
  prevTot = prevTot * 2 + 1;
 else
  prevTot = prevTot * 2 + 0;
 end
end

// Display binary and decimal numbers
mprintf("Binary number %s \nDecimal number: %d", binNo, prevTot);

Scilab programming: using the build-in function bin2dec

In order to verify if the above conversion algorithm are properly designed, we can use the build-in Scilab function bin2dec to convert from binary to decimal numbers:

-->bin2dec('111001')
 ans =
 
 57. 
 
-->

C programming: using for loops

C implementation of Method 1: Multiply bits with powers of two

#include <stdio.h>
#include <string.h>
#include <math.h>

int main(void)
{

char binNo[] = "111001";
int decNo = 0;
int i;
int stringLen;

stringLen = strlen(binNo); // Length of string

for (i=0; i<stringLen; i++){
 if (binNo[i]=='1'){
  decNo = decNo + 1 * pow(2,(stringLen-1-i)); 
 }
 else{
  decNo = decNo + 0 * pow(2,(stringLen-1-i));
 }
}

printf("Binary: %s", binNo);
printf("\nDecimal: %d", decNo);
 
return 0;
}

C implementation of Method 2: Using Doubling

#include <stdio.h>
#include <string.h>

int main(void)
{

char binNo[] = "111001";
int prevTot = 0;
int i;
int stringLen;

stringLen = strlen(binNo); // Length of string

for (i=0; i<stringLen; i++){
 if (binNo[i]=='1'){
 prevTot = prevTot * 2 + 1; 
 }
 else{
 prevTot = prevTot * 2 + 0;
 }
 }

printf("Binary: %s", binNo);
printf("\nDecimal: %d", prevTot);
 
return 0;
}

As reference save the image below which contains a summary of both methods for binary to decimal conversion.

Binary to Decimal Conversion Poster

Image: Binary to Decimal Conversion Poster

For any questions, observations and queries regarding this article, use the comment form below.

Don’t forget to Like, Share and Subscribe!

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh