Hacker News new | ask | show | jobs
by white-flame 3450 days ago
Looking at limited-digit odometer style devices gives the best example of why 2's complement is saner.

  ...    or  ...
  9998       1110
  9999       1111
  0000       0000
  0001       0001
  0002       0010
  ...
What number is before 0 in binary? 1111. So that's where -1 is. The number before that? 1110, so that's -2. The whole XOR + 1 thing can be derived from this shape.

That, and simple addition of both signed and unsigned numbers actually works. :)

The only question is where you draw the line between underflowing negatives and overflowing positives, and going halfsies on the top bit seems to make sense. For an 8-bit number, there are 128 numbers on each of the negative/non-negative split, but zero mucks it up by being not mathematically positive. 1's complement evens it out by having 127 numbers on each side plus two zeros, but messes up signed math.