a * b

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vols

    #1

    a * b

    If we cannot use 'multiply' because of the memory limitation, how to
    implement "a*b" ?
    I know 3*7 can be written as 3<<3 - 3, but what is the general
    solution? thanks.

    Vol.

  • Victor Bazarov

    #2
    Re: a * b

    Vols wrote:
    If we cannot use 'multiply' because of the memory limitation, how to
    implement "a*b" ?
    Huh? Memory limitation? What do you mean, exactly?
    I know 3*7 can be written as 3<<3 - 3,
    Try it, you'll be surprised at the result.
    but what is the general
    solution? thanks.
    3*7

    or you could try

    7*3

    ..

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Vols

      #3
      Re: a * b

      OK. let's put this way:
      How to implement ' a*b ' ( a multiply with b ) without using ' * ',
      for example
      3* 7 = 21, we can do this way 3<<3 - 3 = 21,
      but what is the general solution for ' a * b' ?
      Thanks.
      Vol


      Victor Bazarov wrote:
      Vols wrote:
      If we cannot use 'multiply' because of the memory limitation, how to
      implement "a*b" ?
      >
      Huh? Memory limitation? What do you mean, exactly?
      >
      I know 3*7 can be written as 3<<3 - 3,
      >
      Try it, you'll be surprised at the result.
      >
      but what is the general
      solution? thanks.
      >
      3*7
      >
      or you could try
      >
      7*3
      >
      .
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask

      Comment

      • Siam

        #4
        Re: a * b

        for(int i=0; i<b-1; i++)
        {
        a += a;
        }

        or

        for(int i=0; i<b-1; i++)
        {
        b += b;
        }

        That would work when multiply by a positive number... Easy to adapt for
        negatives too...

        Vols wrote:
        OK. let's put this way:
        How to implement ' a*b ' ( a multiply with b ) without using ' * ',
        for example
        3* 7 = 21, we can do this way 3<<3 - 3 = 21,
        but what is the general solution for ' a * b' ?
        Thanks.
        Vol
        >
        >
        Victor Bazarov wrote:
        Vols wrote:
        If we cannot use 'multiply' because of the memory limitation, how to
        implement "a*b" ?
        Huh? Memory limitation? What do you mean, exactly?
        I know 3*7 can be written as 3<<3 - 3,
        Try it, you'll be surprised at the result.
        but what is the general
        solution? thanks.
        3*7

        or you could try

        7*3

        .

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask

        Comment

        • Siam

          #5
          Re: a * b

          Oops, the second solution should of course be:

          for(int i=0; i<a-1; i++)
          {
          b += b;
          }

          Comment

          • Victor Bazarov

            #6
            Re: a * b

            Vols wrote:
            OK. let's put this way:

            Please don't top-post.
            How to implement ' a*b ' ( a multiply with b ) without using ' * ',
            for example
            3* 7 = 21, we can do this way 3<<3 - 3 = 21,
            No, *we* cannot.

            (3<<3 - 3) will *not* yield 21, at least in C++. Try it and you'll see.
            but what is the general solution for ' a * b' ?
            If multiplication is not available on your hardware, then you create a loop
            in which you add 'a' to 'a' (b-1) times. That's what they were supposed to
            teach you in grade school.

            But that only works for _numbers_, not for matrices, for example.
            Operations
            mean different things for different types, so there can be no general
            solution.
            Thanks.
            Vol
            >
            >
            Victor Bazarov wrote:
            >Vols wrote:
            >>If we cannot use 'multiply' because of the memory limitation, how to
            >>implement "a*b" ?
            >>
            >Huh? Memory limitation? What do you mean, exactly?
            >>
            >>I know 3*7 can be written as 3<<3 - 3,
            >>
            >Try it, you'll be surprised at the result.
            >>
            >>but what is the general
            >>solution? thanks.
            >>
            > 3*7
            >>
            >or you could try
            >>
            > 7*3
            >>
            >.
            >>
            >V
            >--
            >Please remove capital 'A's when replying by e-mail
            >I do not respond to top-posted replies, please don't ask
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask


            Comment

            • Howard

              #7
              Re: a * b

              Please don't top-post! [re-arranged]
              >>
              >Victor Bazarov wrote:
              Vols wrote:
              If we cannot use 'multiply' because of the memory limitation, how to
              implement "a*b" ?
              >
              Huh? Memory limitation? What do you mean, exactly?
              >
              I know 3*7 can be written as 3<<3 - 3,
              >
              Try it, you'll be surprised at the result.
              >
              but what is the general
              solution? thanks.
              >
              3*7
              >
              or you could try
              >
              7*3
              >
              Vols wrote:
              >OK. let's put this way:
              >How to implement ' a*b ' ( a multiply with b ) without using ' * ',
              >for example
              >3* 7 = 21, we can do this way 3<<3 - 3 = 21,
              Really? Better test it. (And then check your operator precedence table.)
              >but what is the general solution for ' a * b' ?
              There is no "general solution" which involves shifting or any other trick
              like that. There are an infinite number of ways to generate a given value,
              once you know the value you want.

              Multiplication is simply repeated addition. The solutions below show one
              way (well, two ways) to use addition to get your answer.

              "Siam" <siamraf@gmail. comwrote in message
              news:1154448203 .629948.306460@ s13g2000cwa.goo glegroups.com.. .
              for(int i=0; i<b-1; i++)
              {
              a += a;
              }
              >
              or
              >
              for(int i=0; i<b-1; i++)
              I take it you meant "i < a-1"? (And it might be more easily understood if
              you started at 1.)
              {
              b += b;
              }
              >
              That would work when multiply by a positive number... Easy to adapt for
              negatives too...
              >
              What about for 0?

              -Howard


              Comment

              • Vols

                #8
                Re: a * b

                How to implement ' a*b ' ( a multiply with b ) without using ' * ',
                for example
                3* 7 = 21, we can do this way 3<<3 - 3 = 21,
                >
                No, *we* cannot.
                Yes, we are, please try :unsigned a = (3<<3)-3;

                Comment

                • Vols

                  #9
                  Re: a * b

                  Vols wrote:
                  OK. let's put this way:
                  >
                  >
                  Please don't top-post.
                  What is top-post?
                  How to implement ' a*b ' ( a multiply with b ) without using ' * ',
                  for example
                  3* 7 = 21, we can do this way 3<<3 - 3 = 21,
                  >
                  No, *we* cannot.
                  Yes, YOU cannot, others can with (3<<3)-3
                  (3<<3 - 3) will *not* yield 21, at least in C++. Try it and you'll see.
                  >
                  but what is the general solution for ' a * b' ?
                  >
                  If multiplication is not available on your hardware, then you create a loop
                  in which you add 'a' to 'a' (b-1) times. That's what they were supposed to
                  teach you in grade school.
                  Congruatulation s! You graduated from primary school.
                  There could be some other solutions.

                  But that only works for _numbers_, not for matrices, for example.
                  Operations
                  mean different things for different types, so there can be no general
                  solution.
                  >
                  Thanks.
                  Vol


                  Victor Bazarov wrote:
                  Vols wrote:
                  >If we cannot use 'multiply' because of the memory limitation, how to
                  >implement "a*b" ?
                  >
                  Huh? Memory limitation? What do you mean, exactly?
                  >
                  >I know 3*7 can be written as 3<<3 - 3,
                  >
                  Try it, you'll be surprised at the result.
                  >
                  >but what is the general
                  >solution? thanks.
                  >
                  3*7
                  >
                  or you could try
                  >
                  7*3
                  >
                  .
                  >
                  V
                  --
                  Please remove capital 'A's when replying by e-mail
                  I do not respond to top-posted replies, please don't ask
                  >
                  --
                  Please remove capital 'A's when replying by e-mail
                  I do not respond to top-posted replies, please don't ask

                  Comment

                  • Jakob Bieling

                    #10
                    Re: a * b

                    Vols <volunteers@gma il.comwrote:
                    >>How to implement ' a*b ' ( a multiply with b ) without using ' * ',
                    >>for example
                    >>3* 7 = 21, we can do this way 3<<3 - 3 = 21,
                    >No, *we* cannot.
                    Yes, we are, please try :unsigned a = (3<<3)-3;
                    But that was not the code in question. The original code does *not*
                    work, while yours does. '3<<3 - 3' is different from '(3<<3)-3'.

                    regards
                    --
                    jb

                    (reply address in rot13, unscramble first)


                    Comment

                    • Victor Bazarov

                      #11
                      Re: a * b

                      Vols wrote:
                      >Vols wrote:
                      >>OK. let's put this way:
                      >>
                      >>
                      >Please don't top-post.
                      >
                      What is top-post?
                      What you did before and what you're not doing any more. You're learning.
                      That's good!
                      >>How to implement ' a*b ' ( a multiply with b ) without using ' * ',
                      >>for example
                      >>3* 7 = 21, we can do this way 3<<3 - 3 = 21,
                      >>
                      >No, *we* cannot.
                      Yes, YOU cannot, others can with (3<<3)-3
                      Parentheses make a difference, don't they?
                      >(3<<3 - 3) will *not* yield 21, at least in C++. Try it and you'll
                      >see.
                      >>
                      >>but what is the general solution for ' a * b' ?
                      >>
                      >If multiplication is not available on your hardware, then you create
                      >a loop in which you add 'a' to 'a' (b-1) times. That's what they
                      >were supposed to teach you in grade school.
                      Congruatulation s! You graduated from primary school.
                      There could be some other solutions.
                      There could. But why bother?
                      [...excessive quoting removed...]
                      V
                      --
                      Please remove capital 'A's when replying by e-mail
                      I do not respond to top-posted replies, please don't ask


                      Comment

                      • Ivan Vecerina

                        #12
                        Re: a * b

                        "Vols" <volunteers@gma il.comwrote in message
                        news:1154447730 .841957.250560@ m79g2000cwm.goo glegroups.com.. .
                        : OK. let's put this way:
                        : How to implement ' a*b ' ( a multiply with b ) without using ' * ',
                        : for example
                        : 3* 7 = 21, we can do this way 3<<3 - 3 = 21,
                        : but what is the general solution for ' a * b' ?
                        You'll find a complete and definitive answer
                        in Knuth's TAOCP.

                        But a reasonably easy first improvement over the
                        addition of a repeated b times looks somewhat like:
                        sum = 0;
                        while( b>0 ) {
                        if( b&1 ) sum += a;
                        b>>=1; a<<=1;
                        }

                        But instead of reinventing the wheel, I would
                        recommend looking for an existing "big int"
                        library, there are many around...

                        hth -Ivan
                        --
                        http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
                        Brainbench MVP for C++ <http://www.brainbench.com


                        Comment

                        • Thomas J. Gritzan

                          #13
                          Re: a * b

                          Siam schrieb:
                          for(int i=0; i<b-1; i++)
                          {
                          a += a;
                          }
                          >
                          or
                          >
                          for(int i=0; i<b-1; i++)
                          ....; i<a-1; ...
                          {
                          b += b;
                          }
                          >
                          That would work when multiply by a positive number... Easy to adapt for
                          negatives too...
                          Did you try it?
                          I think it would calculate a * 2^b and the other b * 2^a.

                          To multiply a and b, you would need another variable:

                          // works for positive b.
                          int sum = 0;
                          for(int i=0; i<b; i++)
                          sum += a;

                          --
                          Thomas

                          Comment

                          • Howard

                            #14
                            Re: a * b


                            "Howard" <alicebt@hotmai l.comwrote in message
                            news:e7Lzg.5266 89$Fs1.328019@b gtnsc05-news.ops.worldn et.att.net...
                            >
                            Multiplication is simply repeated addition. The solutions below show one
                            way (well, two ways) to use addition to get your answer.
                            >
                            Actually they're not, as Thomas pointed out. :-(

                            -Howard


                            Comment

                            • Thomas J. Gritzan

                              #15
                              Re: a * b

                              Thomas J. Gritzan schrieb:
                              Siam schrieb:
                              >for(int i=0; i<b-1; i++)
                              >{
                              > a += a;
                              >}
                              >>
                              >or
                              >>
                              >for(int i=0; i<b-1; i++)
                              >
                              ...; i<a-1; ...
                              >
                              >{
                              > b += b;
                              >}
                              >>
                              >That would work when multiply by a positive number... Easy to adapt for
                              >negatives too...
                              >
                              Did you try it?
                              I think it would calculate a * 2^b and the other b * 2^a.
                              Just for clarification:
                              2^b in this case is the b-th exponent of 2, and not 2 xor b.

                              --
                              Thomas

                              Comment

                              Working...