Binary numbers
Published: 2022-09-13 15:01:24

Unlike our known decimal system, which uses 10 as a base and is aimed to calculate practically everything in our everyday life, binary numbers are used in computer applications. From the root of the word it becomes obvious that in this system the base is 2 and the numbers are represented by only two symbols or digits - 1 and 0. However, we can represent every single number within using only two digits (for example numbers from 0 to 10 we can represent as 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, and 1010 (I will show how to get these numbers a bit later)). The binary system is significant for information theory and computer technology because it is compact and reliable in order to be represented in electromechanical devices with two states such as “1-on - 0-off,” “1-open - 0-closed,” or “1-go – 0-no go". 

In general, a binary number is longer than its corresponding decimal number. For example, the 1234567 number in the decimal system equals to 100101101011010000111 represented binary. The binary number is longer because a binary digit only distinguishes between the possibilities of 0 or 1 whereas a decimal digit distinguishes among 10 possibilities. To put it in another way, a binary digit carries less information than a decimal digit. 

Every digit is referred to as a bit: a bit of information is transmitted whenever one of two alternatives is realized in the machine. For instance 1001101 is a seven-bit number, 1010 is a four-bit binary number.

## Relation between decimal and binary numbers

Let's take a random decimal number and convert it to a binary number.

For example 5 in binary is (101)2.

In this example, the decimal number system is used to represent the number 5, and the digits 0 through 9 can be used to do so. In contrast, as I mentioned above a binary number system simply employs two digits, 0 and 1. 

Now, let’s understand how to convert 5 in the binary number system. The following steps help to convert 5 in binary.

Step 1: First, we should divide the number 5 by 2. Use the integer quotient obtained in this step as the dividend for the next step. Continue this step, until the quotient becomes 0.

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

Step 2: Now, we should write the remainder in reverse chronological order. (i.e from bottom to top).
Here, the Least Significant Bit (LSB) is 0 and the Most Significant Bit (MSB) is 1.
Hence, the decimal number 5 in binary is (101)2

The number of bits of the number 5 in binary is 3.


Now let's take a binary number and convert it to a decimal number.

For example, if we take binary number 101, then we can divide the number into columns. Starting from the right side, there is 1 in the first column, 0 in the second column and again 1 in the third column. Now, we should multiply each column with the powers of 2, starting from 0 and right hand side, like depicted in the picture. 

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

The number can be converted to decimal by multiplying out as follows:
1*1 + 0*2 + 1*4 = 5
Simple calculations show that 101 in the binary system equals 5 in the decimal system. 

The same way we can convert any binary number. Another example to clarify the process.
Let's take 1101. Now we should do some calculations.
1*1 + 0*2 + 1*4 + 1*8 = 13

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

1101 in binary system is 13 in decimal system. 

## Binary Addition
Adding two binary numbers will give us a binary number itself. It is the simplest method. The table below shows the addition of two binary numbers with one digit.

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

As you see, adding 1 and 1 gives us carry 1. Like in decimal system addition, we move the carry to the next step and continue calculating considering the carry. 
For example, let's add 1101 and 1001.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwtdtash6saduecvh94b.png)
As you can see in the last step we had a carry, so we put the 1 immediately in the answer because there was no steps left. So the answer is 10110.

## Binary Subtraction
Subtracting two binary numbers will give us a binary number too. It is also a straightforward method. Subtraction of two single-digit binary number is given in the table below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1afczfz8btlhik3bxyls.png)
For instance, let's subtract 1101 and 1010. The solution will be 

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

In the second step, we see that we should subtract 1 from 0. Therefore, we borrow 1 from the 0's left side neighbor 1 and in the result we get 1. However, as we already borrowed 1, the next step will be 0 - 0 which equals to 0. 

## Binary Multiplication
The multiplication process is the same for the binary numbers as it is for numerals. Let's look at an example. 
 Multiply 1101 and 1010
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnp2tvm2tguns13dzi94.png)
First, we multiply the first number by 0. Then emitting one symbol, multiplying the same number by 1 and so on. I want to repeat that in this step multiplication in binary system is the same as in decimal system. Afterwards, we add the binary numbers like I showed above and finally get the desired number.