3
\$\begingroup\$

The 2's complement binary multiplication does not have same procedure as unsigned if the both operands do not have the same sign. What is the logic behind that?

Does special consideration apply to division also when we carry out division with 2's complement numbers?

\$\endgroup\$
  • 2
    \$\begingroup\$ useful link (explains multiplication pretty well). tl;dr: there's a "brain-dead" way which always works, and then there's a faster way which requires special logic. \$\endgroup\$ – helloworld922 Nov 29 '16 at 2:46
  • \$\begingroup\$ Well, if you used the same procedure, the result would be wrong. \$\endgroup\$ – CL. Nov 29 '16 at 9:18
2
\$\begingroup\$

The 2's complement binary multiplication does not have same procedure as unsigned

In modulo 2n arithmetic -1 and 2n-1 are equivilent. It follows that if the output is the same size as the input then we can used a modulo 2n multiplier for both signed and unsigned operations.

However if the output is larger than the inputs this property no longer holds. Consider for example multipying the 8 bit number 11111111 (255 if interpreted as straight binary, -1 if interpreted as 2's complement) with itself to produce a 16 bit result. For signed numbers the correct result is 0000000000000001. However for unsigned numbers the correct result is 1111111000000001 (65025 in decimal)

If you want to think of this in modular arithmetic terms you can note that -1 and 255 are the same modulo 256 but different modulo 65536.

This is why when you look at (for example) the arm instruction set you see only one 32*32->32 multiply instruction but two different 32*32->64 multiply instructions.

Does special consideration apply to division also when we carry out division with 2's complement numbers?

Division (in the sense we think of it on computers) is not a modular arithmetic operation. So there is no reason to expect an equivilence between signed and unsigned division and indeed there isn't one.

Again to give an example consider 11111110 / 00000010 . In unsigned arithmetic this would result in 01111111 (127) in signed arithmetic it would result in 11111111 (-1)

\$\endgroup\$
0
\$\begingroup\$

Well perhaps you understand that twos complement makes addition and subtraction very nice, dont need to care about interpretation of the bit patterns as signed or not. Even better subtraction uses addition logic, invert the second operand and invert the carry in bit (invert and add one to negate).

But multiply and divide are a different story and it has to do with sign extension. And the simple nature of elementary math.

If I want to multiply two four bit numbers (the letters are bits)

    abcd
  * efgh
=========

To not lose any bits I am going to need at least 8 bits to store the result. To make it easier to see the second number is all ones, we dont know if that is positive or negative yet just demonstrates the problem using what we learned in grade school.

     abcd
  *  1111
=========
     abcd
    abcd
   abcd
+ abcd
==========
  xxxxxxx

when added together that is initially a 7 bit result but there exists the possibility of a carry making it need 8 bits, 0xF times 0xF unsigned is 0xE1 needs 8 bits as a result.

And here is the rub, if those are unsigned numbers then you pad abcd with zeros, if those are signed numbers then you pad with the sign, so if a is a one and unsigned then it looks like what you see above, if using signed numbers and a is a one then you have to

     abcd
  *  1111
=========
  111abcd
  11abcd
  1abcd
+ abcd
==========
  xxxxxxx

Or you can think of it as multiplying 1111abcd with the second four bit operand.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.