What type should I use for a 128 bit number in in .Net?

C#.NetInt128

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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");

Solution 2 - C#

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.

Solution 3 - C#

No, there's nothing in .NET <= 3.5. I'm hoping/expecting that BigInteger will make its return in .NET 4.0. (It was cut from .NET 3.5.)

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

If you don't mind making reference to the J# library (vjslib.dll included with VS by default) there is already and implementation of BigInteger in .NET

using java.math;

public static void Main(){
    BigInteger biggy = new BigInteger(....)

}

Solution 7 - C#

C# PCL library for computations with big numbers such as Int128 and Int256. https://github.com/everbytes/BigMath

Solution 8 - C#

I believe Mono has a BigInteger implementation that you should be able to track down the source for.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam TegenView Question on Stackoverflow
Solution 1 - C#LarsenalView Answer on Stackoverflow
Solution 2 - C#Rick SladkeyView Answer on Stackoverflow
Solution 3 - C#Jon SkeetView Answer on Stackoverflow
Solution 4 - C#Charles BurnsView Answer on Stackoverflow
Solution 5 - C#Brett AllenView Answer on Stackoverflow
Solution 6 - C#sbeskurView Answer on Stackoverflow
Solution 7 - C#LessneekView Answer on Stackoverflow
Solution 8 - C#CraigView Answer on Stackoverflow