In this tutorial we are going to learn about converting a binary number into a decimal number with:
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.
Before converting to decimal let’s write down the powers of two. We will use only 8 bits for this example:
| 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
| 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⋅128+0⋅64+1⋅32+1⋅16+1⋅8+0⋅4+0⋅2+1⋅1
The result of the sum is the decimal number:
32+16+8+1=57
The binary number converted to decimal is:
1110012=5710
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⋅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⋅2+1=3
We do the same operations until we run out of bits:
3⋅2+1=77⋅2+0=1414⋅2+0=2828⋅2+1=57
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:
1110012=5710
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);
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 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.

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!

