On Friday, April 13, 2007 9:39 AM [GMT-3=CET], Gerhard Fiedler wrote: > Harold Hallikainen wrote: > > > Something I read in BYTE magazine MANY years ago, that I have not seen > > published elsewhere, is, I think, much simpler. > > > > When using twos complement, make the sign of the weight of the msb > > negative. Then add up bits times weights, as usual. > > > > For example, in an 8 bit number, the msb weight becomes -128 instead of > > 128. So, 10101010 is: > > > > 1*(-128)=-128 > > 0*64=0 > > [...] > > and -128+32+8+2=-86. > > This is a derivative of the original rule of what two's complement means. > It's simply "folding" the values around at 0, which is equal to subtracting > 256 if the value is over 127. This can be achieved by converting the number > into decimal as if it were unsigned, then subtracting 256 (or whatever your > range is) if it's outside the positive range: > > 0b10101010 = 2+8+32+128 = 170 (for 0b10101010 being unsigned) > 0b10101010 = 170-256 = -86 (for 0b10101010 being two's complement) > > Gerhard ...or sublw 0 ;take the two's complement (256 range -128 +127) -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist