x
Solved

Posted on 2006-06-19
Medium Priority
287 Views
Hi all,
For example 5*8 = 40.
PS: i don't want to use 5+5+5...+ 5 eight times.
0
Question by:valleytech
• 7
• 5
• 3
• +2

LVL 85

Expert Comment

ID: 16937880
x=5;
x+=x;
x+=x;
x+=x;
0

LVL 15

Expert Comment

ID: 16937892
You can use a loop: "for" or "while".
0

Author Comment

ID: 16937925
can we use some shift operator or something like that??
0

LVL 85

Expert Comment

ID: 16937976
Can we use boolean or compare operators?
Can we use division?
Can we use array lookups?
0

LVL 46

Expert Comment

ID: 16937982
Hi valleytech,

If you can use binary (base 2) math, it's pretty easy.  :)

unsigned int Mult (unsigned multiplier, unsigned multiplicand)
{
unsigned Accumulator;

for (Accumulator = 0; Multiplier;  Multiplier >> 1)
{
if (Multiplier & 1)
Accumulater += Multiplicand;
Multiplicand << 1;
}
return Accumulator;
}

Good Luck!
Kent
0

Author Comment

ID: 16938039
well,
somehow it doesn't work

#include <stdio.h>
#include <stdlib.h>

unsigned int Mult (unsigned multiplier, unsigned multiplicand)
{
unsigned Accumulator;

for (Accumulator = 0; multiplier;  multiplier >> 1)
{
if (multiplier & 1)
Accumulator += multiplicand;
multiplicand << 1;
}
return Accumulator;
}

void main()
{
int first, second, result;

printf("Enter the first number : ");
scanf("%d",&first);

printf("Enter the second number : ");
scanf("%d",&second);

result = Mult(first, second);

}

i guess the for(), multiplier >> 1 should be mutiplier = multiplier >> 1
am i right?
0

LVL 46

Expert Comment

ID: 16938139
Hi valleytech,

Whoops.  That'll work.  I meant to type:

Multiplier >>= 1;

Kent
0

LVL 5

Expert Comment

ID: 16938169
Yes that's correct.. it should be multiplier = multiplier << 1
0

Author Comment

ID: 16938272
oh kdo Multiplier >>= 1 ==> first number 5, second number 8 , result is 16!!! suppose to be 40
oh cryptosid  multiplier = multiplier << 1 ==> first number 5, second number 8 , result is 8!!! suppose to be 40
could you please run and verify for me? thanks a lot.
0

LVL 5

Assisted Solution

cryptosid earned 1000 total points
ID: 16938464
there were 2 mistakes... i have corrected.. now it should be fine..

#include <stdio.h>
#include <stdlib.h>

unsigned int Mult (unsigned multiplier, unsigned multiplicand)
{
unsigned Accumulator;

for (Accumulator = 0; multiplier;  multiplier >>= 1)
{
if (multiplier & 1)
Accumulator += multiplicand;
//2nd mistake..
multiplicand <<= 1;
}
return Accumulator;
}

void main()
{
int first, second, result;

printf("Enter the first number : ");
scanf("%d",&first);

printf("Enter the second number : ");
scanf("%d",&second);

result = Mult(first, second);

}
0

LVL 5

Expert Comment

ID: 16938473
Even God makes mistakes :-) ...

Kent is GOD around here :-)

Best Regards,
Siddhesh
0

Author Comment

ID: 16938531
he he.
Could you please explain to me the advantage of this algorithm over just like a regular loop? Thanks.
0

Author Comment

ID: 16938560
i mean regular loop in this example
5 * 8;
first = 5;
second = 8;
result = 0;
for (i=0; i <=8; i++)
{
result = resut + first;
}
0

LVL 46

Accepted Solution

Kent Olsen earned 1000 total points
ID: 16938848
Hi cryptosid,

> Kent is GOD around here :-)

Yeah.  My wife told me I could be God there.  She's God here.  :~}

Hi Valleytech,

Let's walk through the math just a bit.

8 * 5.     In binary, we're multiplying 01000 by 00101.

The first time through the loop the lower bit is a 1.
We add the Multiplicand (8) to the Accumulator.
Right shift the Multiplier (101), becoming 010.
Left shift the Multiplicand (8) becoming 16.

The second time through the loop the lower bit is a 0.
Right shift the Multiplier (010), becoming 001.
Left shift the Multiplicand (16) becoming 32.

The third time through the loop the lower bit is a 1.
We add the Multiplicand (32) to the Accumulator (8) giving 40.
Right shift the Multiplier (001), becoming 000.
Left shift the Multiplicand.

The loop ends because the Multiplier is now 0.

Turning the math around and multiplying 5 * 8 shows the same thing.  We're only going to add to the accumulator once, and that is on the third pass through the loop.  5*2 = 10, 10 * 2 = 20, 20 * 2 = 40.

Kent
0

Author Comment

ID: 16938887
wow. I get it now. Thanks. By the way, i also need help on Windbg. Could you please take a look at? thanks.
0

LVL 5

Expert Comment

ID: 16946646
Hi valleytech..

At microprocessor level intially one never had the luxury of assembly instructions that could multiply 2 numbers. So we used to use the method which Kent has methodically explained above.

Kent,
Great going.. yes and Wives are God's at home... no question about that.. in a marraige one person is always right and the other person is the 'husband' :-)

Regards,
Siddhesh
0

Author Comment

ID: 16954815
oh my friends,
I just wonder how that algorithm handle for negative numbers??? I checked it works for negative numbers, but i don't know why :) Thanks.
0

LVL 5

Expert Comment

ID: 16963971
I believe the system stores negative numbers in 2's complement form. That's the reason why it should be working fine.
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.