DEV Community

Gevorg
Gevorg

Posted on

Operations with binary numbers!

This is a continuation to the video about binary numbers and their conversions. Now I am going to explain how to do basic mathematical operations with binary numbers.

Let's start with the addition of binary numbers. Suppose we have 1011 and 1110. in order to add these numbers together we write these two numbers below each other and start adding terms that are above each other from the right side, like with do with decimal numbers. The basic thing to keep in mind is that

1 + 0 = 1
0 + 1 = 1
0 + 0 = 1
1 + 1 -> in this case we write 0 in the column and carry 1 to the next column.
1 + 1 + 1 -> if we have three one's we write one of them in the same column and carry another one to the next column.

Here is an example below

  1011
+ 1110
-------
 11001
Enter fullscreen mode Exit fullscreen mode

1 column in pretty straight forward -> (1 + 0) = 1
2 column -> 1 + 1 = we write 0 below and carry 1 to the next column
3 column -> we have 1 + 0 here, but we should now forget about the 1 from the previous column, so again we get 1 + 1, which means 0 under this column and passing 1 to the next column.
fourth
4 column -> here we have 1 + 1, but we also have the one from the previous column, so as I mentioned one of the '1' goes under this column and the other one gets passed to the left side.

In conclusion we get 11001.

Now let's do the subtraction of binary numbers.

subtracting in binary is pretty simple.

1 - 0 = 1
1 - 1 = 0
0 - 1 = in this case we look at a one from the left side and we pass it to the right column by making it a 2.

Let's consider this example

 0101
-  11
-------
  010
Enter fullscreen mode Exit fullscreen mode

1 column -> 1 - 1 = 0
2 column -> 0 - 1 = we carry the 1 from the third column and make it into 2, so we can 2 - 1 = 1
3 column -> we passed the one from this column so we have 0 in here.

The result is going to be 010, or 10

Now let's look into binary multiplication.

this is exactly the same as we do with decimal numbers. we multiple the numbers like we do usually then we use the addition technique to add them together.

      1101
×     1110
-----------
+     0000
+    1101   
+   1101
+  1101       
-----------
  10110110
Enter fullscreen mode Exit fullscreen mode

Last but not least let's look into division.

Let's work on this example. Again we do this the same way we do with decimals. However, during binary division if our dividend is greater than the divisor we write 1 and subtract those two from each other to get the next number. If the dividend is smaller we write 0 and bring down the next digit of the divided until we get a bigger number.

 11001  |  101
-101    -------- 
 -----   | 101
  00101            
 -  101           
 ------- 
      0
Enter fullscreen mode Exit fullscreen mode

we start by taking the first three digits from 11001, and if this number (110) is bigger than our divisor (101), we are going to write 1 in the result section and we will subtract 101 from 110. After that we look at the number we got after subtraction which is 0010, since this number is smaller than 101 we can write a 0 in the result section. Then we drop the number from the next column which is 1. the number we get is 101. 101 = 101 so we add another 1 to the result section and after subtracting this numbers we get 0 remainder. Our answer is going to be 101.

This is going to be it for binary operations. Thanks for reading!!!

Top comments (0)