I need to do some large integer math. Are there any classes or structs out there that represent a 128-bit integer and implement all of the usual operators?
BTW, I realize that decimal
can be used to represent a 96-bit int.
An Int128 is the "natural" type of GUID, IP6 addresses and more at low level but nevertheless true x86 instruction CPUID returns 4 integers (in 4 registers) that need to be "concatenated" to get the real value, the real "intent" was to return an Int128.
As an extension the integer scalar type __int128 is supported for targets which have an integer mode wide enough to hold 128 bits. Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer.
It's here in System.Numerics. "The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds."
var i = System.Numerics.BigInteger.Parse("10000000000000000000000000000000");
While BigInteger
is the best solution for most applications, if you have performance critical numerical computations, you can use the complete Int128
and UInt128
implementations in my Dirichlet.Numerics library. These types are useful if Int64
and UInt64
are too small but BigInteger
is too slow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With