# Decimal to Binary Conversion

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

• method 1: Descending Powers of Two and Subtraction
• method 2: Short Division by Two with Remainder
• Scilab: using while loop
• Scilab: using the build-in function dec2bin
• C: using while loop

When dealing with embedded systems, microcontrollers, digital electronics or engineering in general we might need to covert a decimal number (base 10) into a binary number (base 2).

We are going to convert the decimal number 57 into a binary number. The methods below can be applied to any positive integer decimal number.

#### Method 1: Descending Powers of Two and Subtraction

Before doing any calculation we are going to write down the powers of 2. In order to make the example easier to follow we are going to use. We only need to write the powers of 2 until we exceed out decimal number to be converted. Nevertheless in our example we’ll write up to 256.

$\begin{equation*} \begin{split} 2^0 =& 1\\ 2^1 =& 2\\ 2^2 =& 4\\ 2^3 =& 8\\ 2^4 =& 16\\ 2^5 =& 32\\ 2^6 =& 64\\ 2^7 =& 128\\ 2^8 =& 256 \end{split} \end{equation*}$

First we write the decimal number as a sum of powers of 2.

By looking into the power of 2 table above we can extract the highest power of 2 which is less than our decimal number (57). In our case is 32. So we write 57 as a sum of 32 and 25. The same we proceed with 25, we write it as a sum of powers of 2. Finally we get:

$\begin{equation*} \begin{split} 57 =& 32 + 25\\ =& 32 + 16 + 9\\ =& 32 + 16 + 8 + 1 \end{split} \end{equation*}$

Next, we multiply with 1 each power of 2. The sum remains the same but written closer to binary extraction:

$\begin{equation*} \begin{split} 57 =& 1 \cdot 32 + 1 \cdot 16 + 1 \cdot 8 + 1 \cdot 1 \end{split} \end{equation*}$

We write the same expression but with powers of 2:

$\begin{equation*} \begin{split} 57 =& 1 \cdot 2^5 + 1 \cdot 2^4 + 1 \cdot 2^3 + 1 \cdot 2^0 \end{split} \end{equation*}$

Now we add into the sum the missing powers of 2 but multiplied with 0 (zero) to keep the same result:

$\begin{equation*} \begin{split} 57 =& 1 \cdot 2^5 + 1 \cdot 2^4 + 1 \cdot 2^3 + 0 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 \end{split} \end{equation*}$

From the expression above we extract the coefficients of the powers of 2 which is the binary number:

$\begin{equation*} \begin{split} 111001 \end{split} \end{equation*}$

Finally, the representation of 57 in binary format is:

$\begin{equation*} \begin{split} 57_{10} = 111001_{2} \end{split} \end{equation*}$

#### Method 2: Short Division by Two with Remainder

The second method is faster and probably easier to remember than the first method.

We start by dividing our decimal number 57 by 2. The result of the division is 28 with the remainder 1. We keep in mind the remainder and continue to divide 28 by 2. The result is 14 with the remainder 0. We proceed with the division until the result of the division is equal to 1:

$\begin{equation*} \begin{split} 57 \div& 2 (1)\\ 28 \div& 2 (0)\\ 14 \div& 2 (0)\\ 7 \div& 2 (1)\\ 3 \div& 2 (1)\\ 1 \div& 2 (1) \end{split} \end{equation*}$

By extracting all the remainders of the divisions we get the binary representation of the decimal number. The remainder of the first division represents the first bit (from right).

By putting all the remainders on next to another we get the binary representation of the decimal number:

$\begin{equation*} \begin{split} 111001 \end{split} \end{equation*}$

Finally, the representation of 57 in binary format is:

$\begin{equation*} \begin{split} 57_{10} = 111001_{2} \end{split} \end{equation*}$

Obviously we got the same result as with Method 1.

If we need to represent the decimal number on 8 bits we only need to fill up the left side of the binary representation with 0 until we have a total of 8 bits:

$\begin{equation*} \begin{split} 57_{10} = 00111001_{2} \end{split} \end{equation*}$

#### Scilab: using while loop

The Scilab script below coverts a given decimal number decNo into a binary number binNo using the division by 2 method into a while loop:

// Decimal number to be converted
decNo = 57;

// Display decimal number
mprintf("Decimal: %d",decNo)

// Binary string initialization
binNo = [];

// While loop
while (decNo >= 1)
remNo = modulo(decNo,2); // calculate remainder
decNo = floor(decNo/2); // divide by 2 and keep integer
if remNo
binNo = ['1' binNo]; // if remainder is 1 add 1 to string
else
binNo = ['0' binNo]; // if remainder is 0 add 0 to string
end
end

// Transform vector of strings into one string
binNo = strcat(binNo);

// Display binary number
mprintf("\nBinary: %s",binNo)

#### Scilab: using the build-in function dec2bin

More easily we can use directly the Scilab build-in function dec2bin for decimal to binary conversion:

-->dec2bin(57)
ans =

111001

-->

You can use the dec2bin function to verify if the script above with while loop works correctly.

C: using while loop

The same algorithm, division by 2,  programmed in C language:

#include <stdio.h>

int main(void)
{

int decNo = 57;
int binNo[8];
int i = 0;

printf("Decimal number is: %d", decNo);
printf("\nBinary number is: ");

/* Calculate the binary number */
while(decNo>=1){

binNo[i]= decNo % 2;
decNo = decNo / 2;

if (decNo>=1){
i = i + 1;
}
else {
break;
}
}

/* Display the binary number */
while (i>=0){

printf("%d",binNo[i]);
i--;
}

return 0;
}

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

Image: Decimal to Binary Conversion

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

Don’t forget to Like, Share and Subscribe!