问题
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.
回答1:
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");
回答2:
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.
回答3:
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.)
回答4:
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(....)
}
回答5:
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.
回答6:
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.
回答7:
C# PCL library for computations with big numbers such as Int128 and Int256. https://github.com/everbytes/BigMath
回答8:
Here's an implementation of big integer from .net matters.
http://msdn.microsoft.com/en-us/magazine/cc163696.aspx
回答9:
Here's an implementation of Int128 in .NET: https://int128.codeplex.com/
回答10:
I believe Mono has a BigInteger implementation that you should be able to track down the source for.
来源:https://stackoverflow.com/questions/227731/int128-in-net