Well its not just binary arithmetic. When we subtract any number from the other, lets say 7-2, we can proceed as same. 10's complement of 2 is 8. We add 8 to 7 and get 15. As we have a carry, we neglect it and the answer is 2.
What I do is add (-2) to 7. (-2) is actually 0-2. The question is how do I get 0-2? I assume a carry and perform 10-2. I get 8. Thus I assume 0-2 is 8. I add 8 to 7 and I get 15. The 1 is the carry which I had assumed which was initially not there. So I discard it and I get 5.
Now suppose I have to perform 7-8. 10's complement of 8 is 2. We add 2 to 7 and get 9. As the carry is absent, we take the negative of 10's complement of 9 which is (-1). (-8) is actually 0-8. which is 2 (I have assumed the carry). I add 2 to 7 and get 9. Now I have to take back the carry. So I subtract 10 from 9 and I get -1.
Thus the 2's complement method of binary subtraction is actually the basic interpretation of elementary subtraction learnt in schools.
Please review it and correct if I am wrong somewhere
now the no again starts from 0 because then the no chages to (10000) base 2 where the msb 1 is neglected as overflow as we have 4 bit ops now we need to full fill one property that is
-x+x = 0
so this points that -1 has to be 1 unit behind 0 -2 has to be 2 units behind 0 -3 has to be 3 units behind 0 so this 0 cannot be the starting zero as we have the starting point so this has to be the 2nd zero
so the no line will be
1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 ... so the above no line then compared with the no line of unsigned i.e. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 so unsigned(15) = signed(-1) unsigned(14) = signed(-2)
in general unsigned (2^n - x) = signed(-x) as 2^n = 0 where n is the number of bits
in short 2^n - x is nothing but the 2's compliment of x
(2^n - 1 - x) = 1's compliment
we can also carry out the subtraction by 1's compliment i.e.
x-y = 1111-(x - (1111-y))
but then we do
x-y = 10000-(x+(10000-y)) because subtracting the last no from 10000 is nothing but neglecting the carry so x-y = neglect carry(x+(10000-y))
where 10000 is the 16
x-y = neglect carry(x+2's compliment(y))
the same explanation holds for every base as explained by maulik
the example quoted by maulik covers all the explanation and is perfect
Please do reply
ReplyDeleteWell its not just binary arithmetic. When we subtract any number from the other, lets say 7-2, we can proceed as same. 10's complement of 2 is 8. We add 8 to 7 and get 15. As we have a carry, we neglect it and the answer is 2.
ReplyDeleteWhat I do is add (-2) to 7. (-2) is actually 0-2. The question is how do I get 0-2? I assume a carry and perform 10-2. I get 8. Thus I assume 0-2 is 8. I add 8 to 7 and I get 15. The 1 is the carry which I had assumed which was initially not there. So I discard it and I get 5.
Now suppose I have to perform 7-8. 10's complement of 8 is 2. We add 2 to 7 and get 9. As the carry is absent, we take the negative of 10's complement of 9 which is (-1). (-8) is actually 0-8. which is 2 (I have assumed the carry). I add 2 to 7 and get 9. Now I have to take back the carry. So I subtract 10 from 9 and I get -1.
Thus the 2's complement method of binary subtraction is actually the basic interpretation of elementary subtraction learnt in schools.
Please review it and correct if I am wrong somewhere
i want to know that this could be done by 9's compliment also but why in memory we represent negative nos with 2's compliment
ReplyDeletethe explanation is amazing
and our approach is a bit same
yes maulik is absolutely right with his explanation
ReplyDeleteNow lets start with 4 bit nos
with 8 bit we have 0-15 nos possible
but if we need to include negative nos there must be a decrease of magnitude3 variation
that is we need to decrease the magnitude
now so for symmetry we make number of negative = number of non negatives
so the range becomes -8 7
but we cannot change the symbols or the values which are assigned to positive nos as there will problems when dealing with arithmetic ops
so now lets start plotting a no line
unsigned
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3..
signed
0 1 2 3 4 5 6 7 ? ? ? ? ? ? ? ? 0
now the no again starts from 0 because then the no chages to (10000) base 2 where the msb 1 is neglected as overflow as we have 4 bit ops
now we need to full fill one property that is
-x+x = 0
so this points that -1 has to be 1 unit behind 0
-2 has to be 2 units behind 0
-3 has to be 3 units behind 0
so this 0 cannot be the starting zero as we have the starting point
so this has to be the 2nd zero
so the no line will be
1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 ...
so the above no line
then compared with the no line of unsigned
i.e.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0
so unsigned(15) = signed(-1)
unsigned(14) = signed(-2)
in general unsigned (2^n - x) = signed(-x)
as 2^n = 0
where n is the number of bits
in short 2^n - x is nothing but the 2's compliment of x
(2^n - 1 - x) = 1's compliment
we can also carry out the subtraction by 1's compliment
i.e.
x-y = 1111-(x - (1111-y))
but then we do
x-y = 10000-(x+(10000-y))
because subtracting the last no from 10000
is nothing but neglecting the carry
so
x-y = neglect carry(x+(10000-y))
where 10000 is the 16
x-y = neglect carry(x+2's compliment(y))
the same explanation holds for every base as explained by maulik
the example quoted by maulik covers all the explanation and is perfect
thanks to maulik for participating and giving such a perfect explanation
ReplyDelete