How can 2’s complement behave as a negation of a number?
We have discussed about 1’s complement and 2’s complement.
In that article, I have told you that 2’s complement of a number is equal to
the negation of that number. This article describes how this is happened. For
more simplicity, I will first brief you about the working of 10’s complement in
decimal number system.10’s complement and 2’s complements are very similar. So,
let’s start –
Subtraction using 10’s complement in decimal number system
See the below example carefully. Here we have used two different
methods – (1) borrow method which is traditional method and (2) 10’s complement
method.
Ex-1: subtract the following two numbers: 47 – 20
Borrow method
Very simple right? Now see the more complex method.
10’s complement method
Step 1: Find the 9’s complement of the subtrahend (20 ).If
you do not know what 9’s complement is ,please see my article on it. The link
is given at the bottom of this article.
As 99 is the largest number among all 2 digit numbers, we
subtracted 20 from it to find the 9’s complement.
Step 2: Add 1 to the 9’s complement to find the 10’s
complement –
Step 3: Now add the 10’s complement of 20 to the minuend
( 47) to subtract and eliminate the most significant digit.The answer is ready!
27 is your answer. We have eliminated MSD(Most Significant Digit) because it is
similar to subtracting 100 from the sum 127. Confused? Why are we subtracting
100 from 127? There is nothing to get confused. It is same as writing this –
Ex-2: subtract the two number: 20-47
Borrow method:
How can you perform subtraction of the above two numbers?
Easy! It going to be -27.Now see the 10’s complement method –
10’s complement method
Step 1: find the 9’s complement of the subtrahend :
Step 2: find the 10’s complement of the subtrahend by adding
1 to the 9’s complement:
52+1 =
53
Step 3: Add the 10’s complement of 47 to 20:
53+20 =
73
73 is your answer! Here we did not subtract 100 from 73
because we do not want the minus sign. In 10’s complement method 10’s
complement of a number is behaved as the negative of that number. Did not make sense?
Let us move to binary number system and then you will understand the logic.
Subtraction in binary number system using 2’s complement
I have already told you in my previous article that
preprocessors hate subtraction (building logic gates for subtraction is little
bit more complex than building logic gates for addition).Wouldn’t it be so good
if preprocessors perform subtraction using only addition? Seems interesting!
This is achieved by using 2’s complement method. As I told you 10’s complement
and 2’s complements are similar, we will follow same procedure here to find the
subtraction. let us see an example –
Ex-3: Perform
subtraction of the two numbers 1111 – 1010:
Direct method is similar to borrow method.So, I will not
explain it. Let’s see the steps for 2’s complement method
Step 1: Determine the 2’s complement of the subtrahend by
finding 1’s complement of that number and adding 1 to it.
Step 2: Add the minuend (1111) to the 2’s complement of
subtrahend (0110 )
Step 3: drop the most significant bit ( better say the carry
). Thus, the answer is (0101)₂
Another example –
Ex-4: subtract 43 from 19 :
Now binary representation of 19 is (0001 0011)₂ and binary representation of 43 is (0010 1011)₂.Follow the same steps –
Step 1: determine the 2’s complement of the subtrahend
Step 2:add minuend to it.
2’s
complement of 43 is 1101 0101
0001
0011 + 1101 0101 = 0011 1110
The answer is (0011 1110)₂ which is 62 in decimal number
system. Computer will store it as negative of that result. In the entire
subtraction we did not really use subtraction. Besides, we used addition. That
is why preprocessors use 2’s complement method. In the upper example we did
not omit the MSB because there is no carry.
When you tell your computer to subtract a number from another, computer first convert the subtrahend to its 2’s complement and then addition
is performed and the carry is omitted from the result. This result is internal
and may not be equal to the number you expect. Note the result is generated
internally,you cannot see it. But computer will give you the answer which you
expected.
How can computers differentiate a number and 2’s
complemented number?
In the above example we see that (0011 1110)₂ is the answer
which is same as 62.But, this 62 is not same as the ordinary 62.It behaves as a
negative number with the same magnitude we expect. But how can computers
differentiate them? It is simple. Computer knows which is an ordinary number
and which is the 2’s complemented number.
I hope you do not have any doubt. If you want to see my
previous article on 1’s complement and 2’s complement, here is the link - what are 1's complement and 2's complement?
Comments
Post a Comment
If you have any doubt please comment and let me know