Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Int128 in .Net?

Tags:

c#

.net

int128

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.

like image 933
Adam Tegen Avatar asked Oct 22 '08 22:10

Adam Tegen


People also ask

Is there Int128?

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.

How do I use 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.


2 Answers

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"); 
like image 99
Larsenal Avatar answered Sep 17 '22 15:09

Larsenal


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.

like image 23
Rick Sladkey Avatar answered Sep 17 '22 15:09

Rick Sladkey