C# Reference ยท Numbers and Math
C# BigInteger operations
Reference for System.Numerics.BigInteger: arbitrary-size integers, arithmetic, Pow, ModPow, GreatestCommonDivisor, Parse and conversion rules.
System.Numerics.BigInteger is an integer with no fixed size: it grows as needed, limited only by memory. Use it when long (about 9.2 ร 10ยนโธ) or Int128 is not enough: factorials, Fibonacci numbers, exact cryptography math and big counts. It is immutable, a value type, and supports the normal operators, so a * b just works.
BigInteger never overflows, but it is much slower than int and long and allocates. Division by zero still throws. You need using System.Numerics;.
using System.Numerics; BigInteger f = 1; for (int i = 2; i <= 25; i++) { f *= i; } // 25! = 15511210043330985984000000
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public struct BigInteger : IBinaryInteger<BigInteger>, ISignedNumber<BigInteger> ... public BigInteger(int value) // also long, uint, ulong, float, double, decimal, byte[] public static BigInteger Parse(string value) public static bool TryParse(string? value, out BigInteger result) public static BigInteger Pow(BigInteger value, int exponent) public static BigInteger ModPow(BigInteger value, BigInteger exponent, BigInteger modulus) public static BigInteger GreatestCommonDivisor(BigInteger left, BigInteger right) public static BigInteger DivRem(BigInteger dividend, BigInteger divisor, out BigInteger remainder) public static double Log(BigInteger value, double baseValue) public static BigInteger Abs(BigInteger value) public long GetBitLength() public byte[] ToByteArray()
| Name | Description |
|---|---|
value |
The base, or the number to process. |
exponent |
For Pow, an int (non-negative). For ModPow, a BigInteger (non-negative). |
modulus |
For ModPow, the divisor. Must not be zero. |
Returns: a BigInteger, except Log, Log10 and Sign which return double and int.
Errors: DivideByZeroException for division, remainder or modulus of zero; ArgumentOutOfRangeException for a negative exponent; OverflowException when an explicit cast to a smaller type does not fit; FormatException from Parse. Converting NaN or an infinity from double throws OverflowException.
Usage notes
- No overflow, but real cost. Multiplication of two n-digit numbers takes time that grows faster than linearly. Printing a number with
ToString()is slower still, because it converts from binary to decimal. - Implicit widening, explicit narrowing. Any built-in integer converts implicitly to
BigInteger. Going back requires a cast that throws if the value does not fit. - Mixing with
double.(BigInteger)3.7truncates to3. ABigIntegerconverts todoubleexplicitly and becomesInfinitywhen too large; the result loses precision above 2โตยณ. - Division truncates toward zero and
%follows the dividend's sign, as forint. There is no unsigned version; for a non-negative remainder use((a % m) + m) % m. - Use
ModPow, neverPowthen%.BigInteger.Pow(2, 1_000_000) % mbuilds a number with 300,000 digits first.ModPowkeeps every intermediate value below the modulus. - Immutable and a struct.
x += 1creates a new value each time. In tight loops over huge numbers, minimize the number of temporary values. - Bit operations (
&,|,^,<<,>>) use infinite two's complement for negative values.>>rounds toward negative infinity. - Not cryptographically safe by itself. The arithmetic is not constant-time. Use the types in
System.Security.Cryptographyfor real cryptographic work. - Smaller alternatives.
Int128andUInt128hold 128 bits without allocation. Use them if you know the size limit.
Related
type-bigintandtype-int128num-mathandnum-generic-mathnum-parse- Tutorial: Integers