Search code examples
c#integerint128

In C#, how do I extract the two UInt64 values that make up a UInt128?


Suppose I have a UInt128 like this

UInt64 upperA = 7, lowerA = 8;
UInt128 foo = new(upperA, lowerA);
++foo;

And now I want to extract the two UInt64s from the updated foo.
If they were properties, I could do this

UInt64 upperB = foo.Upper, lowerB = foo.Lower;

But they aren't, so how do I get them?


Solution

  • By converting to UInt64 you can get the lower bits already; to get the upper one you can apply a bit shift first:

    var lower = (UInt64)foo;
    var upper = (UInt64)(foo >> 64);