Binary numbers
Tags: binary, binarytodecimal, decimaltobinary, operationswithbinary
Published: 2022-09-15 21:53:41

Binary code is the language computers understand. Binary numbers were first used in ancient Egypt. However, in the 17th century Gottfried Wilhelm Leibniz created the binary system which we use nowadays.
**So what is binary system?** It is base-2 numeral system requiring only 2 symbols for its numbers – 0 and 1. Examples of binary numbers are: 0, 1, 10, 11, 100, 101, 111, 1000, … .
**Converting decimal numbers into binary.** Decimal numbers (the numbers we use in everyday life) can be converted into binary numbers. Imagine we have number 47 and we need to find its equivalent in binary system. We should divide it by 2, store the remainder and divide it by 2 until the quotient is 0. 
47 / 2   | 1
23 / 2   | 1
11 / 2   | 1
5 / 2    | 1
2 / 2    | 0
1        | 1
Starting from the last remainder and ending with the first we get the binary representation of the number 47. It is 101111.
The vice versa is also possible. 
**Converting binary into decimal.** Let’s take the same binary number and try to convert it to a decimal. 
We have 101111. We start to count the digits of this number from right to left starting from 0.
1  0  1  1  1  1
5  4  3  2  1  0
Then we write this numbers as powers of 2.
 1   0   1   1   1   1
2^5 2^4 2^3 2^2 2^1 2^0
Now we should multiple the digits of the binary numbers with the powers of 2 written below each of them and find the sum of those.
1 * 2^5 + 0 * 2^4 +1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1 * 2^0 = 32 + 0 + 8 + 4 + 2 + 1 = 47
**Addition of binary numbers.** Binary addition is almost the same as for the decimal numbers. The only difference is that there are only 0s and 1s. We should remember this in order to calculate:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10
If the number exceeds 1, we carry one to the next bit. Take this as an example:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogmzrx3rx0tnmsz5lmdw.png)

We start the addition from the right side and continue to the left: 1+1 is 0 and we carry 1, 0 + 1 is 1 and with the carry we write 0, and once more carry 1, 1 + 1 is 0 plus the carry is 1, so we write 1, then we carry 1, and 1 + 1 is 0 and with a carry is 11.
**Multiplication of binary numbers.** The binary multiplication is also similar to the usual multiplication. 
We should know that:
0 * 0 = 0
0 * 1 = 0
1 * 0 = 0
1 * 1 = 1
Let’s consider this example:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xj5vb2cx2yz5ocqgfs12.png)

First we multiplied each digit of the second number with the first number, but when we changed the digit each time we shifted left by one. Then we added the numbers together.
**Subtraction of binary numbers:** The subtraction of binaries can be with or without borrowing. Before getting to examples we should know the following:
0 - 0 = 0
0 - 1 = 1 (borrowing from the next higher digit)
1 - 0 = 1
1 - 1 = 0
Let's take a look at the subtraction without borrowing, which is basically a regular subtraction.

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

The next one is with borrowing.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvjxpipelxhp36ji2zi5.png)
First we start from the right, 0 - 0 is 0, for 0 - 1 we borrow 1 and get 1 (from 10 - 1), then in its left column there is left 0 - 0 which is 0 and, finally, 1 - 1 = 0.

**Division of binary numbers.** Like the other operations, it is not surprising that division is almost the same as for the decimals.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vbm7bf8yttk2x36qhlc.png)
We divide the number as we would do for any decimal number, and in the end we get the quotient(111) and remainder if exists (in our case 0).