DEV Community

Cover image for Binary numbers and decimal numbers (the conversion and different bases)
Vana Hakop Chakerian
Vana Hakop Chakerian

Posted on

Binary numbers and decimal numbers (the conversion and different bases)

Hello everyone! In this post, we will learn one of the most essential and basic computer science concepts, which is your beloved science's starting point. That is how to convert the decimal to binary and vice versa. So, let's go over it and discover a new world for us.

When you look at the pictures on your computer, the text, the files, and everything, you may ask yourself, what is it for computers? All of them are binary data consisting of 1s and 0s.
This code may represent a word such as 10, equal to A.
Computers use 0s and 1s (which are bits each of them) to store information.
1 is considered a true value, and 0 is a false value, which is the same thing as saying you will get 1 when electricity flows and 0 when there is no electricity flow. So, these single bits can be used when you want to save information, for example, is this year 2024? Yes ( = true = 1).
However, computers use the collection of bits to show more complex information than true/false.
Let's take a look at this example

If we have just two digits, there are four ways to represent it
00, 01, 10, 11                     ( 2 ^2 = 4 ways)
Then, if we have three digits, the possible combination of digits would be
000, 001, 011, 111, 100, 101, 110, 010              (2 ^3 = 8)
As you might see, the pattern is 2 to the power of n. But why 2 to the power of something?
Because only two numbers (1 and 0) can combine with each other and represent a command. In the same way, for decimals or base 10 (0 through 9), it would be 10 to the power of n. For base 8, 8^n, etc. Therefore, "base" refers to the number of unique digits, including zero, that a number system uses to represent values.

 Base 2 (Binary): Uses two digits (0 and 1).
 Base 8 (Octal): Uses eight digits (0 through 7).
 Base 10 (Decimal): Uses ten digits (0 through 9).
 Base 16 (Hexadecimal): Uses sixteen digits (0 through 9 and A through F)

You may have a question about why we use Octal and Hexadecimal bases.
For the first let's take a look at the hexadecimal (Base 16)
Compact representation: it condenses binary data, making it easier to read and write, and significantly reduces the length of binary sequences.
For example, the binary 1010 1111 is AF in hexadecimal.
Alignment with Bytes: Since one byte is 8 bits, two hexadecimal digits represent one byte, making it convenient for addressing memory and working with color codes in graphics (like #FF5733).
Ease of Conversion: It's straightforward to convert between binary and hexadecimal, making it useful in programming and debugging.
 

For base 16, our set would be:
0, 1, 2 ,3, 4, 5, 6, 7, 8, 9,  A    B    C      D     E      F    (in hexadecimal)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  11  12    13   14   15  (in decimal)
                                                 
 Therefore, we use the letters to show the number after 9. As far as base 16 goes, the numbers would be smaller, so we use letters. Take 10 as an example (10 = A).

Octal (Base 8)

Simpler Binary Grouping: Each octal digit corresponds to three binary digits (bits), which simplifies the representation of binary numbers compared to using decimal.
For example, the binary 111 000 translates to 70 in octal.

Historical Use: Octal was historically used in some computing systems (like early Unix systems) and is still sometimes seen in permissions settings (e.g., chmod 755).

Readability: For certain applications, octal can be more human-readable than long binary sequences

Another thing about getting the highest possible number of digits is the formula of 2^n-1.

For now, let's start with the conversion part.

                       ** Decimal system**
Enter fullscreen mode Exit fullscreen mode

From our school years, we always say that numbers from the right would be 10 ^ n (starting from 0 index= power)
234 = 4 * 10^0  +  3 * 10^1 + 2*10^2 = 4*1 + 3* 10 + 2* 100
In the same way, we can convert binary to decimal by just one difference in base, which would be 2.
For example,
1010 = ? decimal
1          0             1                0
2^3    2^2       2^1           2^0   = (1 * 0 + 1* 2 + 0* 4 + 1*8 = 10 in decimal)
So, to convert the numbers from binary to decimal, you have to write the base to the power of n, where n starts from zero, and you have to start it from the right side.
Here are the most useful powers with base 2:
2^0 = 1                        2^5 = 32
2^2 = 4                        2^6 = 64
2^3 = 8                        2^7 = 128
2^4 = 16                      2^8 = 256

Now, let's convert from decimal to binary.
There are two ways to convert from decimals to binary: comparing or dividing, and we will learn both.
First of all, consider some blanks (usually it is in the group of 4 or 8 digits)
6 in decimal a ? in binary
 _          _           _           _  
 8         4          2           1              (these numbers result from 2^1, 2^2, etc.)

First, compare 6 with the leftist term ( 8 )and ask yourself, is this value below the blank (8) greater than 6?
If Yes, write 0
If No, write 1
So 8 > 6, so the answer is yes.
 0    _       _      _
8      4      2      1
Then, the next term. Is 4 > 6? No
So write 1.
0       1     _      _
8       4     2       1
After writing 1, subtract 4 from 6 (6 – 4 = 2). Now compare 2 with the values written below the blanks.
2 > 2? No, it is equal. So write 1 and subtract 2 – 2 = 0.
0      1        1      _
8       4        2       1
Hence, there is no value to represent put 0.
0        1          1        0
8         4          2         1
Therefore, 6 in decimal = 0110 or 110 in binary.
Here, you can find the binary version of our decimal numbers.
0 = 0
1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110
7 = 111
8 = 1000
9 = 1001
10 = 1010
This is my favorite way of converting, but let's see the second way by division.
Let's take the same example to make it easy to compare it with the first one.
We have 6 Yes? Alright.
Now divide 6 with base 2 (because binary numbers are in base 2)
6 | 2 0 (is six divisible by two? If yes, write 0)
3 | 2 1
1 | 2  1
0 | 2 0
The numbers on the left side are the quotient (except 6), and the numbers on the right (0s and 1s) are the remainders.
Therefore, starting from above, the number will be 0110 or 110, which is the same as the answer in the first way.
Note: To convert from base 2 to base 8 and 16, you have to divide it by 8 and 16.

Good job guys.

Thank you for taking the time to read this article. It will be helpful for you.

Top comments (0)