# Multiplying two's complement numbers

1. Feb 15, 2015

### Zondrina

1. The problem statement, all variables and given/known data

I'm a little confused by the wiki article, and I can't seem to get the correct answer.

Suppose $A = 01100101 = +101$ is an 8-bit two's complement number.

I'm trying to multiply $A \times 101 = 01100101 \times 101 = (+101) \times (-3) = -303$.

2. Relevant equations

3. The attempt at a solution

I needed to sign extend the $101$ term so I would be computing:

$A \times 11111101 = 01100101 \times 11111101 = (+101) \times (-3) = -303$.

When I multiply $01100101 \times 11111101$ using ordinary multiplication, I get something completely incorrect. Sign extending to 16-bits to fit the answer also seems impractical.

How would I perform a computation such as this one?

2. Feb 15, 2015

### Staff: Mentor

3. Feb 16, 2015

### Zondrina

I tried doing this multiplication:

$$\quad \quad \quad \space 01100101 \\ \quad \quad \space \times 11111101 \\ \quad \quad ------ \\ \quad \quad \quad \space \space 01100101 \\ \quad \quad \quad 000000000 \\ \quad \quad \space \space 0110010100 \\ \quad \quad 01100101000 \\ \quad \space \space 011001010000 \\ \quad 0110010100000 \\ \space \space 01100101000000 \\ 011001010000000 \\ --------- \\ \quad 111010001$$

Which is obviously not going to work, because the answer (-303) wont fit inside of 8 bits. 8 bits can only represent $[-128, 127]$, so there will be an arithmetic overflow.

I would need at least 10 bits to hold the answer properly. So I sign extended to 10 bits and tried multiplying these two numbers together:

$$0001100101 \times 1111111101$$

Which also yielded an incorrect answer for some reason. 16 bits also did not work.

4. Feb 16, 2015

### Svein

I have written some non-standard multiplication routines, and I prefer to remember the signs, convert to positive numbers, multiply and then convert back.

5. Feb 16, 2015

### Zondrina

So wait, you're saying if I simply do the positive version of the multiplication and then find the two's complement of the answer it will be equivalent.

I'll go try that out, ill post in a moment.

6. Feb 16, 2015

### Svein

Well, not quite. I introduce a logical variable AnswerIsNegative which is initially at false. Then I check the variables: if (A<0) then AnswerIsNegative = not AnswerIsNegative etc.

7. Feb 16, 2015

### Zondrina

Okay so I said $A = 01100101 = +101$ and $101 = -3$. We want to multiply $A \times 101 = (+101) \times (-3) = -303$.

Finding the two's complement of $101 = -3$ resulted in $011 = +3$.

I sign extended $A$ and $011$, and then performed the multiplication:

$$001100101 \times 0011 = 0100101111 = +303$$

Finding the two's complement of the result yielded $1011010001 = -303$. So clearly an overflow occurred.

8. Feb 16, 2015

### Staff: Mentor

I'd do the calculation in 16-bits because that will hold the answer and I would start with -3 and multiply it by 101 because there are less terms to add up.

1111.1111.1111.1101 x 0000.0000.0110.0101

It worked for me:
Code (Text):

1111.1111.1111.1101    x 0000.0000.0110.0101
-----------------------------
1111.1111.1111.1101 = x 0000.0000.0000.0001
1111.1111.1111.0100 = x 0000.0000.0000.0100
-----------------------------
1111.1111.1111.0001 (sum of previous two lines)
1111.1111.1101.0000 = x 0000.0000.0010.0000
-----------------------------
1111.1111.1100.0001 (sum of previous two lines)
1111.1110.1000.0000 = x 0000.0000.0100.0000
-----------------------------
1111.1110.1100.0001 = -3 x 101

9. Feb 16, 2015

### Zondrina

Thank you for all the suggestions everybody, I see there's more than one way to do this sort of problem now.