Binary numbers
Published: 2023-09-13 19:57:43

Hi everyone! In this post, we will learn about one of the most basic yet crucial topics in Computer Science: binary numbers. Let us get started.
ccc
We will discuss
* What are binary numbers?
* Where do we use them?
* How to convert binary to decimal and vice versa?
* How to convert decimal to any numeric system?
* How to do arithmetic operations with binary numbers?

What are binary numbers?
Binary numbers are representations of decimal numbers in base two; in other words, they are a combination of 0s and 1s. Each digit is referred to as a bit. For example, in 101, each of the 1s and 0s is one bit. To find out the maximum decimal number we can store in n number of bits, we use the following formula: 2^n - 1. For example, in 1011, the greatest integer we can store is 2^4 - 1.
The base 10 is the most understandable and commonly used numerical system humans use, whereas computers store and manipulate information by calculating 0s and 1s. The CPU(Central processing unit) is responsible for conducting these computations constantly and quickly.  



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

For instance, let us take the number 101101. In order to convert it, we must remember that we are counting from right to left, starting from zero. So, we have to take 2 to the power of the bit's position and multiply it by the value of the bit. As we start counting from the right-hand side, the position of 1 is 0, so we write 2 to the power of 0 and multiply it by 1. Next, we write 2 to the power of 1 multiplied by 0 until we reach the last position. Ultimately, we add everything to get our final decimal number. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scsp5udeebl4dtobs6tt.png)
The easiest way to do the opposite is to divide our base 10 number by 2 and carry the remainder, as shown in the example. For collecting the base 2 form, we must write down the exact order of the remainders from bottom to top. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ckwnabgkfhuefw1kaz1j.png)
Conversion between any other numeric systems is no different from binary. For example, if we want to convert decimal 73 into base 4, we would follow the same steps of dividing, carrying remainders, and collecting our number from the bottom to the top. And if we want to convert base 4 to decimal, we should again compute the sum of the 4s powered by their position and multiplied by the digit.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plhqib2tjif78jrqn3cp.png)
However, the approach would differ if we want to convert binary into base 4. In that case, we group the number into pairs of 2, as shown below. Next, we do the same calculations with positions and collect our final number. When converting to an octal system, we would group them into 3. Why?. The obvious pattern is that 4 is 2^2; hence, we group in 2, and 8 is 2^3, so we group in 3.

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

**Adding** two binary numbers together involves using the binary addition operation, similar to adding decimal numbers. Binary numbers are based on a system that only uses two digits: 0 and Here's how the process works:

1. Start by adding the rightmost bits of the two binary numbers, just like when adding the rightmost digits in decimal addition. The possible combinations are: 
0+0=0,
0+1=1, 
1+0=1, 
1+1=10 (which is 2 in binary).
2. If the sum of two bits in a column is 2, write down 0 in that column and carry over a 1 to the next column to the left.
3. Repeat this process from right to left, column by column, adding and carrying over as necessary until all the bits in both numbers are added.
4. If there is a carry left after adding the leftmost bits, write it down as an additional digit on the left side of the result.
5. The binary addition results in the series of digits written down from right to left.
Here is an example:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5jos981fupytanv44kz.png)

Subtraction of binary has certain rules too:
0 - 0 = 0
0 - 1 = 1, borrow 1, resulting in -1 carried over
1 - 0 = 1
1 - 1 = 0
Here's the guide
1. First, write down both binary numbers and align them vertically. If one number is shorter than the other, add leading zeros to it to make them the same length.
2. Start from the right and work your way to the left.
3. For each pair of corresponding bits, subtract the bit from binary A from the bit from binary B. If the bit from A is smaller than from B, you must borrow from the next higher bit in A.
4. When you need to borrow, subtract 1 from the next higher bit in A.
5. Continue this process for each pair of bits, moving from right to left. Carry over any borrows as needed.
6. The result is the series of digits you've written down from right to left.

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

 Here's a simple explanation of how multiplication works with binary:
1. Write down both binary numbers, one above the other.
2. Multiply the rightmost digit of the bottom number by each digit of the first, starting from the right. Write the results underneath the corresponding digits, shifted to the left according to their position.
3. Sum up all the rows to get the final result.

  110
X 
  100
  ——–
  000
 000
110
——————
11000
———