Martin Kirk (mlk@asu.edu) wrote: >Here is my situation. The input data is 8-bit, two's complement >data. I need to multiply two inputs together then multiply the result >by 8 and still get the result to fit in 16 bits. > >As I see it the max 8-bit value can be FFh (-1). In order to >make the multiply work for the 16-bit math I need to set all upper 8 >bits (I think) so that my multiplier data is FFFFh. This will cause an >overflow if I have two such values to multiply so I was going to limit >one of my inputs to 5 bits (ie, max value = 1Fh). But if I do this how >do I use this 5-bit representation of -1 in the 16-bit adder??? Martin: The two's-complement representation was designed to solve exactly your problem. If you're using narrow numbers (with a small number of digits) and want to translate them to wider numbers (with more digits), all you have to do is "sign-extend" your numbers. That is, replicate the sign bit (the narrow representation's leftmost bit) to the left, filling all the "extra" bits in the wider representation. In your case, you want to extend 5-bit numbers in the range [-16 - 15] to 16-bit numbers. Here... Take a look at the 5-bit and 16-bit representations of some numbers in that range: Decimal 5-Bit Binary 16-Bit Binary ------- ------------ ------------- 0 00000 00000000 00000000 1 00001 00000000 00000001 10 01010 00000000 00001010 15 01111 00000000 00001111 -1 11111 11111111 11111111 -10 10110 11111111 11110110 -15 10000 11111111 11110000 As you can see, bit-positions 5-15 in the 16-bit numbers contain the same value (0 for positive numbers, 1 for negative) that's in bit 4. [If you're REAL unfamiliar with binary notation, I guess I should tell you that the bits are numbered from right to left, starting with bit number 0. If you're not, I apologize for talking down to you.] If you were writing your own math routines or were more comfortable with binary math, I'd recommend that you write your multiply routine to use the 5-bit value directly; the resulting code would be shorter and would execute more quickly. However, since you're using pre-packaged routines, just extend the sign bit into the upper bits before you do the multiplication, and you'll be fine. -Andy -- Andrew Warren - fastfwd@ix.netcom.com Fast Forward Engineering, Vista, California