Binary numbers. How one can convert decimal to binary and binary to decimal. Binary numbers addition and multiplication.
Published: 2022-09-15 21:01:25

## Binary numbers - how the computer thinks.


In order to understand in general terms how a computer thinks, let's start from the very beginning. A computer is essentially a lot of electronics put together in the right order. And electronics (before the program was added to it) understands only one thing: it is on or off, there is a signal or there is no signal.

Usually "there is a signal" is denoted by one, and "no signal" is denoted by zero: hence the expression that "the computer speaks the language of zeros and ones."

This language of zeros and ones is also called the binary number system - because it has only two digits. Our usual number system is decimal, it has ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). But there are many others - five, eleven, and whatever else.

You and I don't have the number ten, do we? The number 10 consists of two digits - 1 and 0.

The binary number system, however, also looks strange for an unfamiliar look:

0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000, 10001…

These are the numbers that the computer thinks somewhere inside itself. But it is completely inconvenient for a person to think with such numbers, so we will convert numbers from binary to a more convenient number system.





## Converting numbers from decimal to binary


How to convert a number from decimal to binary. And here everything is actually very, very simple, although it is difficult to explain in words, so I will immediately give** an example of converting numbers from decimal to binary**. Let's take the number 61, in order to convert to the binary system, we need to divide this number by two and see what happens in the remainder of the division. And the result of the division is again divided by two. In this case, 61 is the dividend, we will always have a two as a divisor, and we divide the quotient (the result of division) again by two, continue dividing until the quotient is 1, this last unit will be the leftmost digit . The figure below demonstrates this.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyg0c8ucnwmxoggo8gh5.png)

At the same time, note that the number 61 is not 101111, but 111101, that is, we write out the result from the end.

And vice versa, it also works! For example, the number 11010101 - how to make a clear decimal out of it? In the same way. Let's go from the end:

1*2^0+0*2^1+1*2^2+0*2^3+1*2^4+0*2^5+1*2^6+1*2^7=

1*1+0*2+1*4+0*8+1*16+0*32+1*64+1*128=

1+0+4+0+16+0+64+128=213

That is,

11010101 = 213

This is approximately how the computer understands the numbers we are used to.





## Binary addition


Adding binary numbers is not that hard: 1+0 =1; 1+1=0 (later I will give an explanation); 0+0=0. These were simple examples where only one digit was used, let's look at examples where the number of digits is more than one.
101 + 1101 in decimal is 5 + 13 = 18. Let's count in a column.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42dnv5ima4bid51o4ylv.png)


The result is highlighted in orange, the calculator says that we calculated correctly, you can check it. Now let's see why it happened, because at first I wrote that 1 + 1 = 0, but this is for the case when we have only one digit, for cases when there are more than one digits, 1 + 1 = 10 (or two in decimal), which is logical.

Then look what happens, we perform additions by digits from right to left:

1. 1+1=10, write zero, and one goes to the next bit.

2. In the next digit, 0+0+1=1 is obtained (this unit came to us from the result of addition in step 1).

3. Next, we have the expression 1 + 1 = 10, so we write 0 to the result of the discharge, and the unit goes to the next one.

4. Here we have a unit only for the second number, but it has been transferred here, so 0 + 1 + 1 = 10.

5. Glue everything together: 10|0|1|0.

If laziness is in a column, then let's count like this: 101011 + 11011 or 43 + 27 = 70. What can we do here, but let's look, because no one forbids us to make transformations, and the sum does not change from changing the places of the terms, for the binary number system this rule also applies.

1. 101011 = 101000 + 11 = 101000 + 10 + 1 = 100000 + 1000 + 10 + 1
2. 11011 = 11000 + 10 + 1 = 10000 + 1000 + 10 + 1
3. 100000 + 10000 + (1000 +1000) + (10+10) + (1+1)
4. 100000 + (10000 + 10000) + 100 + 10
5. 100000 + 100000 +110
6. 1000000 + 110
7. 1000110

You can check with a calculator, 1000110 in binary is 70 in decimal.





## Multiplication of binary numbers


It remains for us to consider the multiplication of binary numbers, which is realized by shifting one bit to the left. But first, let's look at the results of a single-digit multiplication: 1*1 = 1; 1*0=0 0*0=0. Actually, everything is simple, now let's look at something more complex. Let's take the numbers 101001 (41) and 1100 (12). We will multiply by a column.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8cddq3h8rg4ndwk8cks.jpeg)


If it is not clear from the table how it happened, then I will try to explain in words:

1. It is convenient to multiply binary numbers in a column, so we write out the second factor under the first, if the numbers have a different number of digits, then it will be more convenient if the larger number is on top.

2. The next step is to multiply all the digits of the first number by the least significant digit of the second number. We write down the result of the multiplication below; in this case, it is necessary to write it down so that the result of the multiplication is written under each corresponding digit.

3. Now we need to multiply all the digits of the first number by the next digit of the second number and write the result one more line below, but this result needs to be shifted one digit to the left, if you look at the table, this is the second sequence of zeros from the top.

4. You need to do the same for subsequent digits, each time moving one digit to the left, and if you look at the table, you can say that one cell to the left.

5. We got four binary numbers, which we now need to add and get the result. Addition we recently considered, problems should not arise.

In general, the multiplication operation is not so difficult, you just need to practice a little.