Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Addition and multiplication

Posted on 2006-06-19
18
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +2
18 Comments
 
LVL 84

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

715 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