Multiplying two's complement numbers

  • Thread starter STEMucator
  • Start date
  • Tags
    Numbers
In summary, the conversation discusses how to perform a computation involving two's complement numbers, specifically multiplying an 8-bit two's complement number by another number. The conversation includes suggestions such as doing the positive version of the multiplication and then finding the two's complement of the answer, as well as using a non-standard multiplication routine by remembering the signs and converting the numbers to positive before multiplying. It is also mentioned that using a higher number of bits can help avoid arithmetic overflow.
  • #1
STEMucator
Homework Helper
2,076
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
  • #3
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.
 
  • #4
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
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.
 
  • #6
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.
 
  • #7
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
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
 
  • #9
Thank you for all the suggestions everybody, I see there's more than one way to do this sort of problem now.
 

FAQ: Multiplying two's complement numbers

What is a two's complement number?

A two's complement number is a binary number representation used in computers to represent both positive and negative integers. In this system, the most significant bit (MSB) is reserved as the sign bit, with 0 indicating a positive number and 1 indicating a negative number.

How do you multiply two's complement numbers?

To multiply two's complement numbers, you can use the same process as multiplying regular binary numbers. First, you multiply the digits as you would in a regular multiplication problem. Then, you add any necessary carry bits from the previous step. Finally, you take the result and perform a two's complement conversion to get the final answer.

What happens when you multiply two's complement numbers with different sign bits?

If you multiply two's complement numbers with different sign bits, the result will have a negative sign. This is because the product of two negative numbers is always positive, but in the two's complement system, a positive result is represented by a negative sign bit.

Can two's complement numbers be multiplied without converting them first?

Yes, two's complement numbers can be multiplied without converting them first. However, the result may need to be converted back to two's complement form in order to properly represent negative numbers. Converting the numbers beforehand can make the process easier and prevent errors.

Are there any special cases to consider when multiplying two's complement numbers?

Yes, there are a few special cases to consider when multiplying two's complement numbers. First, if the two numbers being multiplied have the same sign bit, the result will always be positive. Second, if one of the numbers is 0, the result will always be 0. Lastly, if one of the numbers is the minimum value for its bit size (-128 for an 8-bit number), the result will always be a negative number.

Similar threads

Replies
2
Views
2K
Replies
24
Views
6K
Replies
5
Views
1K
Replies
6
Views
18K
Replies
8
Views
2K
Replies
4
Views
3K
Replies
6
Views
9K
Back
Top