Skip to content
Tutorials Center
My learning

Topics

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;.

C#
using System.Numerics;

BigInteger f = 1;
for (int i = 2; i <= 25; i++) { f *= i; }   // 25! = 15511210043330985984000000

Try it

C# Factorials, Pow and conversions

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.

C# Number theory helpers

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

C#
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.7 truncates to 3. A BigInteger converts to double explicitly and becomes Infinity when too large; the result loses precision above 2โตยณ.
  • Division truncates toward zero and % follows the dividend's sign, as for int. There is no unsigned version; for a non-negative remainder use ((a % m) + m) % m.
  • Use ModPow, never Pow then %. BigInteger.Pow(2, 1_000_000) % m builds a number with 300,000 digits first. ModPow keeps every intermediate value below the modulus.
  • Immutable and a struct. x += 1 creates 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.Cryptography for real cryptographic work.
  • Smaller alternatives. Int128 and UInt128 hold 128 bits without allocation. Use them if you know the size limit.