Source: unsplash.com |
The Binary subtraction of two numbers, as you know consists of adding one number to complement of the other. The complement can be a 2s or 1s complement.
Consider the subtraction with 2s complement. After adding with 2s complement of the other number, there are two cases:-
- If carry occurs, ignore it, and the answer is the difference required.
- If carry does not occur, find the 2s complement of the result to get the magnitude of difference, and add a negative sign to answer...
Now, we just go a little deep and try to make a sense of why these rules are framed...
Let A and B the two binary numbers of n bits, and we need to find A - B, which we do by finding 2s complementing of B and adding it to A
We can write the expression as:-
A + 2s complement of B
= A + (2n - B)
= A - B + 2n
Based on A is greater or lesser than B, we have two cases :-
Case I: If A > B
A - B + 2n
= (A - B) + 2n
= (required value) + 2n
Thus A - B + 2n will be nothing but just the difference plus 2n. But, 2n is an n+1 bit number, which starts with 1 (MSB) and has n zeros.
Ex: (23)10 = (1000)2, (24)10 = (10000)2, etc.
We can see that A and B are of only n-bits, and that means A-B will also be of n-bits. This value upon adding to the 2n value, we will get an n+1 bit binary with 1 as MSB and the difference filling the next zeros.
Ex: if n = 3, and A - B = 101
we can write that (2n)10 = (23)10 = (8)10 = (1000)2
then, A - B + 2n = 101 + 1000 = 1101
* similarly, for the same n, if A - B = 110
we can write that A - B + 2n = 110 + 1000 = 1110
* for n = 4 and A - B = 0100
then A - B + 2n = 0100 + 10000 = 10100
So yeah, if A > B, then for n-bit A and B, we are directly have the A - B value with an extra 1..
Case - II: Suppose consider that the A < B; here A - B gives a negative value. But B - A gives a positive number right..?
so it is like
A - B + 2n
= (A - B) + 2n
= - (B - A) + 2n
= 2n - (B - A)
= 2n - (some value)
If you observe, (2n - any value) is nothing but the 2s complement...
So, what we got is just the 2s complement of the difference.. wait, it's actually 2s complement of negative of difference (we actually got 2s complement of B - A, but we have to find A - B)
Another thing is, see that we are subtracting a smaller value (the difference) of an n-bit binary from a bigger value, i.e., 2n (an n+1-bit value) which can never produce a carry...
Observations:
In case I, where A > B, we observed a carry, which concludes that the difference is positive; the difference is simple, just neglect that extra carry and note down the remaining part...
In case II we did not get a carry, suggesting that the difference is negative; now to get the value, we need to do a 2s complement once again on the result.. (as it is in 2s complement form because of the operation we performed)
That's why we got such rules of checking carry and deciding whether a further 2s complement is required or not, and yeah, a similar analysis can be made when we use the 1s complement addition...
Thanks for reading...
Hope you understood the post, and you need not hesitate to inform us if there's any kind of error in the topic discussed...
If you also knew or came across any exciting concepts, make sure you share with us and other readers through the comment section, and help us reaching 'The Eternal Thing'...
Comments
Post a Comment