Binary numbers and actions with them. (Vahagn Mesropyan)
Published: 2023-09-13 20:51:33

The simplest definition of binary numbers is "the language of computers". This language does not contain any letters and uses only the numbers 0 and 1, which is understandable for computers. In a single bit we can put only one digit and as the binary system is in base-2 (has only to options 0 and 1) we can convert decimal numbers into binary.0 and 1 in both decimal and binary numbering systems are the same, whereas the number 2 in decimal converted to binary will be 10, 3 will be 11, 4 will be 100, etc.

1.  ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72c6fm8k1ar8zaqfisg5.jpg)

The picture above represents an example of a number in base-2 where the red numbers represent the number of each bit.
In order to know the maximum decimal number that we can represent in binary system with certain number of bits, we use this formula: 2^n-1 (1.1), where n is the number of bits. For example, in 5 bits the maximum number that we can write is 31. 
In order to convert binary number to decimal we use this formula: 2^n*k (1.2) where 2 is the number of options we can have, n is the number of a bit and k is the number written in that bit (0 or 1). Using this formula we need to define the decimal number in each bit and find their sum, which will represent that number in base-10 (decimal). Here is an example.

2.  ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mg47s6mjn1uexzki87u3.jpg)

To convert decimal number into binary number, firstly we need to define how many bits we need. For example, decimal number 12 will require 4 bits because using the formula 1.1 we can find out that 3 bits can store up to number 7 and 5 bits can store up to 31, so 4 bits can store the number 15 which we will use to convert the number 12. The bit number 3 should contain 1 as using the formula 1.2 we know that this will be 8 in decimal, so we have left 4. The bit number 2 should also contain 1 as using the same formula we can define the number 4. Other two bits should contain 0s because we already got the number 12 (8+4). So, the final answer for this question will be 1100.

Besides binary system (base-2) there are also other systems that computers understand, they are base-4, base-8, base-16 and so on. Conversions for those bases can be done with the same formula 1.2 taking in a count that we have more than 2 options there and the numbers written in bits can be bigger than 1. In case of base-16 we can use letters (A, B, C,...) to represent the numbers 10, 11,..., 15, since we can not put 2 digits in a single bit.

Actions with binary numbers are as simple as in decimal (base-10). Here is an example of addition.

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

So 1+0 gives 1, 0+0 gives 0 and 1+1 gives 0, but with a carry which is transferred to the next bit.
Subtraction is also very simple.

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

Multiplications are usually longer, but also are as simple as in decimal system.

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