Shift Operation

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

    #16
    Re: Shift Operation

    In article <1160487007.734 018.243340@m7g2 000cwm.googlegr oups.com>,
    Ancient_Hacker <grg2@comcast.n etwrote:
    >Why would you ever want to do an "arithmetic " right shift on a negative
    >number?
    To divide by 2?
    >Even if the "sign" bit got shifted into the top bits, the result is
    >mostly mathematically useless. i.e. shift of -1 on a two's complement
    >machine gives you .....
    -1, which is (-1)/2 rounded down (which is the behaviour I usually
    want from integer division).

    -- Richard



    Comment

    • Frederick Gotham

      #17
      Re: Shift Operation

      Nishu posted:
      (unsigned) i >>= 1; /* (Results in logical shift) */

      As Chris mentioned, you might want to set your compiler to Strict C Mode if
      it compiles that for you. No case ever results in an L-value in C.

      --

      Frederick Gotham

      Comment

      • Frederick Gotham

        #18
        Re: Shift Operation

        Richard Tobin posted:

        i = ((unsigned)i) >4;

        If I'm not mistake, this will not retain the original bit-pattern on systems
        other than 2's complement.

        --

        Frederick Gotham

        Comment

        • Frederick Gotham

          #19
          Re: Shift Operation

          Frederick Gotham posted:
          No case ever results in an L-value in C.
          Typo correction:


          No CAST ever results in a...


          --

          Frederick Gotham

          Comment

          • Harald van Dijk

            #20
            Re: Shift Operation

            Frederick Gotham wrote:
            Richard Tobin posted:
            i = ((unsigned)i) >4;
            >
            If I'm not mistake, this will not retain the original bit-pattern on systems
            other than 2's complement.
            If you care about the bit pattern, you probably shouldn't be using
            signed integers anyway.

            Comment

            • Nishu

              #21
              Re: Shift Operation


              Harald van Dijk wrote:
              Frederick Gotham wrote:
              Richard Tobin posted:
              i = ((unsigned)i) >4;
              If I'm not mistake, this will not retain the original bit-pattern on systems
              other than 2's complement.
              >
              If you care about the bit pattern, you probably shouldn't be using
              signed integers anyway.
              Yes, since most of the processors consider 2's complement signed
              integers implementation, my interest is in relation to that only. (i =
              ((unsigned)i) >4;) It will_NOT retain the intended bit pattern for
              signed arithmetic. Since, as C is flexible for >operation on signed
              integers, C compilers come up >as arithmetic right shift, maintaining
              the sign of the number while shifting and in case you need padding of
              0s in MSBs for specific applications, just type CAST it to unsigned !!
              Thanks a lot.

              -Nishu

              Comment

              • pete

                #22
                Re: Shift Operation

                Nishu wrote:
                >
                Harald van Dijk wrote:
                Frederick Gotham wrote:
                Richard Tobin posted:
                i = ((unsigned)i) >4;
                >
                If I'm not mistake,
                this will not retain the original bit-pattern on systems
                other than 2's complement.
                If you care about the bit pattern, you probably shouldn't be using
                signed integers anyway.
                >
                Yes, since most of the processors consider 2's complement signed
                integers implementation, my interest is in relation to that only. (i =
                ((unsigned)i) >4;) It will_NOT retain the intended bit pattern for
                signed arithmetic. Since, as C is flexible for >operation on signed
                integers,
                C compilers come up >as arithmetic right shift, maintaining
                the sign of the number while shifting and in case you need padding of
                0s in MSBs for specific applications, just type CAST it to unsigned !!
                Thanks a lot.
                What is your reason for not wanting to use the division operator
                to acheive an arithmetic result?

                What's wrong with (i /= 16)
                instead of (i = ((unsigned)i) >4)?

                --
                pete

                Comment

                • Nishu

                  #23
                  Re: Shift Operation


                  pete wrote:
                  >
                  <snip>
                  What is your reason for not wanting to use the division operator
                  to acheive an arithmetic result?
                  >
                  What's wrong with (i /= 16)
                  instead of (i = ((unsigned)i) >4)?
                  Well, It has nothing to do with the end result, which is obviously the
                  same. It is more specific in relation to optimization.
                  "Intelligen t" (Better) compilers optimize division by the powers of 2
                  (2, 4, 8, etc) to the simple (and faster) right shift operations using
                  barrel shifters on fixed point processors (say, DSPs and ARM) but some
                  poor compilers evoke division "function call" which is not the optimal
                  way of doing this kind of division. Hence, just to avoid this
                  possibilty, better to use shift operation and Since C language (middle
                  level language) support it, it is better to do it there itself. This
                  obviously can be extended to multiply operations too..like multiple by
                  7, which is nothing but (8 - 1) and so on..

                  -Nishu

                  Comment

                  • Frederick Gotham

                    #24
                    Re: Shift Operation

                    Nishu posted:
                    Well, It has nothing to do with the end result, which is obviously the
                    same. It is more specific in relation to optimization.
                    "Intelligen t" (Better) compilers optimize division by the powers of 2
                    (2, 4, 8, etc) to the simple (and faster) right shift operations using
                    barrel shifters on fixed point processors (say, DSPs and ARM) but some
                    poor compilers evoke division "function call" which is not the optimal
                    way of doing this kind of division. Hence, just to avoid this
                    possibilty, better to use shift operation and Since C language (middle
                    level language) support it, it is better to do it there itself. This
                    obviously can be extended to multiply operations too..like multiple by
                    7, which is nothing but (8 - 1) and so on..

                    A lot of people think you should just write:

                    x /= 2;

                    , and let the compiler optimise it.

                    However, these same people still use static data in functions to boost
                    efficiency, so there's no bonafide rule for where you yourself should
                    optimise, and where the compiler should optimise on your behalf.

                    --

                    Frederick Gotham

                    Comment

                    • CBFalconer

                      #25
                      Re: Shift Operation

                      Nishu wrote:
                      pete wrote:
                      >>
                      <snip>
                      >What is your reason for not wanting to use the division operator
                      >to acheive an arithmetic result?
                      >>
                      >What's wrong with (i /= 16)
                      >instead of (i = ((unsigned)i) >4)?
                      >
                      Well, It has nothing to do with the end result, which is obviously
                      the same. It is more specific in relation to optimization.
                      "Intelligen t" (Better) compilers optimize division by the powers of
                      2 (2, 4, 8, etc) to the simple (and faster) right shift operations
                      using barrel shifters on fixed point processors (say, DSPs and ARM)
                      but some poor compilers evoke division "function call" which is
                      not the optimal way of doing this kind of division. Hence, just to
                      avoid this possibilty, better to use shift operation and Since C
                      language (middle level language) support it, it is better to do it
                      there itself. This obviously can be extended to multiply operations
                      too..like multiple by 7, which is nothing but (8 - 1) and so on..
                      You should leave it to the compiler optimizer, which is aware of
                      the number system used and can find these optimizations much better
                      than you can. Write for clarity. Unless you are writing the
                      actual optimizer.

                      --
                      Some informative links:
                      <news:news.anno unce.newusers
                      <http://www.geocities.c om/nnqweb/>
                      <http://www.catb.org/~esr/faqs/smart-questions.html>
                      <http://www.caliburn.nl/topposting.html >
                      <http://www.netmeister. org/news/learn2quote.htm l>
                      <http://cfaj.freeshell. org/google/>


                      Comment

                      Working...