# Multiplying without the astrerisk

• 03-03-2003
grmoo
Multiplying without the astrerisk
Rewrite the following function so that the following conditions are
satisfied:
A) the multiplication operator ('*') is not used.
B) the division operator ('/') is not used.
C) the function does not make use of loops.
D) And no sneaky using assembly either!

int MultiplyBy321(int value)
{
int rslt;

rslt = value * 321;

return rslt;
}

how would something like that be done?
• 03-03-2003
vasanth
Well how abt using goto statements.. It is some what a loop.. can GOTO be used..
• 03-03-2003
jdinger
The first thing would be to pay attention to your teacher in the future so you don't have to ask other people to do your homework. The second would be to look up bit shifting and break 321 down into bitshiftable bites.
• 03-03-2003
grmoo
this isnt for school or anything like that this is for my own use, it is on that gearbox test that i requested and i have no idea how to do it....and i think using the goto statement would be considered a loop
• 03-03-2003
jdinger
The only way a goto would work in this situation would be ugly as sin (same as using a for loop to "fake" it). Look up bitshifting. Salem's example is right on for what I'm talking about. Bitshifting gives the same outcome as multiplying but is faster and only works when one of the numbers is a power of 2 (2,4,8,16,etc).

For numbers that aren't (like 321), you have to break it down into ones that are and add them up.
• 03-03-2003
grmoo
does anyone know of an online tutorial for that?
• 03-03-2003
Hammer
Quote:

Originally posted by grmoo
does anyone know of an online tutorial for that?
• 03-03-2003
Magos
321 =>
256 + 64 + 1 =>
2^8 + 2^6 + 2^0 =>
• 03-04-2003
Jaguar
Quote:

Or "cheat"
result = pow(10, log10(val)+log10(321) );
I think it is the best idea I've ever seen.:D
• 03-05-2003
Magos
Code:

```int MultiplyBy321(int Number) {   return (Number + Number + Number + ... + Number); //321 times }```
:D