a * b

V

Vols

If we cannot use 'multiply' because of the memory limitation, how to
implement "a*b" ?
I know 3*7 can be written as 3<<3 - 3, but what is the general
solution? thanks.

Vol.
 
V

Victor Bazarov

Vols said:
If we cannot use 'multiply' because of the memory limitation, how to
implement "a*b" ?

Huh? Memory limitation? What do you mean, exactly?
I know 3*7 can be written as 3<<3 - 3,

Try it, you'll be surprised at the result.
but what is the general
solution? thanks.

3*7

or you could try

7*3

..

V
 
V

Vols

OK. let's put this way:
How to implement ' a*b ' ( a multiply with b ) without using ' * ',
for example
3* 7 = 21, we can do this way 3<<3 - 3 = 21,
but what is the general solution for ' a * b' ?
Thanks.
Vol
 
S

Siam

for(int i=0; i<b-1; i++)
{
a += a;
}

or

for(int i=0; i<b-1; i++)
{
b += b;
}

That would work when multiply by a positive number... Easy to adapt for
negatives too...
 
S

Siam

Oops, the second solution should of course be:

for(int i=0; i<a-1; i++)
{
b += b;
}
 
V

Victor Bazarov

Vols said:
OK. let's put this way:


Please don't top-post.
How to implement ' a*b ' ( a multiply with b ) without using ' * ',
for example
3* 7 = 21, we can do this way 3<<3 - 3 = 21,

No, *we* cannot.

(3<<3 - 3) will *not* yield 21, at least in C++. Try it and you'll see.
but what is the general solution for ' a * b' ?

If multiplication is not available on your hardware, then you create a loop
in which you add 'a' to 'a' (b-1) times. That's what they were supposed to
teach you in grade school.

But that only works for _numbers_, not for matrices, for example.
Operations
mean different things for different types, so there can be no general
solution.
 
H

Howard

Please don't top-post! [re-arranged]

Really? Better test it. (And then check your operator precedence table.)

There is no "general solution" which involves shifting or any other trick
like that. There are an infinite number of ways to generate a given value,
once you know the value you want.

Multiplication is simply repeated addition. The solutions below show one
way (well, two ways) to use addition to get your answer.

Siam said:
for(int i=0; i<b-1; i++)
{
a += a;
}

or

for(int i=0; i<b-1; i++)

I take it you meant "i < a-1"? (And it might be more easily understood if
you started at 1.)
{
b += b;
}

That would work when multiply by a positive number... Easy to adapt for
negatives too...

What about for 0?

-Howard
 
V

Vols

Please don't top-post.

What is top-post?
No, *we* cannot.
Yes, YOU cannot, others can with (3<<3)-3
(3<<3 - 3) will *not* yield 21, at least in C++. Try it and you'll see.


If multiplication is not available on your hardware, then you create a loop
in which you add 'a' to 'a' (b-1) times. That's what they were supposed to
teach you in grade school.
Congruatulations! You graduated from primary school.
There could be some other solutions.
 
J

Jakob Bieling

Yes, we are, please try :unsigned a = (3<<3)-3;

But that was not the code in question. The original code does *not*
work, while yours does. '3<<3 - 3' is different from '(3<<3)-3'.

regards
 
I

Ivan Vecerina

: OK. let's put this way:
: How to implement ' a*b ' ( a multiply with b ) without using ' * ',
: for example
: 3* 7 = 21, we can do this way 3<<3 - 3 = 21,
: but what is the general solution for ' a * b' ?
You'll find a complete and definitive answer
in Knuth's TAOCP.

But a reasonably easy first improvement over the
addition of a repeated b times looks somewhat like:
sum = 0;
while( b>0 ) {
if( b&1 ) sum += a;
b>>=1; a<<=1;
}

But instead of reinventing the wheel, I would
recommend looking for an existing "big int"
library, there are many around...

hth -Ivan
 
T

Thomas J. Gritzan

Siam said:
for(int i=0; i<b-1; i++)
{
a += a;
}

or

for(int i=0; i<b-1; i++)

....; i<a-1; ...
{
b += b;
}

That would work when multiply by a positive number... Easy to adapt for
negatives too...

Did you try it?
I think it would calculate a * 2^b and the other b * 2^a.

To multiply a and b, you would need another variable:

// works for positive b.
int sum = 0;
for(int i=0; i<b; i++)
sum += a;
 
H

Howard

Multiplication is simply repeated addition. The solutions below show one
way (well, two ways) to use addition to get your answer.

Actually they're not, as Thomas pointed out. :-(

-Howard
 
T

Thomas J. Gritzan

Thomas said:
...; i<a-1; ...


Did you try it?
I think it would calculate a * 2^b and the other b * 2^a.

Just for clarification:
2^b in this case is the b-th exponent of 2, and not 2 xor b.
 
V

Vols

What you did before and what you're not doing any more. You're learning.
That's good!
There could. But why bother?

Why bother? All the other guys show good points. Your answer is just
nothing. Actually there are some other tricks in the question such like
' if 'a' is integer and 'b' is float. The guy -- Victor Bazarov is
just a trash talker and BS
 
S

Siam

Thomas said:
Did you try it?
I think it would calculate a * 2^b and the other b * 2^a.

Haha, so true...how foolish of me :/

Here's a way without an accumulating sum, however :p

for(int i=1;i<b; i++)
{
a += a/i;
}

Siam
 
L

Luc The Perverse

Vols said:
Why bother? All the other guys show good points. Your answer is just
nothing. Actually there are some other tricks in the question such like
' if 'a' is integer and 'b' is float. The guy -- Victor Bazarov is
just a trash talker and BS

Actually your question makes no sense - no one really knows what you are
asking for or what you want
 
M

Matt

Vols said:
What is top-post?

Here are a couple of top-posting FAQs:

A: Because it ruins the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail and on Usenet?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top