shift right operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DeepaK K C

    shift right operator

    main()
    {
    int a =100;
    a = a>>32;
    printf(" %d", a);
    }

    it prints "a" as 100 only....but I am expecting a = 0.....
    can some one tell me the reason?

    bye
    Deepak\
  • Chris Dollin

    #2
    Re: shift right operator

    DeepaK K C wrote:
    [color=blue]
    > main()
    > {
    > int a =100;
    > a = a>>32;
    > printf(" %d", a);
    > }
    >
    > it prints "a" as 100 only....but I am expecting a = 0.....
    > can some one tell me the reason?[/color]

    It's a manifestation of undefined behaviour. (Some machines
    only have a 5-bit shift count, for example.)

    What happens if you turn your compiler warnings up?


    --
    Chris "electric hedgehog" Dollin

    Comment

    • David Resnick

      #3
      Re: shift right operator

      DeepaK K C wrote:
      [color=blue]
      > main()
      > {
      > int a =100;
      > a = a>>32;
      > printf(" %d", a);
      > }
      >
      > it prints "a" as 100 only....but I am expecting a = 0.....
      > can some one tell me the reason?
      >
      > bye
      > Deepak\[/color]

      Fixed version of your program:
      #include <stdio.h>
      int main(void)
      {
      int a = 100;
      a = a>>32;
      printf(" %d\n", a);
      return 0;
      }

      temp(558)$ gcc -Wall -o foo foo.c
      foo.c: In function `main':
      foo.c:6: warning: right shift count >= width of type
      temp(559)$ foo
      100

      Do you compile with warnings on? Unless int is 64 bits, you
      are invoking undefined behavior. Once you do that, what you
      get is up to the vagaries of fate, in gcc apparently it does
      nothing for shifts >= to the size, at least for the version
      I am using. Here is what the standard says:

      The integer promotions are performed on each of the
      operands. The type of the result is that of the promoted
      left operand. If the value of the right operand is negative
      or is greater than or equal to the width of the promoted
      left operand, the behavior is undefined.

      -David

      Comment

      • CBFalconer

        #4
        Re: shift right operator

        DeepaK K C wrote:[color=blue]
        >
        > main()
        > {
        > int a =100;
        > a = a>>32;
        > printf(" %d", a);
        > }
        >
        > it prints "a" as 100 only....but I am expecting a = 0.....
        > can some one tell me the reason?[/color]

        probably because (32 % 32 == 0). Other errors in your program:

        int main(void)

        main returns an int, say so. If you're not using any arguments,
        say so.

        failure to #include <stdio.h>

        The call to printf is invalid, and leads to undefined behavior.

        failure to return anything from main. return 0 needed.

        failure to indent properly.

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson


        Comment

        • Eric Sosman

          #5
          Re: shift right operator



          CBFalconer wrote:[color=blue]
          > DeepaK K C wrote:
          >[color=green]
          >>main()
          >>{
          >>int a =100;
          >>a = a>>32;
          >>printf(" %d", a);
          >>}
          >>
          >>it prints "a" as 100 only....but I am expecting a = 0.....
          >>can some one tell me the reason?[/color]
          >
          > probably because (32 % 32 == 0).[/color]

          That's just (off-topic) speculation. Chris Dollin
          has already provided the correct answer.
          [color=blue]
          > Other errors in your program:
          >
          > int main(void)
          >
          > main returns an int, say so. If you're not using any arguments,
          > say so.
          >
          > failure to #include <stdio.h>
          >
          > The call to printf is invalid, and leads to undefined behavior.[/color]

          It's only invalid in the absence of <stdio.h>, so I
          think you're double-counting. (And I'm surprised you didn't
          note the missing newline; it doesn't lead to U.B. but it's
          an error nonetheless.)
          [color=blue]
          > failure to return anything from main. return 0 needed.
          >
          > failure to indent properly.[/color]

          Oh, come on. How about "uninformat ive identifiers"
          or "inconsiste nt use of white space" or "lack of comments"
          or "failure to use `>>=' when the opportunity arose?"
          Not to mention "posting questions on a Wednesday" and
          "running afoul of someone who arose on the wrong side
          of bed?" Ease up a little.

          --
          Eric.Sosman@sun .com

          Comment

          • Peter Nilsson

            #6
            Re: shift right operator

            Chris Dollin wrote:[color=blue]
            > DeepaK K C wrote:
            >[color=green]
            > > main()
            > > {
            > > int a =100;
            > > a = a>>32;
            > > printf(" %d", a);
            > > }
            > >
            > > it prints "a" as 100 only....but I am expecting a = 0.....
            > > can some one tell me the reason?[/color]
            >
            > It's a manifestation of undefined behaviour. (Some machines
            > only have a 5-bit shift count, for example.)[/color]

            More precisely, shifting by the full width of an integer, or
            more, invokes undefined behaviour.

            A 32-bit shift of a (non-negative) int can be performed in a
            conforming way though...

            a = a >> 15 >> 15 >> 2;

            --
            Peter

            Comment

            • Christian Bau

              #7
              Re: shift right operator

              In article <1108594940.953 098.70200@g14g2 000cwa.googlegr oups.com>,
              "Peter Nilsson" <airia@acay.com .au> wrote:
              [color=blue]
              > Chris Dollin wrote:[color=green]
              > > DeepaK K C wrote:
              > >[color=darkred]
              > > > main()
              > > > {
              > > > int a =100;
              > > > a = a>>32;
              > > > printf(" %d", a);
              > > > }
              > > >
              > > > it prints "a" as 100 only....but I am expecting a = 0.....
              > > > can some one tell me the reason?[/color]
              > >
              > > It's a manifestation of undefined behaviour. (Some machines
              > > only have a 5-bit shift count, for example.)[/color]
              >
              > More precisely, shifting by the full width of an integer, or
              > more, invokes undefined behaviour.
              >
              > A 32-bit shift of a (non-negative) int can be performed in a
              > conforming way though...
              >
              > a = a >> 15 >> 15 >> 2;[/color]

              Much better to write:

              a = (a >> 15) >> 15) >> 2;

              Would you bet your life that you got your precedences right? Didn't
              think so. Would I bet your life that you get your precedences right?
              Absolutely.

              Comment

              • Eric Sosman

                #8
                Re: shift right operator



                Christian Bau wrote:[color=blue]
                > In article <1108594940.953 098.70200@g14g2 000cwa.googlegr oups.com>,
                > "Peter Nilsson" <airia@acay.com .au> wrote:[color=green]
                >>[...]
                >>A 32-bit shift of a (non-negative) int can be performed in a
                >>conforming way though...
                >>
                >> a = a >> 15 >> 15 >> 2;[/color]
                >
                >
                > Much better to write:
                >
                > a = (a >> 15) >> 15) >> 2;[/color]

                Ah, this is obviously some strange use of the phrase
                "much better" that I wasn't previously aware of ...

                --
                Eric.Sosman@sun .com

                Comment

                • Peter Nilsson

                  #9
                  Re: shift right operator

                  Christian Bau wrote:[color=blue]
                  > "Peter Nilsson" <airia@acay.com .au> wrote:[color=green]
                  > >
                  > > A 32-bit shift of a (non-negative) int can be performed in a
                  > > conforming way though...
                  > >
                  > > a = a >> 15 >> 15 >> 2;[/color]
                  >
                  > Much better to write:
                  >
                  > a = (a >> 15) >> 15) >> 2;[/color]

                  Goes to show: "If it ain't broken, don't fix it."
                  [color=blue]
                  > Would you bet your life that you got your precedences right?[/color]

                  Yes. Assignment operators have always had lower precedence than
                  shift operators. ;)

                  Obviously, you meant association rather than precedence, but even
                  there, yes! Why? Because in C++ I exploit the same left to right
                  association of shift operators all the time.

                  Ironic that C++ should make me a better C programmer, isn't it? ;)

                  --
                  Peter

                  Comment

                  • Christian Bau

                    #10
                    Re: shift right operator

                    In article <cv0km1$4p6$1@n ews1brm.Central .Sun.COM>,
                    Eric Sosman <eric.sosman@su n.com> wrote:
                    [color=blue]
                    > Christian Bau wrote:[color=green]
                    > > In article <1108594940.953 098.70200@g14g2 000cwa.googlegr oups.com>,
                    > > "Peter Nilsson" <airia@acay.com .au> wrote:[color=darkred]
                    > >>[...]
                    > >>A 32-bit shift of a (non-negative) int can be performed in a
                    > >>conforming way though...
                    > >>
                    > >> a = a >> 15 >> 15 >> 2;[/color]
                    > >
                    > >
                    > > Much better to write:
                    > >
                    > > a = (a >> 15) >> 15) >> 2;[/color]
                    >
                    > Ah, this is obviously some strange use of the phrase
                    > "much better" that I wasn't previously aware of ...[/color]

                    Maintainability . It is obvious to everybody what the intention of the
                    second version is, and what it does. It is impossible to determine what
                    the intention of the first version is (without additional comments in
                    the code), and I really have more important things to remember than
                    associativity of shift operators.

                    Comment

                    • Keith Thompson

                      #11
                      Re: shift right operator

                      Christian Bau <christian.bau@ cbau.freeserve. co.uk> writes:[color=blue]
                      > In article <cv0km1$4p6$1@n ews1brm.Central .Sun.COM>,
                      > Eric Sosman <eric.sosman@su n.com> wrote:
                      >[color=green]
                      >> Christian Bau wrote:[color=darkred]
                      >> > In article <1108594940.953 098.70200@g14g2 000cwa.googlegr oups.com>,
                      >> > "Peter Nilsson" <airia@acay.com .au> wrote:
                      >> >>[...]
                      >> >>A 32-bit shift of a (non-negative) int can be performed in a
                      >> >>conforming way though...
                      >> >>
                      >> >> a = a >> 15 >> 15 >> 2;
                      >> >
                      >> >
                      >> > Much better to write:
                      >> >
                      >> > a = (a >> 15) >> 15) >> 2;[/color]
                      >>
                      >> Ah, this is obviously some strange use of the phrase
                      >> "much better" that I wasn't previously aware of ...[/color]
                      >
                      > Maintainability . It is obvious to everybody what the intention of the
                      > second version is, and what it does. It is impossible to determine what
                      > the intention of the first version is (without additional comments in
                      > the code), and I really have more important things to remember than
                      > associativity of shift operators.[/color]

                      Um, you might want to count the parentheses in the second version.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • CBFalconer

                        #12
                        Re: shift right operator

                        Eric Sosman wrote:[color=blue]
                        > Christian Bau wrote:[color=green]
                        >> "Peter Nilsson" <airia@acay.com .au> wrote:[color=darkred]
                        >>> [...]
                        >>> A 32-bit shift of a (non-negative) int can be performed in a
                        >>> conforming way though...
                        >>>
                        >>> a = a >> 15 >> 15 >> 2;[/color]
                        >>
                        >> Much better to write:
                        >>
                        >> a = (a >> 15) >> 15) >> 2;[/color]
                        >
                        > Ah, this is obviously some strange use of the phrase
                        > "much better" that I wasn't previously aware of ...[/color]

                        For any int size up to and including 32 bits, it is considerably
                        simpler, and even more efficient, to write:

                        a = 0;

                        --
                        "If you want to post a followup via groups.google.c om, don't use
                        the broken "Reply" link at the bottom of the article. Click on
                        "show options" at the top of the article, then click on the
                        "Reply" at the bottom of the article headers." - Keith Thompson


                        Comment

                        • Lawrence Kirby

                          #13
                          Re: shift right operator

                          On Thu, 17 Feb 2005 00:24:13 +0000, Christian Bau wrote:

                          ....
                          [color=blue]
                          > Maintainability . It is obvious to everybody what the intention of the
                          > second version is, and what it does. It is impossible to determine what
                          > the intention of the first version is (without additional comments in
                          > the code), and I really have more important things to remember than
                          > associativity of shift operators.[/color]

                          I don't bother to remember all the precedence levels but associativity is
                          easy: all operators are left associative except unary, ternary and
                          assignment operators which are all right associative (according to the K&R
                          model). The only oddity is the function call operator which doesn't have a
                          fixed arity. That is left associative and still fits reasonably in the
                          rule (it isn't specifically a unary or ternary operator).

                          Lawrence





                          Comment

                          Working...