lshift & rshift

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iesvs@free.fr

    lshift & rshift

    Hello guys,
    every time a rode a doc or a book about the language C I saw that
    operators << and >exist. But each time they said that << translate
    the digit to the left (and >...) but no one said if a << 1 mean
    every time a * 2, because there is the problem with the way the
    integer are stored. So I never use thoses operators because I fear
    strange behaviour on different architecture. Can I use them and be
    sure that a << 1 means a * 2 on every computer ? Is is in the ansi
    standard of language C ?

  • Richard Heathfield

    #2
    Re: lshift &amp; rshift

    iesvs@free.fr said:
    Hello guys,
    every time a rode a doc or a book about the language C I saw that
    operators << and >exist. But each time they said that << translate
    the digit
    Each bit is moved one place to the left, the leftmost bit falls off the end
    into the bit bucket (which should be emptied regularly), and the rightmost
    bit is set to 0.
    to the left (and >...)
    Beware signed types when shifting. The danger is particularly - er -
    dangerous when right-shifting negative numbers. What happens to the sign
    bit? (Answer: it depends on the implementation. )
    but no one said if a << 1 mean
    every time a * 2,
    Not every time, no. For example, consider a system where CHAR_BIT is 8.

    unsigned char ch = 1;

    ch <<= 1; /* ch is now 2 */
    ch <<= 1; /* ch is now 4 */
    ch <<= 1; /* ch is now 8 */
    ch <<= 1; /* ch is now 16 */
    ch <<= 1; /* ch is now 32 */
    ch <<= 1; /* ch is now 64 */
    ch <<= 1; /* ch is now 128 */
    ch <<= 1; /* ch is now 0, even though 2 * 128 is 256. */
    because there is the problem with the way the
    integer are stored. So I never use thoses operators because I fear
    strange behaviour on different architecture. Can I use them and be
    sure that a << 1 means a * 2 on every computer ?
    If you stick to unsigned types, and don't push any 1-bits off the end, yes.
    But why bother? Why not just multiply by 2, if that's what you want to do?
    Is is in the ansi standard of language C ?
    Yes, but - like I said - you do need to be careful.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Philip Potter

      #3
      Re: lshift &amp; rshift

      iesvs@free.fr wrote:
      Hello guys,
      every time a rode a doc or a book about the language C I saw that
      operators << and >exist. But each time they said that << translate
      the digit to the left (and >...) but no one said if a << 1 mean
      every time a * 2, because there is the problem with the way the
      integer are stored. So I never use thoses operators because I fear
      strange behaviour on different architecture. Can I use them and be
      sure that a << 1 means a * 2 on every computer ? Is is in the ansi
      standard of language C ?
      One of the problems with using the bit-shift operators to do arithmetic is you
      no longer say what you mean. If you want to multiply something by 2, say so.

      Another problem is that bit-shifting is not entirely defined for signed types.
      This is partly because a bit-shift could move a value bit into a sign bit, or
      vice versa, and partly because C allows different representations of signed
      integers (2's complement, 1's complement, or sign-magnitude).

      In the bad old days, some people used to use bit-shifts to perform simple
      multiplications because it would run quicker. These days, compilers can spot for
      themselves when this sort of trick is possible and will do so.

      For arithmetic, use arithmetic operators. Save bit-shifting for when you need
      bit-shifting semantics - for example, simulating a shift register.

      --
      Philip Potter pgp <atdoc.ic.ac. uk

      Comment

      • iesvs@free.fr

        #4
        Re: lshift &amp; rshift

        In the bad old days, some people used to use bit-shifts to perform simple
        multiplications because it would run quicker. These days, compilers can spot for
        themselves when this sort of trick is possible and will do so.
        >
        For arithmetic, use arithmetic operators. Save bit-shifting for when you need
        bit-shifting semantics - for example, simulating a shift register.
        >
        --
        Philip Potter pgp <atdoc.ic.ac. uk
        So if I want to multiply an int by two I have to use a * 2 (or a + a),
        but not << ? To be sure it works on every architecture (currently I
        ignore the problem of the size of an int).

        Comment

        • Mark Bluemel

          #5
          Re: lshift &amp; rshift

          iesvs@free.fr wrote:
          So if I want to multiply an int by two I have to use a * 2 <snip>?
          Why would you want to do it any other way?

          Comment

          • iesvs@free.fr

            #6
            Re: lshift &amp; rshift

            On Sep 27, 10:56 am, Mark Bluemel <mark_blue...@p obox.comwrote:
            ie...@free.fr wrote:
            So if I want to multiply an int by two I have to use a * 2 <snip>?
            >
            Why would you want to do it any other way?
            If << is faster, it would be great. I love to have fast software, I'm
            not rich and don't buy expensive computer.

            Comment

            • Ian Collins

              #7
              Re: lshift &amp; rshift

              iesvs@free.fr wrote:
              On Sep 27, 10:56 am, Mark Bluemel <mark_blue...@p obox.comwrote:
              >ie...@free.f r wrote:
              >>So if I want to multiply an int by two I have to use a * 2 <snip>?
              >Why would you want to do it any other way?
              >
              If << is faster, it would be great. I love to have fast software, I'm
              not rich and don't buy expensive computer.
              >
              What did your profiler tell you?

              --
              Ian Collins.

              Comment

              • karthikbalaguru

                #8
                Re: lshift &amp; rshift

                On Sep 27, 2:10 pm, "ie...@free .fr" <ie...@free.frw rote:
                On Sep 27, 10:56 am, Mark Bluemel <mark_blue...@p obox.comwrote:
                >
                ie...@free.fr wrote:
                So if I want to multiply an int by two I have to use a * 2 <snip>?
                >
                Why would you want to do it any other way?
                >
                If << is faster, it would be great. I love to have fast software, I'm
                not rich and don't buy expensive computer.
                Did you try to view the assembly of a * 2 or the assembly of a + a .
                I think,a*2 and a+a must internally be doing shifting using the shift
                operators if shift operators are very fast.
                Am i right ?

                If thatz wrong then, How do you think, a*2 or a+a will be internally ?

                Karthik Balaguru

                Comment

                • Mark Bluemel

                  #9
                  Re: lshift &amp; rshift

                  iesvs@free.fr wrote:
                  On Sep 27, 10:56 am, Mark Bluemel <mark_blue...@p obox.comwrote:
                  >ie...@free.f r wrote:
                  >>So if I want to multiply an int by two I have to use a * 2 <snip>?
                  >Why would you want to do it any other way?
                  >
                  If << is faster, it would be great. I love to have fast software, I'm
                  not rich and don't buy expensive computer.
                  As others have already pointed out, the compiler will be more than
                  capable of applying this sort of optimisation if it is appropriate. In
                  general you should write clear, correct, expressive code and let the
                  compiler deal with optimisation. Any decent compiler is likely to do a
                  better job of optimising this sort of thing than you could.

                  If you have performance issues, you are generally better off looking for
                  better algorithms than fiddling with this sort of trivia.

                  If you have to get down to tuning at the code, rather than algorithm,
                  level then you need to profile your code as Ian has suggested. Only by
                  measuring will you find the bottlenecks.

                  Comment

                  • iesvs@free.fr

                    #10
                    Re: lshift &amp; rshift

                    Did you try to view the assembly of a * 2 or the assembly of a + a .
                    I think,a*2 and a+a must internally be doing shifting using the shift
                    operators if shift operators are very fast.
                    Am i right ?
                    No you're wrong. A saw that it change a * 2 by a + a. (in reality I
                    was looking at a * 7) and I saw something like that :
                    b = a + a
                    c = b + b
                    c + b + a
                    So after I try a * 2 and get
                    a + a
                    But it didn't use lshift.

                    Comment

                    • iesvs@free.fr

                      #11
                      Re: lshift &amp; rshift

                      What did your profiler tell you?

                      What is a profiler ?

                      Comment

                      • Ian Collins

                        #12
                        Re: lshift &amp; rshift

                        iesvs@free.fr wrote:
                        >What did your profiler tell you?
                        >
                        What is a profiler ?
                        >
                        A very useful tool that's included in any self respecting tool set.



                        --
                        Ian Collins.

                        Comment

                        • iesvs@free.fr

                          #13
                          Re: lshift &amp; rshift

                          On Sep 27, 11:28 am, Ian Collins <ian-n...@hotmail.co mwrote:
                          ie...@free.fr wrote:
                          What did your profiler tell you?
                          >
                          What is a profiler ?
                          >
                          A very useful tool that's included in any self respecting tool set.
                          >

                          >
                          --
                          Ian Collins.
                          I never use these kind of stuffs.

                          Comment

                          • Ian Collins

                            #14
                            Re: lshift &amp; rshift

                            iesvs@free.fr wrote:
                            On Sep 27, 11:28 am, Ian Collins <ian-n...@hotmail.co mwrote:
                            >ie...@free.f r wrote:
                            >>>What did your profiler tell you?
                            >>What is a profiler ?
                            >A very useful tool that's included in any self respecting tool set.
                            >>
                            >http://en.wikipedia.org/wiki/Performance_analysis
                            >>
                            >
                            I never use these kind of stuffs.
                            >
                            Then you shouldn't be fiddling with micro-optimisations.

                            --
                            Ian Collins.

                            Comment

                            • pete

                              #15
                              Re: lshift &amp; rshift

                              Richard Heathfield wrote:
                              >
                              iesvs@free.fr said:
                              >
                              Hello guys,
                              every time a rode a doc or a book about the language C I saw that
                              operators << and >exist. But each time they said that << translate
                              the digit
                              >
                              Each bit is moved one place to the left,
                              the leftmost bit falls off the end
                              into the bit bucket (which should be emptied regularly),
                              and the rightmost bit is set to 0.
                              >
                              to the left (and >...)
                              >
                              Beware signed types when shifting. The danger is particularly - er -
                              dangerous when right-shifting negative numbers.
                              What happens to the sign bit?
                              (Answer: it depends on the implementation. )
                              >
                              but no one said if a << 1 mean
                              every time a * 2,
                              >
                              Not every time, no.
                              For example, consider a system where CHAR_BIT is 8.
                              >
                              unsigned char ch = 1;
                              >
                              ch <<= 1; /* ch is now 2 */
                              ch <<= 1; /* ch is now 4 */
                              ch <<= 1; /* ch is now 8 */
                              ch <<= 1; /* ch is now 16 */
                              ch <<= 1; /* ch is now 32 */
                              ch <<= 1; /* ch is now 64 */
                              ch <<= 1; /* ch is now 128 */
                              ch <<= 1; /* ch is now 0, even though 2 * 128 is 256. */
                              That example doesn't show a difference
                              between shifting and a doubling.

                              The result of each shift
                              is the same as what you would get from multiplication.

                              CHAR_BIT is 8

                              unsigned char ch = 1;

                              ch *= 2; /* ch is now 2 */
                              ch *= 2; /* ch is now 4 */
                              ch *= 2; /* ch is now 8 */
                              ch *= 2; /* ch is now 16 */
                              ch *= 2; /* ch is now 32 */
                              ch *= 2; /* ch is now 64 */
                              ch *= 2; /* ch is now 128 */
                              ch *= 2; /* ch is now 0, even though 2 * 128 is 256. */

                              --
                              pete

                              Comment

                              Working...