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 r
BigInteger is now a standard part of C# and friends in .NET 4.0. See:Gunnar Peipman's ASP.NET blog. Note that the CPU can generally work with ordinary integers much more quickly and in constant time, especially when using the usual math operators (+, -, /, ...) because these operators typically map directly to single CPU instructions.
With BigInteger, even the most basic math operations are much slower function calls to methods whose runtime varies with the size of the number. This is because BigInteger implements arbitrary precision arithmetic, which adds considerable but necessary overhead. The benefit is that BigIntegers are not limited to 64 or even 128 bits, but by available system memory (or about 2^64 bits of precision, whichever comes first). Read here.
C# PCL library for computations with big numbers such as Int128 and Int256. https://github.com/everbytes/BigMath
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.
GUID is backed by a 128 bit integer in .NET framework; though it doesn't come with any of the typical integer type methods.
I've written a handler for GUID before to treat it as a 128 bit integer, but this was for a company I worked for ~8 years ago. I no longer have access to the source code.
So if you need native support for a 128 bit integer, and don't want to rely on BigInteger for whatever reason, you could probably hack GUID to server your purposes.
I believe Mono has a BigInteger implementation that you should be able to track down the source for.
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");