ulong bitwise shift

  • Thread starter Thread starter Francois St-Arnaud
  • Start date Start date
F

Francois St-Arnaud

Hello All,

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

public const ulong MASK_1 = 0x8000000000000000;

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

MASK_1
0x8000000000000000
MASK_1 >> 1
0x4000000000000000
MASK_1 >> 2
0x2000000000000000
MASK_1 >> 3
0x1000000000000000
MASK_1 >> 4
0x800000000000000
MASK_1 >> 5
0x400000000000000

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

Francois.
 
MASK_1
0x8000000000000000
MASK_1 >> 1
0x4000000000000000
MASK_1 >> 2
0x2000000000000000
MASK_1 >> 3
0x1000000000000000
MASK_1 >> 4
0x800000000000000
MASK_1 >> 5
0x400000000000000

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

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

Better do something like this:

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

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

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

------ooo---OOO---ooo------
 
Francois St-Arnaud said:
I have the following property in a C# class in a .NET 2003 project:

public const ulong MASK_1 = 0x8000000000000000;

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

MASK_1
0x8000000000000000
MASK_1 >> 1
0x4000000000000000
MASK_1 >> 2
0x2000000000000000
MASK_1 >> 3
0x1000000000000000
MASK_1 >> 4
0x800000000000000
MASK_1 >> 5
0x400000000000000

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

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.
 
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.
 
Back
Top