| Author |
Message |
Davy
Guest
|
Posted:
Wed Dec 14, 2005 8:07 am Post subject:
2's Complement Multiply with Constant |
|
|
Hi all,
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 |
|
| Back to top |
|
 |
Ken Smith
Guest
|
Posted:
Wed Dec 14, 2005 8:07 am Post subject:
Re: 2's Complement Multiply with Constant |
|
|
In article <1134526041.078378.185100@o13g2000cwo.googlegroups.com>,
Davy <zhushenli@gmail.com> wrote:
| Quote: | Hi all,
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)?
|
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 |
|
| Back to top |
|
 |
slebetman@yahoo.com
Guest
|
Posted:
Wed Dec 14, 2005 9:16 am Post subject:
Re: 2's Complement Multiply with Constant |
|
|
Davy wrote:
| Quote: | Hi all,
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)?
|
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. |
|
| Back to top |
|
 |
Guest
|
Posted:
Wed Dec 14, 2005 11:50 pm Post subject:
Re: 2's Complement Multiply with Constant |
|
|
Hi Davy,
Most processors have an assmebly instruction to multiply a signed
number (in your case 2's complement number) with an unsigned number.
It's the simplest way to implement. I am not sure if this is possible
in your case.
Are you implying that the above solution is not possible and hence you
are looking for a different solution?
Regards
Nithin
Davy wrote:
| Quote: | Hi all,
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 |
|
|
| Back to top |
|
 |
Ben Rudiak-Gould
Guest
|
Posted:
Thu Dec 15, 2005 9:16 am Post subject:
Re: 2's Complement Multiply with Constant |
|
|
Davy wrote:
| Quote: | I have 2's Complement digit range from negtive to positive. How to
multiply it with a constant?
|
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 |
|
| Back to top |
|
 |
RED
Guest
|
Posted:
Thu Dec 15, 2005 3:50 pm Post subject:
Re: 2's Complement Multiply with Constant |
|
|
Well you can do the sign extension of this "signed number" by the
number of bits of the constant and then simple multiply.. i am assuming
the constant to be a positive number. You can use the concept of adding
the signed numbers (thats easy in 2's compliment) for multiplication...
See the cases of overflow and avoid it..
RED
Davy wrote:
| Quote: | Hi all,
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 |
|
|
| Back to top |
|
 |
Charles Marslett
Guest
|
Posted:
Sat Dec 17, 2005 9:15 am Post subject:
Re: 2's Complement Multiply with Constant |
|
|
On Thu, 15 Dec 2005 06:07:20 +0000, Ben Rudiak-Gould
<br276deleteme@cam.ac.uk> wrote:
| Quote: | Davy wrote:
I have 2's Complement digit range from negtive to positive. How to
multiply it with a constant?
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
|
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 |
|
| Back to top |
|
 |
|
|
|
|