Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Addition and multiplication

Posted on 2006-06-19
18
Medium Priority
?
261 Views
Last Modified: 2010-04-15
Hi all,
 For example 5*8 = 40.
 Instead of using multiplication, how can I use addition to find the answer? Thanks a lot.
PS: i don't want to use 5+5+5...+ 5 eight times.
0
Comment
Question by:valleytech
  • 7
  • 5
  • 3
  • +2
18 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 16937880
how about
x=5;
x+=x;
x+=x;
x+=x;
0
 
LVL 15

Expert Comment

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

Author Comment

by:valleytech
ID: 16937925
can we use some shift operator or something like that??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:ozo
ID: 16937976
Can we use boolean or compare operators?
Can we use division?
Can we use array lookups?
0
 
LVL 46

Expert Comment

by:Kent Olsen
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

by:valleytech
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);

      printf("your result is %d",result);
}

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

Expert Comment

by:Kent Olsen
ID: 16938139
Hi valleytech,

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

  Multiplier >>= 1;


Kent
0
 
LVL 5

Expert Comment

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

Author Comment

by:valleytech
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

by:cryptosid
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);

     printf("your result is %d",result);
}
0
 
LVL 5

Expert Comment

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

Kent is GOD around here :-)

Best Regards,
Siddhesh
0
 

Author Comment

by:valleytech
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

by:valleytech
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;
}
 vs your way.
0
 
LVL 46

Accepted Solution

by:
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.
   We don't add.
   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

by:valleytech
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

by:cryptosid
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

by:valleytech
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

by:cryptosid
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question