Solved

Addition and multiplication

Posted on 2006-06-19
18
222 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 84

Expert Comment

by:ozo
Comment Utility
how about
x=5;
x+=x;
x+=x;
x+=x;
0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
You can use a loop: "for" or "while".
0
 

Author Comment

by:valleytech
Comment Utility
can we use some shift operator or something like that??
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Can we use boolean or compare operators?
Can we use division?
Can we use array lookups?
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Kdo
Comment Utility
Hi valleytech,

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

  Multiplier >>= 1;


Kent
0
 
LVL 5

Expert Comment

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

Author Comment

by:valleytech
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Assisted Solution

by:cryptosid
cryptosid earned 250 total points
Comment Utility
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
Comment Utility
Even God makes mistakes :-) ...

Kent is GOD around here :-)

Best Regards,
Siddhesh
0
 

Author Comment

by:valleytech
Comment Utility
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
Comment Utility
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 45

Accepted Solution

by:
Kdo earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now