I have 2's Complement digit range from negtive to positive. How to multiply it with a constant?
Now, I have to turn 2's complement to signed-digit format and do shift-addition/multiplication.
And there is a big problem confused me a long time: Is there any method have the merits from signed-digit(easy to multiply) and 2's complement(easy to add)?
Any suggestions will be appreciated! Best regards, Davy
Didn't find your answer? Ask the community — no account required.
S
slebetman
I'm assuming doing this on platforms without a hardware multiplier or a multiply instruction. Otherwise multiplying with a constant is the same as multiplying with a variable.
In which case, I don't get it. 2's complement is as easy to multiply as it is to add. There's no need to convert it to signed digit first.
Lets look at some examples of 2's complement multiplication of negative numbers, we'll stick to 8 bits to make it short:
-1 * 2 = -2 11111111 (-1, shift left by 1 to multiply by 2) 11111110 (equals -2)
-5 * 4 = -20 11111011 (-5 shift left by 2 to multiply by 4) 11101100 (equals -20)
-3 * 5 = -15 11111101 (-3 shift left by 2 to multiply by 4) 11110100 (equals -12 add -3) 11110001 (equals -15)
In none of the examples above do I need to convert to signed digit. If multiplying by a negative constant, then simply use the positive version of the constant and 2's complement the variable before multiplying (in effect multiplying it by -1):
1 * -2 = -2 00000001 (1, 2's complement to multiply by -1) 11111111 (-1, shift left to multiply by 2) 11111110 (equals -2)
-1 * -2 = 2 11111111 (-1, 2's complement to multiply by -1) 00000001 (1, shift left to multiply by 2) 00000010 (equals 2)
I don't get what you're complaining about.
K
Ken Smith
Imagine a 16 bit 2's compliment number. The LSB has a value of 1, the next is 2 ... etc ... 16384. The top bit has a value of -32768.
--
kensmith@rahul.net forging knowledge
R
RED
B
Ben Rudiak-Gould
Fixed-width 2's complement multiplication is the same as fixed-width unsigned multiplication. Just pretend you're doing unsigned multiplication; it'll work even if the value is negative.
(This does not apply if you want a result larger than the operands, though.)
-- Ben
C
Charles Marslett
And of course if you do want the double size product, just remember that the 2-s compliment number is the same as (-2^n)+unsignedNum, so you can just subtract the other number in the upper half of the product:
That is, if you have an unsigned product A*B=[C:D] with A, B, C and D all n bits long, if A is signed and B is not, the product is [C-B:D] and if both are signed, then it is [C-A-B:D].
--Charles
Join the Discussion
Have something to add? Share your thoughts — no account required.
Didn't find your answer?
Ask the community — no account required
Report Content
You are reporting this content to the moderators. They will look at it
ASAP.