ulong bitwise shift

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francois St-Arnaud

    ulong bitwise shift

    Hello All,

    I have the following property in a C# class in a .NET 2003 project:

    public const ulong MASK_1 = 0x8000000000000 000;

    I find the following observations unsettling (from my Command Window -
    Immediate):

    MASK_1
    0x8000000000000 000
    MASK_1 >> 1
    0x4000000000000 000
    MASK_1 >> 2
    0x2000000000000 000
    MASK_1 >> 3
    0x1000000000000 000
    MASK_1 >> 4
    0x8000000000000 00
    MASK_1 >> 5
    0x4000000000000 00

    What? A wraparound on the 4 MSBs? I'm I missing something?

    Francois.


  • Peter Koen

    #2
    Re: ulong bitwise shift

    "Francois St-Arnaud" <Sfrancois.star naudP@Avideotro n.caM> wrote in
    news:q4Pnb.9257 $zB4.37753@wagn er.videotron.ne t:
    [color=blue]
    > MASK_1
    > 0x8000000000000 000
    > MASK_1 >> 1
    > 0x4000000000000 000
    > MASK_1 >> 2
    > 0x2000000000000 000
    > MASK_1 >> 3
    > 0x1000000000000 000
    > MASK_1 >> 4
    > 0x8000000000000 00
    > MASK_1 >> 5
    > 0x4000000000000 00
    >
    > What? A wraparound on the 4 MSBs? I'm I missing something?
    >[/color]

    Please, count the zeroes. Your code is just missing to print out leading
    0's.

    Better do something like this:

    Console.WriteLi ne("0x{0:X16}" , MASK);

    --
    ------ooo---OOO---ooo------

    Peter Koen - www.kema.at
    MCAD CAI/RS CASE/RS IAT

    ------ooo---OOO---ooo------

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: ulong bitwise shift

      Francois St-Arnaud <Sfrancois.star naudP@Avideotro n.caM> wrote:[color=blue]
      > I have the following property in a C# class in a .NET 2003 project:
      >
      > public const ulong MASK_1 = 0x8000000000000 000;
      >
      > I find the following observations unsettling (from my Command Window -
      > Immediate):
      >
      > MASK_1
      > 0x8000000000000 000
      > MASK_1 >> 1
      > 0x4000000000000 000
      > MASK_1 >> 2
      > 0x2000000000000 000
      > MASK_1 >> 3
      > 0x1000000000000 000
      > MASK_1 >> 4
      > 0x8000000000000 00
      > MASK_1 >> 5
      > 0x4000000000000 00
      >
      > What? A wraparound on the 4 MSBs? I'm I missing something?[/color]

      That looks fine to me - note that there are fewer 0s at the end of the
      versions which are shifted right by 4 or 5 bits than there are for the
      other ones. If you shift to a decimal display rather than hex it will
      be more obvious.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Francois St-Arnaud

        #4
        Re: ulong bitwise shift

        AH! Thanks! I'm going blind!
        I think that I got confused because I had the same code in a C++ project in
        another .NET window, but the tooltip popups and the Watch window showed the
        leading zeroes; not so in a C# project.

        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.1a09d0 dcfc90ef6798998 4@msnews.micros oft.com...[color=blue]
        > Francois St-Arnaud <Sfrancois.star naudP@Avideotro n.caM> wrote:[color=green]
        > > I have the following property in a C# class in a .NET 2003 project:
        > >
        > > public const ulong MASK_1 = 0x8000000000000 000;
        > >
        > > I find the following observations unsettling (from my Command Window -
        > > Immediate):
        > >
        > > MASK_1
        > > 0x8000000000000 000
        > > MASK_1 >> 1
        > > 0x4000000000000 000
        > > MASK_1 >> 2
        > > 0x2000000000000 000
        > > MASK_1 >> 3
        > > 0x1000000000000 000
        > > MASK_1 >> 4
        > > 0x8000000000000 00
        > > MASK_1 >> 5
        > > 0x4000000000000 00
        > >
        > > What? A wraparound on the 4 MSBs? I'm I missing something?[/color]
        >
        > That looks fine to me - note that there are fewer 0s at the end of the
        > versions which are shifted right by 4 or 5 bits than there are for the
        > other ones. If you shift to a decimal display rather than hex it will
        > be more obvious.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        Working...