Richard Heathfield wrote:
Ico said:
mj********@yaho o.co.in wrote: How can we perfom multiplication programatically without using + or *
operator.
Can any one help out in this one.
#include <stdio.h>
int mult(int a, int b)
{
int c = 0;
while(a--) c -= b;
return -c;
}
Very funny. :-)
But I suspect his teacher is trying to get him to find out how computers do
addition and multiplication "under the hood" (because a computer does not
have the luxury of a magic wand we call '*').
For "computer", read "lowest level of computing hardware". That level
usually doesn't have multiplication as a primitive operation. Go any higher
(say, the processor as a whole) and you'll find that most computers do have it.
<musing>
Doing this in C is a silly exercise, by the way, because the assumption that
C's set of primitive operations (minus + and *) closely resembles that of
the hardware is less and less commonly valid (or relevant). This line of
thinking, incidentally, is also what makes people invest in clever bit
tricks when they start prematurely optimizing.
I'd think a good exposition in microcode would be more relevant. Either that
or, if you really think it's relevant that people find a general
multiplication algorithm on their own (why?), a very basic set of operators
and statements with which to do it, but not just "go do it in C without * or +".
</musing>
S.