what is problem ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c.lang.myself@gmail.com

    what is problem ?

    suppose

    int a=4;

    int prod1=17*a;

    int prod2=a<<4+a; //another way of implementing 17*a

    Is there any problem with prod2..?
  • vippstar@gmail.com

    #2
    Re: what is problem ?

    On Nov 13, 5:23 pm, "c.lang.mys...@ gmail.com"
    <c.lang.mys...@ gmail.comwrote:
    suppose
    >
    int a=4;
    >
    int prod1=17*a;
    >
    int prod2=a<<4+a; //another way of implementing 17*a
    >
    Is there any problem with prod2..?
    There's no problem, the problem is the comment which is a lie.

    a << 4 + a means a << (4 + a)

    Comment

    • Zhao Jian

      #3
      Re: what is problem ?

      On Nov 13, 11:23 pm, "c.lang.mys...@ gmail.com"
      <c.lang.mys...@ gmail.comwrote:
      suppose
      >
      int a=4;
      >
      int prod1=17*a;
      >
      int prod2=a<<4+a;  //another way of implementing 17*a
      >
      Is there any problem with prod2..?
      It should be "int prod2 = (a << 4) + a;"

      Comment

      • Chris Dollin

        #4
        Re: what is problem ?

        c.lang.myself@g mail.com wrote:
        suppose
        >
        int a=4;
        >
        int prod1=17*a;
        (We must be inside a function.)
        int prod2=a<<4+a; //another way of implementing 17*a
        >
        Is there any problem with prod2..?
        Even if it's fixed as vippstar or Zhao note elsethread, the problem
        with `prod2` is that it's not clear what's going on. The Horse! Why
        shift-and-add when you can multiply by 17 and have the compiler sort
        things out?

        --
        "We dance, and the worlds melt away." - Curved Air, /Metamorphosis/

        Hewlett-Packard Limited registered no:
        registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

        Comment

        • Keith Thompson

          #5
          Re: what is problem ?

          vippstar@gmail. com writes:
          On Nov 13, 5:23 pm, "c.lang.mys...@ gmail.com"
          <c.lang.mys...@ gmail.comwrote:
          >suppose
          >>
          >int a=4;
          >>
          >int prod1=17*a;
          >>
          >int prod2=a<<4+a; //another way of implementing 17*a
          >>
          >Is there any problem with prod2..?
          >
          There's no problem, the problem is the comment which is a lie.
          >
          a << 4 + a means a << (4 + a)
          I think you just did his homework for him.

          To the original poster: Why do you ask? Did you try running the code
          yourself and seeing what it does?

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Andrey Tarasevich

            #6
            Re: what is problem ?

            Chris Dollin wrote:
            c.lang.myself@g mail.com wrote:
            >
            >suppose
            >>
            >int a=4;
            >>
            >int prod1=17*a;
            >
            (We must be inside a function.)
            >
            >int prod2=a<<4+a; //another way of implementing 17*a
            >>
            >Is there any problem with prod2..?
            >
            Even if it's fixed as vippstar or Zhao note elsethread, the problem
            with `prod2` is that it's not clear what's going on. The Horse! Why
            shift-and-add when you can multiply by 17 and have the compiler sort
            things out?
            Well, if they turn out to be non-equivalent, how do you know that the OP
            needs specifically the former and not the latter?

            BTW, they are not equivalent. The latter produces undefined behavior for
            negative 'a' in C99. It is, of course, rather strange to allow the
            possibility of OP preferring the UB for negative input, but
            nevertheless... :)

            --
            Best regards,
            Andrey Tarasevich

            Comment

            • Wolfgang Draxinger

              #7
              Re: what is problem ?

              c.lang.myself@g mail.com wrote:
              int prod2=a<<4+a; //another way of implementing 17*a
              Why so complicated? Read this
              <http://dl.fefe.de/optimizer-isec.pdfto get some insight, why
              writing readable code should be preferred over "hand optimized"
              code, which in some cases even performs worse.

              Wolfgang Draxinger
              --
              E-Mail address works, Jabber: hexarith@jabber .org, ICQ: 134682867

              Comment

              • CBFalconer

                #8
                Re: what is problem ?

                Wolfgang Draxinger wrote:
                c.lang.myself@g mail.com wrote:
                >
                >int prod2=a<<4+a; //another way of implementing 17*a
                >
                Why so complicated? Read this
                <http://dl.fefe.de/optimizer-isec.pdfto get some insight, why
                writing readable code should be preferred over "hand optimized"
                code, which in some cases even performs worse.
                In addition, it doesn't work. It runs into either implementation
                defined or undefined results whenever a is a negative integral
                quantity.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.

                Comment

                Working...