The conversion from decimal to binary is one of the most important operations we will perform when working with a binary numbering system.
People find it very difficult to work in binary. It is very uncomfortable, the numbers are very long, and we are not used to it. If someone tells us the number 00110101
in binary, frankly, we are left as we are.
However, if I tell you 53
in decimal… it is a very different thing. We visualize it better, and we could even operate with it (for example, you could multiply it by 5 mentally without too much effort).
So the conversion between binary and decimal is something you will have to do frequently. Simply because people are very accustomed to using the decimal system.
But even in machines, it is an operation that machines will have to perform frequently. Although they use binary internally, users will usually input numbers in decimal.
So, at least at some point, almost every time a program works with numbers (and they are the majority), it will have to convert user inputs from decimal to binary and outputs from binary to decimal.
Don’t “worry”! Fortunately, the conversion of a number to decimal or vice versa is quite simple. Keep reading and I will tell you how 👇.
Conversion from Binary to Decimal
To convert a binary number to decimal, we multiply each binary digit by the power of two corresponding to its position and sum the results.
For example, to convert the binary number 10110
(22) to decimal:
That is,
To do it simply, we make a table.
- At the top, we put the digits of the binary number
- Below, we put the powers of 2
- We multiply digits by powers. That is, we remove those that are
0
in binary - We add them up
Binary | 1 | 0 | 1 | 1 | 0 |
---|---|---|---|---|---|
Power | 16 | 8 | 4 | 2 | 1 |
Multiply | 16 | 4 | 2 |
Finally, we sum the numbers in the last row
Conversion from Decimal to Binary
To carry out the reverse conversion, from decimal to binary, we can use the method of successive division or the method of powers of two.
- We divide the decimal number by 2
- We take the remainder of the operation
- We repeat the process until the quotient is
0
. - The obtained binary bits are the result
Let’s see it with an example. Suppose we have the same number 22 in decimal, which we want to convert to binary.
Using the method of successive division we have,
22 / 2 = 11 remainder 0
11 / 2 = 5 remainder 1
5 / 2 = 2 remainder 1
2 / 2 = 1 remainder 0
1 / 0 = 0 remainder 1
If you find it easier in table form, here it is.
Operation | Step 5 | Step 4 | Step 3 | Step 2 | Step 1 | Start | |||||
---|---|---|---|---|---|---|---|---|---|---|---|
Divide / 2 | 0 | ← | 1 | ← | 2 | ← | 5 | ← | 11 | ← | 22 |
Remainder | 1 | 0 | 1 | 1 | 0 |
The resulting binary number is 11001
.