Negative Numbers using Two’s Complement
A colleague asked me about question (15) in my Uber Geek Challenge post so I thought I'd provide a simplistic explanation as I don't think Wikipedia gives such.
Basically, there are two popular ways of storing negative numbers in a binary number system: one's complement and two's complement. The easiest to understand is, arguably, one's complement. To get the negative of a given binary value when using one's complement you simply flip all the bits. However with zero represented by all zeros in binary, flipping all bits results in all ones and this is interpreted as "-0". Having two values for zero (one positive and one negative) isn't desirable so the alternate approach, two's complement, flips all the bits then adds 1. That ensures you don't get a positive and negative zero, and the only price you pay is a slight asymmetry in the numeric range - one less positive number if you don't count zero as positive.
The image on the right shows the binary representation of 8-bit negative numbers and their positive counterparts.
14 Jan 2010 Damien Wintour 0 comments







