Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> There's also the notable feature of the most significant bit, which, if excluded from the usable range of numbers, becomes the sign-bit. If it is empty (clear), it must be a positive number, if it's set, we have a negative number (since it must be a clear bit flipped)

There also is the problematic feature that, according to that logic, zero is a positive number :-)

> Can we do something about this? Namely, can we get rid of negative zero? Well, as we've seen, we have an extra step on the negative side of things… what about just adding 1 to compensate for this? So, for -1, we wouldn't write 1110 (flipping all bits of 0001), but 1111? Just the same, for -2, we would add 1 to 1101, making this 1110, and so on. – Well, this works! we just eliminated negative zero! Welcome to two's complement.

I think that the “flip all the bits, add one” method to flip the sign in such a number came after the idea of using that mapping between numbers and bit patterns.

I’m thinking that because the mapping naturally comes from the observation that unsigned n-bit integers do math modulo 2^n, and that, thus, the all-ones pattern stands for

   {(2^n 1) + k 2^n | k ∈ ℤ}
⇒ it’s an arbitrary choice whether to pick 2^n-1 and -1 as representatives of that set.

⇒ the hardware for implementing addition, subtraction and multiplication can be 100% identical, whatever representatives you pick for those numbers.

One thing this loses compared to the ones’ complement is that it is harder to flip the sign of a number. However, it isn’t hard to discover the “flip all the bits, add one” method.

A thing that it doesn’t really lose is that this encoding has a ‘weird’ number. To flip the sign of 1000…000, you compute 0111…111 + 1, which is 1000…000, so in some sense, that’s a second number that is its own negative in this encoding.

That isn’t much different from two’s complement. It has two zeroes, this has one zero and another number that’s equal to its own negation.



I think, it may be worth keeping in mind how important it was to have an intuitively accessible representation in the era of console lights and toggle switches. Ones' complement and octal grouping (it's easy to interpret a triplet of bits) did this. – And subtraction was just addition with one of the operands inverted. On the other hand, you had to compensate for this by adding extra circuitry to handle transitions across zero (i.e., a change of the sign-bit occurring in additions or subtractions), and checking for -0 in code as well. As operator consoles became less important, this became less worth the effort and we transitioned. (Mind how the connection between octal digit positions and bits is lost in two's complement. This would have been a major issue.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: