Solved

Addition and multiplication

Posted on 2006-06-19
18
240 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 45

Expert Comment

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

Expert Comment

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

Accepted Solution

by:
Kdo earned 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 161
Raspberry Pi 3 to send text message 9 115
Autosar OS Multicore Share Resources confusion ? 2 40
A  &&  B  || (C||D) how it works in c 7 56
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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

809 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