How can we perfom multiplication programatically without using + or * operator.

Discussion in 'C Programming' started by mjdeesh_hi@yahoo.co.in, May 29, 2006.

  1. Guest

    How can we perfom multiplication programatically without using + or *
    operator.
    Can any one help out in this one.

    Jagadeesh.
    , May 29, 2006
    #1
    1. Advertising

  2. santosh Guest

    wrote:
    > How can we perfom multiplication programatically without using + or *
    > operator.
    > Can any one help out in this one.


    If you conceptualise the numerical values in a binary format, then left
    shifting the value by N is similar to multiplying the value by 2^N.
    santosh, May 29, 2006
    #2
    1. Advertising

  3. said:

    > How can we perfom multiplication programatically without using + or *
    > operator.


    Addition can be done by using ^ (xor), & (and), and << (left shift) in the
    proper way.

    Multiplication is repeated addition (i.e. addition in a loop).

    Alternatively, you can double and halve, if you're careful in the case of an
    odd number. Note that doubling will require using addition as described
    above (since you are not allowed the * operator).

    That's enough of a hint - now do your own homework.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, May 29, 2006
    #3
  4. Ico Guest

    wrote:
    >
    > How can we perfom multiplication programatically without using + or *
    > operator.
    > Can any one help out in this one.
    >


    #include <stdio.h>

    int mult(int a, int b)
    {
    int c = 0;
    while(a--) c -= b;
    return -c;
    }

    int main(void)
    {
    int a = 5;
    int b = 6;

    printf("%d x %d = %d\n", a, b, mult(a, b));

    return 0;
    }

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    Ico, May 29, 2006
    #4
  5. Ico said:

    > wrote:
    >>
    >> How can we perfom multiplication programatically without using + or *
    >> operator.
    >> Can any one help out in this one.
    >>

    >
    > #include <stdio.h>
    >
    > int mult(int a, int b)
    > {
    > int c = 0;
    > while(a--) c -= b;
    > return -c;
    > }


    Very funny. :)

    But I suspect his teacher is trying to get him to find out how computers do
    addition and multiplication "under the hood" (because a computer does not
    have the luxury of a magic wand we call '*').

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, May 29, 2006
    #5
  6. newbie Guest

    Richard Heathfield wrote:
    > Ico said:
    >
    > > wrote:
    > >>
    > >> How can we perfom multiplication programatically without using + or *
    > >> operator.
    > >> Can any one help out in this one.
    > >>

    > >
    > > #include <stdio.h>
    > >
    > > int mult(int a, int b)
    > > {
    > > int c = 0;
    > > while(a--) c -= b;
    > > return -c;
    > > }

    >
    > Very funny. :)
    >
    > But I suspect his teacher is trying to get him to find out how computers do
    > addition and multiplication "under the hood" (because a computer does not
    > have the luxury of a magic wand we call '*').
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)


    Hehehehehehe!

    Totally irrelevent but I thought your magic wand comment was funny :)
    newbie, May 29, 2006
    #6
  7. santosh Guest

    newbie wrote:
    > Richard Heathfield wrote:

    .... snip ...

    > > But I suspect his teacher is trying to get him to find out how computers do
    > > addition and multiplication "under the hood" (because a computer does not
    > > have the luxury of a magic wand we call '*').

    >
    > Hehehehehehe!
    >
    > Totally irrelevent but I thought your magic wand comment was funny :)


    Have you been exposing yourself to nitrous oxide? ;)
    santosh, May 29, 2006
    #7
  8. Richard Bos Guest

    wrote:

    > How can we perfom multiplication programatically without using + or *
    > operator.


    #include <stdio.h>

    int main(void)
    {
    double m1=4.0, m2=5.0;

    printf("%f times %f equals %f.\n", m1, m2, m1/(1.0/m2));

    return 0;
    }

    HTH; HAND.

    Richard
    Richard Bos, May 29, 2006
    #8
  9. Ico Guest

    Richard Bos <> wrote:
    > wrote:
    >
    >> How can we perfom multiplication programatically without using + or *
    >> operator.

    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > double m1=4.0, m2=5.0;
    >
    > printf("%f times %f equals %f.\n", m1, m2, m1/(1.0/m2));
    >
    > return 0;
    > }


    Taking stupid assignments literally, that must be some kind of dutch
    humor :)

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    Ico, May 29, 2006
    #9
  10. SM Ryan Guest

    wrote:
    #
    #
    #
    # How can we perfom multiplication programatically without using + or *
    # operator.
    # Can any one help out in this one.

    Any good text on computation theory should include the multiplication
    of radix one integers.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Mention something out of a Charleton Heston movie, and suddenly
    everybody's a theology scholar.
    SM Ryan, May 29, 2006
    #10
  11. Tomás Guest

    posted:


    > How can we perfom multiplication programatically without using + or *
    > operator.
    > Can any one help out in this one.
    >
    > Jagadeesh.



    Seems like you need a book on digital electronics and boolean logic.


    -Tomás
    Tomás, May 29, 2006
    #11
  12. Skarmander Guest

    Re: How can we perfom multiplication programatically without using+ or * operator.

    Richard Heathfield wrote:
    > Ico said:
    >
    >> wrote:
    >>> How can we perfom multiplication programatically without using + or *
    >>> operator.
    >>> Can any one help out in this one.
    >>>

    >> #include <stdio.h>
    >>
    >> int mult(int a, int b)
    >> {
    >> int c = 0;
    >> while(a--) c -= b;
    >> return -c;
    >> }

    >
    > Very funny. :)
    >
    > But I suspect his teacher is trying to get him to find out how computers do
    > addition and multiplication "under the hood" (because a computer does not
    > have the luxury of a magic wand we call '*').
    >

    For "computer", read "lowest level of computing hardware". That level
    usually doesn't have multiplication as a primitive operation. Go any higher
    (say, the processor as a whole) and you'll find that most computers do have it.

    <musing>
    Doing this in C is a silly exercise, by the way, because the assumption that
    C's set of primitive operations (minus + and *) closely resembles that of
    the hardware is less and less commonly valid (or relevant). This line of
    thinking, incidentally, is also what makes people invest in clever bit
    tricks when they start prematurely optimizing.

    I'd think a good exposition in microcode would be more relevant. Either that
    or, if you really think it's relevant that people find a general
    multiplication algorithm on their own (why?), a very basic set of operators
    and statements with which to do it, but not just "go do it in C without * or +".
    </musing>

    S.
    Skarmander, May 29, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manish_Ganvir
    Replies:
    13
    Views:
    1,559
    Keith Thompson
    Feb 14, 2005
  2. Andy B
    Replies:
    9
    Views:
    2,141
    ian_taylor
    Oct 22, 2008
  3. William Hughes
    Replies:
    13
    Views:
    1,204
    Ben Bacarisse
    Mar 15, 2010
  4. RAKHE
    Replies:
    2
    Views:
    579
    Mark Bluemel
    Jul 21, 2010
  5. Guido Holz

    howto perfom Image.read

    Guido Holz, Jan 31, 2008, in forum: Ruby
    Replies:
    1
    Views:
    80
    Tim Hunter
    Jan 31, 2008
Loading...

Share This Page