Basic arithmetic (Addition, Subtraction) with Binary numbers (Quick Notes)
Today, I was asked to demonstrate basic arithmetic (addition & subtraction) using binary numbers to some 1st-year students.
Surprisingly, my google foo failed me and I couldn't quickly find a decent tutorial. Even more surprisingly, within 2 minutes of grabbing a pen & paper, I actually remembered how I learnt to do it (almost 20 years ago).
So here are some quick notes on how to add and subtract binary numbers...
- Addition
Addition is actually very easy, you just have to remember the basic sums
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 --> carry 1
So let's see a random example step by step:
0 1 1 0
+ 1 0 1 1
---------------
Like with the addition of decimal numbers we start from the right and move to the left:
(0 + 1 = 1)
0 1 1 0
+ 1 0 1 1
---------------
1
(1 + 1 = 0 --> carry 1)
0 1 1 0
+ 1 0 1 1
---------------
0 1
(1 + 0 + 1(carry) = 0 --> carry 1)
0 1 1 0
+ 1 0 1 1
---------------
0 0 1
(0 + 1 + 1(carry) = 0 --> carry 1)
0 1 1 0
+ 1 0 1 1
---------------
0 0 0 1
And finally for the remaining carry: (0 + 1(carry) = 1)
0 1 1 0
+ 1 0 1 1
---------------
1 0 0 0 1
You can easily verify the above result either using a binary enabled calculator (such as the one in Windows) or by converting the numbers to decimal ( 6 + 11 = 17)
- Subtraction
Subtraction in binary is much more interesting. My preferred method is doing it using two's complements. However, this method can easily confuse someone for the first few times, because it works like magic. That's why I like first to demonstrate the complements method using decimal numbers.
2.1 Complements method using decimal numbers.
The complements method converts a subtraction to an addition. Let's examine two numbers 337 and 2017. We'll examine both possible subtractions, i.e. 2017-337 = 1680 and 337-2017 = -1680 (which gives a negative result).
2.1.1
Let's examine the subtraction 2017-337
We first make sure that both numbers have the same number of digits (add leading zeros if needed)
2017-337 --> 2017 - 0337
Then we compute the complement of the second number. In this case we are in the decimal system, so to compute the 9-complememt of 0337, we subtract each digit from 9: 0337 ---> 9662
The we add +1 to the 9-complement to get the 10-complement: --> 9662 + 1 = 9663
We add the 10-complement to the first number to get: 2017 + 9663 = 11680.
And finally we omit the extra leading 1 to get 1680 which is our result
2017-337 = 1680
2.1.2
Let's examine the subtraction 337 - 2017
337 - 2017 --> 0337 - 2017
We start with same steps:
2017 -----> 7982 (9-complement) -----> 7983 (10-complement)
0337 + 7983 = 8320
In this case, our result doesn't have a extra leading 1. This means our result is negative, and equals to the 10-complement of the number we got.
8320 -----> 1679 (9-complement) -----> 1680 (10-complement) <----- So our result is -1680 (exactly the same you would get using a calculator)
2.2
Now that we have demonstrated the complements method in decimal, it makes it more intuitive to use it in binary subtraction. In binary, we have the one's-complement (1-complement) and the two's-complement (2-complement). 1-complement is computed with same reasoning as with the decimal system. But, whereas in the decimal system there are 10 digits (and you have to subtract from 9), in the binary system the are two digits and you subtract from 1.
And this is essentially the same as flipping 0-->1 and 1-->0.
So to compute the 1-complement of a binary number just write 1's for 0's and vice versa.
And to compute the 2-complement just add +1 to the 1-complement.
Combining with the reasoning for the decimal system the steps are:
a. Make sure that both numbers have the same number of digits (add leading zeros if needed)
b. Compute the two's complements for the second number
c. Add the number from step b. to the first number
d1. If in c. you get an extra leading 1 digit, drop it to get the exact result (which is positive)
d2. If in c. you you don't get an extra digit, then the result is negative and equal to the 2-complement of c.
2.2.1 Example:
1011 - 11
a. 1011 - 11 --> 1011 - 0011
b. 0011 ---> 1100 (1-complement) ---> 1101 (2-complement)
c. 1011 + 1101 = 11000 (One extra leading 1)
d1. We drop the extra leading 1 to get our result (+1000)
(Verify that 11 - 3 = 8)
2.2.2 Example:
11 - 1011
a. 11 - 1011 --> 0011 - 1011
b. 1011 ---> 0100 (1-complement) ---> 0101 (2-complement)
c. 0011 + 0101 = 1000 (No extra leading 0 - therefore negative result)
d2. 1000 ---> 0111 (1-complement) ---> 1000 (2-complement)
Therefore the result is -1000 (-8)
(Verify that 3 - 11 = -8)
Congratulations @dev-man! You have received a personal award!
1 Year on Steemit
Click on the badge to view your Board of Honor.
Congratulations @dev-man! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!