David613 wrote:This may be more depressing than funny but I've found log entries at work that include -0 (negative zero, not indicating subtraction) and -2-1=1. It has given one of my co-workers and I some amusement deciding whether zero is positive or negative.
Several, if not most of the ways to represent signed numbers result in having a representation for negative zero.
one's-complement is just an inversion of all the bits. So 11111=-0 and 000000=+0. Two's complement (invert the bits and add 1) has replaced one's-complement in most places, certainly where arithmetic is involved because there is more fiddling to do to get it to come out right.
Floating point numbers use sign-and-magnitude where a bit indicates if the number is positive or negative and the rest just gives the number. So if the sign bit is 1 the number is negative, even if its magnitude is 000000.
In both cases usually hardware will "normalise" the number before you have to deal with it, and that includes converting -0 to +0.
And in any case, a test for -0==+0 will always return true... unless you are comparing the bit pattern of course... So you need two comparison instructions, which is another reason those forms are rarely used.
Free interesting fact: In two's complement, you can imagine the most significant bit is negative and the others are positive. So for example with a four-bit word, bit 0 represents +1, Bit 1 is +2, Bit 2 is +4, but bit 3 is -8. So 1111 is 1+2+4-8 = -1. Whereas 1000 is just -8 and 0111 only adds up to 7, which explains why it's always asymmetric; with 16 bits the range is -32768 to +32767 and so on. And of course that extra negative number is where the space for -0 went to. One's complement is symmetric but has two zeros.