CIS3355:
Business Data Structures |
Which is better: One's or two's complement? We already mentioned that both procedures work equally as well, but are somewhat different in how they function. We already saw that one of the reasons we use two's complement: To eliminate the -0: To illustrate some of the differences between one's and two's complement, we need to add some numbers together. Let's first add the values +56 and - 43 (The answer of course, should be 13). We already know the binary value of 56 (and -56; above). To find the value value of -43: Now, adding +56 and - 43: Which evaluates to:
That's obviously wrong !!! In one's compliment, if the result is a non-negative value, we need to add '1' to get the correct answer: Which evaluates to: 23 + 22 = 8 + 4 = 13 Why !!! Basically, as we saw, binary has two zeros (0): In order to eliminate one of them, we have to add '1'. Now, let's add +56 and - 43 using 2's complement 43: Which evaluates to: 23 + 22 = 8 + 4 = 13 So two's complement IS better !!! We'll see. Now let's add 43 and -56 (which of course is -13). First let's add using a one's complemented 56: As we know, whenever a value is negative, we again need to complement in order to make sense of the result: Which evaluates to: - (23 + 22 + 20) = - (8 + 4 + 1) = -13 (The correct answer). Now let's add the numbers using a 2's complemented 56: Again, whenever a value is negative, we again need to complement in order to make sense of the result: Which evaluates to: - (23 + 22 ) = - (8 + 4 ) = -12 Wait !! That's wrong !!! Don't forget, when we converted 56 to -56 in two's complement, we added '1' to the result: So now we have to subtract 1 to make it right ??? Sort of. We can actually get the correct answer by adding '1 after we complement: Which evaluates to: - (23 + 22 + 20) = - (8 + 4 + 1) = -13 (The correct answer). So, which is better: One's or two's complement? As we noted, neither is better, the just use different ways of complementing. Essentially the differences are:
In sum respects, the procedures are opposites. Some good references to help you better understand complementing include:
Some questions you should be able to answer:
|