On Fri, 5 Jun 1998 15:10:10 -0400 Barry Cooper writes: > I keep running across references to 2's complements, does anyone >have a >relatively simple explanation? > > Thanks, Barry > 2's complement notation is one way of writing an integer (positive and negative). It is about the same as driving your car backwards and seeing what happens to the odometer. If you drive backwards one mile (or kilometer), it reads 99999 (or thereabouts). This represents negative 1. If we add 00001 to it, we get 0, which is what we expect. In a byte, we "drive backwards" from 00 to FF for negative 1, FE for negative 2, etc. In decimal, we can calculate a nine's complement of a number by subtracting the number from 99999 (or however many digits we're dealing with). This works out to be 9 minus the corresponding digit. For example, the 9's complement of 1234 is 9-1=8 9-2=7 9-3=6 9-4=5 So, the 9's complement of 1234 is 8765. If we add 1 to this, we get the TENS COMPLEMENT, in this case 8766. This corresponds to -1234 in tens complement notation. Let's try it out! How about 1236+ (-1234) We get 1236+8766=(1)0002 We throw out the 1 that overflows into the carry. So, the answer is 2! We do the same thing in binary... We first find the one's complement, then add 1 to the result to get the two's complement. In fact, the instruction on the PDP-8 to get the two's complement was CIA (for complement and increment accumulator). The 1's complement is interesting. We just subtract the number we're interested in from 11111111 (or however many bits we're using). Turns out that for each bit, there are only two choices (this is binary!). If we subtract 0 from the 1, we get 1. If we subtract 1 from the 1, we get zero. We can do the subtraction (to get the 1's complement) really easily by just inverting all the bits (changing 1's to 0's and vice versa). This is the COMPLEMENT instruction. if we then add 1, we get the two's complement, which is the negative of the number we started with (in 2's complement notation). A few other interesting things about 2's complement... The most significant bit is a 1 if the number is negative. Thus it is called the sign bit. Our normal binary scaling for an 8 bit number, starting with the least significant bit, is 1, 2, 4, 8, 16, 32, 64, 128. In two's complement form, the scaling is 1, 2, 4, 8, 16, 32, 64, -128. So, if you've got some 2's complement number, it's easy to convert to decimal by using this scaling. Finally, a byte is a byte, and it can represent a bunch of different things. It's up to the programmer to decide what format the data is in. For example, 0x30 could represent the number 0 (in ascii), or could represent the number 48 (decimal) in unsigned binary or 2's complement binary, or could represent the number 30 (decimal) in packed BCD. It's up to the programmer to keep track of data types! Harold _____________________________________________________________________ You don't need to buy Internet access to use free Internet e-mail. Get completely free e-mail from Juno at http://www.juno.com Or call Juno at (800) 654-JUNO [654-5866]