1>>x behaviour

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

    1>>x behaviour

    Hello,

    I have a little question about operator >between integers: does
    anyone know why, when I write:
    "1 >x", the returned value is 1 for all values multiple of 32 and 0
    else (this does not hurt me, 1 does).
    When I do the same thing with : "1L >x", only multiples of 64 do
    this.
    I thought 0 was the only possible result for any x 0.
    Why is it not the case ? And how to correct this simply -- in one
    instruction, else I did it by:

    long longFoo = intBar;
    longFoo >>= x;
    return (int) longFoo;

    .... but this looks weird to do this.

    Thanks in advance to any answer.

  • Morten Wennevik

    #2
    Re: 1>>x behaviour

    Hi Vivien,

    The shift operation is done using the lowest 5 bits of the x operand for
    Int32, and lowest 6 bits for Int64.

    1 >32 is the same as 1 >0


    On Thu, 23 Nov 2006 12:20:55 +0100, Vivien Parlat <pocky6@gmail.c omwrote:
    Hello,
    >
    I have a little question about operator >between integers: does
    anyone know why, when I write:
    "1 >x", the returned value is 1 for all values multiple of 32 and 0
    else (this does not hurt me, 1 does).
    When I do the same thing with : "1L >x", only multiples of 64 do
    this.
    I thought 0 was the only possible result for any x 0.
    Why is it not the case ? And how to correct this simply -- in one
    instruction, else I did it by:
    >
    long longFoo = intBar;
    longFoo >>= x;
    return (int) longFoo;
    >
    ... but this looks weird to do this.
    >
    Thanks in advance to any answer.
    >


    --
    Happy Coding!
    Morten Wennevik [C# MVP]

    Comment

    • Jay

      #3
      Re: 1&gt;&gt;x behaviour

      So is that a bug?

      "Morten Wennevik" <MortenWennevik @hotmail.comwro te in message
      news:op.tjgz0cd 2klbvpo@tr024.b ouvet.no...
      Hi Vivien,

      The shift operation is done using the lowest 5 bits of the x operand for
      Int32, and lowest 6 bits for Int64.

      1 >32 is the same as 1 >0


      On Thu, 23 Nov 2006 12:20:55 +0100, Vivien Parlat <pocky6@gmail.c omwrote:
      Hello,
      >
      I have a little question about operator >between integers: does
      anyone know why, when I write:
      "1 >x", the returned value is 1 for all values multiple of 32 and 0
      else (this does not hurt me, 1 does).
      When I do the same thing with : "1L >x", only multiples of 64 do
      this.
      I thought 0 was the only possible result for any x 0.
      Why is it not the case ? And how to correct this simply -- in one
      instruction, else I did it by:
      >
      long longFoo = intBar;
      longFoo >>= x;
      return (int) longFoo;
      >
      ... but this looks weird to do this.
      >
      Thanks in advance to any answer.
      >


      --
      Happy Coding!
      Morten Wennevik [C# MVP]


      Comment

      • Morten Wennevik

        #4
        Re: 1&gt;&gt;x behaviour

        On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
        So is that a bug?
        >
        It's by design. When you think about it, shifting a 32 bit integer with
        anything more than 31 places doesn't make any sense.




        --
        Happy Coding!
        Morten Wennevik [C# MVP]

        Comment

        • garyusenet@myway.com

          #5
          Re: 1&gt;&gt;x behaviour

          What does shift do? I read this thread and was intrigued but don't
          understand it's use.
          I am just learning the basics.

          Please advise.

          Morten Wennevik wrote:
          On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
          >
          So is that a bug?
          >
          It's by design. When you think about it, shifting a 32 bit integer with
          anything more than 31 places doesn't make any sense.
          >

          >
          >
          --
          Happy Coding!
          Morten Wennevik [C# MVP]

          Comment

          • Marc Gravell

            #6
            Re: 1&gt;&gt;x behaviour

            But there is an oddity here;

            shifting by 5 and then by 7 is equivalent to shifting by 12
            shifting by 16 and then by 16 would leave nothing; yet shifting by 32 in one
            go doesn't always...

            Intriguing, but not a huge problem generally...

            Marc


            Comment

            • Marc Gravell

              #7
              Re: 1&gt;&gt;x behaviour

              In brief, it works on the bitwise representation of a value, and moves
              everything left or right by {x} places, dropping bits off one end, and
              back-filling with 0s at the other.

              e.g. integer 1 in binary is ...00001 (=int 1); left shift gives ...00010
              (=int 2); right shift this twice yields ...00000 (=int 0)



              Marc


              Comment

              • Morten Wennevik

                #8
                Re: 1&gt;&gt;x behaviour

                Hi,

                Shifting is used when you need information at bitlevel

                Consider the number 68437143. In the file it would be represented as

                00000100 00010100 01000100 10010111

                Hidden inside is the dimensions of an image where the information is
                arranged as

                0000 010000 01010001000 10010010111
                Unused Colors Width Height
                4 6 11 11

                To be able to get this information we need to do bit manipulation
                By filtering the number with 2047 we get the lower 11 bits

                000001000001010 001000100100101 11 (68437143)
                &
                000000000000000 000000111111111 11 (2047)
                =
                000000000000000 000000100100101 11 (1175)


                [Code]
                int n = 68437143;
                int filter = 2047
                int Height = n & filter; // 1175


                To get Width we shift the bits 11 places to the right

                000001000001010 001000100100101 11 (68437143)
                >11
                000000000000000 010000010100010 00 (33416) 10010010111 has been pushed out

                [Code]
                n = n >11;

                Do the same filtering to get the Width

                000000000000000 010000010100010 00 (33416)
                &
                000000000000000 000000111111111 11 (2047)
                =
                000000000000000 000000010100010 00 (648)

                Shift right 11 more places

                000000000000000 010000010100010 00 (33416)
                >11
                000000000000000 000000000000100 00 (16)



                Summarized in code it will look like

                int number = 68437143;
                int filter = 2047;
                int height = number & filter;
                number = number >11; // you could also write number >>= 11
                int width = number & filter;
                int color = number >11;

                Image size = 648x1175x16



                For instance, image files pack values int bits, not bytes.

                On Fri, 24 Nov 2006 10:44:45 +0100, <garyusenet@myw ay.comwrote:
                What does shift do? I read this thread and was intrigued but don't
                understand it's use.
                I am just learning the basics.
                >
                Please advise.
                >
                Morten Wennevik wrote:
                >
                >On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
                >>
                So is that a bug?
                >
                >>
                >It's by design. When you think about it, shifting a 32 bit integer with
                >anything more than 31 places doesn't make any sense.
                >>
                >http://msdn2.microsoft .com/en-us/library/xt18et0d.aspx
                >>
                >>
                >--
                >Happy Coding!
                >Morten Wennevik [C# MVP]
                >


                --
                Happy Coding!
                Morten Wennevik [C# MVP]

                Comment

                • Jay

                  #9
                  Re: 1&gt;&gt;x behaviour

                  Yes it doesn't make sense, but it ought to do something sensible rather than something quirky. For
                  example, if you shift by more than 32 bits, it should set the variable to zero (assuming unsigned
                  variables for now) since this is the true answer if you did shift more than 32 bits.

                  "Morten Wennevik" <MortenWennevik @hotmail.comwro te in message
                  news:op.tjik1gg 4klbvpo@tr024.b ouvet.no...
                  On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
                  So is that a bug?
                  >
                  It's by design. When you think about it, shifting a 32 bit integer with
                  anything more than 31 places doesn't make any sense.




                  --
                  Happy Coding!
                  Morten Wennevik [C# MVP]


                  Comment

                  • Morten Wennevik

                    #10
                    Re: 1&gt;&gt;x behaviour

                    On Fri, 24 Nov 2006 14:22:45 +0100, Jay <nospamwrote:
                    Yes it doesn't make sense, but it ought to do something sensible rather
                    than something quirky. For
                    example, if you shift by more than 32 bits, it should set the variable
                    to zero (assuming unsigned
                    variables for now) since this is the true answer if you did shift more
                    than 32 bits.
                    >
                    Not if there is more information in the shift parameter than should be
                    used for shifting. For instance, if you have a value that needs to be
                    shiftet several times each time a different number of places. You could
                    then store all the shift values in a single integer and shift the shift
                    value n places each shift.

                    int someNumber = 12345667;
                    int shiftNumber = 12345;

                    int value1 = someNumber;
                    someNumber >>= shiftNumber;
                    shiftNumber >>= 5;
                    int value2 = someNumber;
                    someNumber >>= shiftNumber
                    shiftNumber >>= 5
                    ....

                    I can't see much reason to use it, but there may be some performance to be
                    gained in a heavy duty loop.


                    --
                    Happy Coding!
                    Morten Wennevik [C# MVP]

                    Comment

                    • Jay

                      #11
                      Re: 1&gt;&gt;x behaviour

                      Thanks for the info. It sounds like the sort of behaviour I'd expect from C, but not what I'd expect
                      from C# which I tend to think of as being cleaned up of quirks. But if that's the way it is, then
                      that's the way it is. I just have to be careful when I code I guess.

                      "Morten Wennevik" <MortenWennevik @hotmail.comwro te in message
                      news:op.tjiwgkt 7klbvpo@tr024.b ouvet.no...
                      On Fri, 24 Nov 2006 14:22:45 +0100, Jay <nospamwrote:
                      Yes it doesn't make sense, but it ought to do something sensible rather
                      than something quirky. For
                      example, if you shift by more than 32 bits, it should set the variable
                      to zero (assuming unsigned
                      variables for now) since this is the true answer if you did shift more
                      than 32 bits.
                      >
                      Not if there is more information in the shift parameter than should be
                      used for shifting. For instance, if you have a value that needs to be
                      shiftet several times each time a different number of places. You could
                      then store all the shift values in a single integer and shift the shift
                      value n places each shift.

                      int someNumber = 12345667;
                      int shiftNumber = 12345;

                      int value1 = someNumber;
                      someNumber >>= shiftNumber;
                      shiftNumber >>= 5;
                      int value2 = someNumber;
                      someNumber >>= shiftNumber
                      shiftNumber >>= 5
                      ....

                      I can't see much reason to use it, but there may be some performance to be
                      gained in a heavy duty loop.


                      --
                      Happy Coding!
                      Morten Wennevik [C# MVP]


                      Comment

                      Working...