Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

integer

int128

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?

like image 634
Hal Heinrich Avatar asked Oct 18 '25 12:10

Hal Heinrich


1 Answers

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);
like image 66
H.B. Avatar answered Oct 21 '25 02:10

H.B.