ulong mixed = (ulong)high << 32 | low;
The cast is very important. If you omit the cast, considering the fact that high
is of type uint
(which is 32 bits), you'll be shifting a 32 bit value 32 bits to the left. Shift operators on 32 bit variables will use shift stuff by right-hand-side
mod 32. Effectively, shifting a uint
32 bits to the left is a no-op. Casting to ulong
prevents this.
Verifying this fact is easy:
uint test = 1u;
Console.WriteLine(test << 32); // prints 1
Console.WriteLine((ulong)test << 32); // prints (ulong)uint.MaxValue + 1