Reputation: 25895
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.
Upvotes: 87
Views: 78163
Reputation: 2424
.NET 8 has added Int128
and UInt128
to System.Text.Json.JsonSerializer
.
These two types Int128
and UInt128
were introduced in .NET 7
Console.WriteLine(JsonSerializer.Serialize(
[ Int128.MaxValue, UInt128.MaxValue ]
));
// [170141183460469231731687303715884105727,340282366920938463463374607431768211455]
Upvotes: 1
Reputation: 41834
System.Int128
and System.UInt128
have been available since .NET Core 7.0 Preview 5
They were implemented in Add support for Int128 and UInt128 data types
I don't know why they aren't in the .NET 7 Preview 5 announcement but in the upcoming .NET 7 Preview 6 announcement there'll also be Int128Converter
and UInt128Converter
for the new types in Preview 5
They didn't have C# support yet though, just like System.Half
, so you'll have to use Int128
explicitly instead of using a native C# keyword
Upvotes: 35
Reputation: 1501163
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.)
Upvotes: 15
Reputation: 51166
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");
Upvotes: 62
Reputation: 10602
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 264 bits of precision, whichever comes first).
Read here.
Upvotes: 6
Reputation: 1874
C# PCL library for computations with big numbers such as Int128 and Int256. https://github.com/lessneek/BigMath
Upvotes: 3
Reputation: 5487
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.
Upvotes: 4
Reputation: 34250
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.
Upvotes: 64
Reputation: 2290
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(....)
}
Upvotes: 3
Reputation: 4383
I believe Mono has a BigInteger implementation that you should be able to track down the source for.
Upvotes: -3