views:

204

answers:

3

i know unsigned,two's complement, ones' complement and sign magnitude, and the difference between these, but what i'm curious about is:

  1. why it's called two's(or ones') complement, so is there a more generalize N's complement?
  2. in which way did these genius deduce such a natural way to represent negative numbers?
A: 

2's Complement: Because there are 2 steps. 1) Flips the bits 2) Add 1 :)

Shadow
serious ?.......
mochidino
-1, Not Useful. Barely even humourous, in fact.
staticsan
+13  A: 

Two's complement came about when someone realized that 'going negative' by subtracting 1 from 0 and letting the bits rollunder actually made signed arithmetic simpler because no special checks have to be done to check if the number is negative or not. Other solutions give you a discontinuity between -1 and 0. The only oddity with two's complement is that you get one more negative number in your range than you have positive numbers. But, then, other solutions give you strange things like +0 and -0.

According to Wikipedia, the name itself comes from mathematics and is based on ways of making subtraction simpler when you have limited number places. The system is actually a "radix complement" and since binary is base two, this becomes "two's complement". And it turns out that "one's complement" is named for the "diminished radix complement", which is the radix minus one. If you look at this for decimal, the meanings behind the names makes more sense.

Method of Complements (Wikipedia)

staticsan
the only thing left to add is that, like with CPU register having fixed numbers of bits, to generalize to radix N, you have to work within a fixed number of digits.
JustJeff
+3  A: 

You can do the same thing in other bases. With decimal, you would have 9's complement, where each digit X is replaced by 9-X, and the 10's complement of a number is the 9's complement plus one. You can then subtract by adding the 10's complement, assuming a fixed number of digits.

An example - in a 4 digit system, given the subtraction

 0846
-0573
=0273

First find the 9's complement of 573, which is 9-0 9-5 9-7 9-3 or 9426
the 10's complement of 573 is 9426+1, or 9427
Now add the 10's complement and throw away anything that carries out of 4 digits

   0846
  +9427      .. 10's complement of 573
= 10273      .. toss the 'overflow' digit
=  0273      .. same answer

Obviously that's a simple example. But the analogy carries. Interestingly the most-negative value in 4-digit 10's complement? 5000!

As for the etymology, I'd speculate that the term 1's complement is a complement in the same sense as a complementary angle from geometry is 90 degrees minus the angle - i.e., it's the part left over when you subtract the given from some standard value. Not sure how "2's" complement makes sense, though.

JustJeff
"2's complement" because it's in base 2. The general term is "radix complement".
dan04
That's really odd, why call the N's complement the result of subtracting the digit from N-1? As opposed to subtracting it from N.
phkahler
@phkahler: Because in base N, subtracting each digit from N-1, and then adding 1, corresponds exactly to subtracting the number from an appropriate power of N. For example, the 10's complement of 0573 is 9426+1=9427, which is precisely 10000-573. This is also why the method works: 846-573 = 846+(10000-573)-10000. [Trivia: This corresponds to a rule known as "all from nine and the last from 10" in the so-called "Vedic mathematics": http://en.wikipedia.org/wiki/Vedic_Mathematics ]
ShreevatsaR