1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Multiplying two's complement numbers

  1. Feb 15, 2015 #1

    Zondrina

    User Avatar
    Homework Helper

    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. jcsd
  3. Feb 15, 2015 #2

    jedishrfu

    Staff: Mentor

  4. Feb 16, 2015 #3

    Zondrina

    User Avatar
    Homework Helper

    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.
     
  5. Feb 16, 2015 #4

    Svein

    User Avatar
    Science Advisor

    I have written some non-standard multiplication routines, and I prefer to remember the signs, convert to positive numbers, multiply and then convert back.
     
  6. Feb 16, 2015 #5

    Zondrina

    User Avatar
    Homework Helper

    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.
     
  7. Feb 16, 2015 #6

    Svein

    User Avatar
    Science Advisor

    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.
     
  8. Feb 16, 2015 #7

    Zondrina

    User Avatar
    Homework Helper

    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.
     
  9. Feb 16, 2015 #8

    jedishrfu

    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
     
     
  10. Feb 16, 2015 #9

    Zondrina

    User Avatar
    Homework Helper

    Thank you for all the suggestions everybody, I see there's more than one way to do this sort of problem now.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted