Multiplying two's complement numbers

  • Thread starter Thread starter STEMucator
  • Start date Start date
  • Tags Tags
    Numbers
AI Thread Summary
The discussion centers on the challenges of multiplying two's complement numbers, specifically the multiplication of an 8-bit number A (01100101, or +101) by -3 (represented as 11111101). The original poster struggles with sign extension and arithmetic overflow when trying to compute the product in 8 bits, realizing that the result (-303) cannot be represented within that range. They explore different methods, including sign extending to 10 and 16 bits, but still encounter issues. A suggested approach involves calculating the positive product first, then applying two's complement to find the negative result, which ultimately resolves the confusion. The conversation highlights the importance of understanding bit representation and overflow in two's complement arithmetic.
STEMucator
Homework Helper
Messages
2,076
Reaction score
140

Homework Statement



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##.

Homework Equations

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?
 
Physics news on Phys.org
jedishrfu said:
Here's a wiki article on it which you may have seen. It includes a section for multiplying two numbers too.

http://en.m.wikipedia.org/wiki/Twos_complement

What did you get for two complement for -3?

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) won't 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.
 
I have written some non-standard multiplication routines, and I prefer to remember the signs, convert to positive numbers, multiply and then convert back.
 
Svein said:
I have written some non-standard multiplication routines, and I prefer to remember the signs, convert to positive numbers, multiply and then convert back.

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.
 
Zondrina said:
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.
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.
When finished: if (AnswerIsNegative) then Answer = -Answer.
 
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.
 
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:
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
 
Thank you for all the suggestions everybody, I see there's more than one way to do this sort of problem now.
 

Similar threads

Back
Top