C# Reference ยท Types and Literals
The BigInteger struct
Reference for System.Numerics.BigInteger in C#: arbitrary-size integers, arithmetic, Pow and ModPow, parsing, conversions and performance notes.
BigInteger (namespace System.Numerics) is a signed integer with no fixed size. It grows as needed, so a factorial or a 2,000-bit key never overflows. The built-in types stop at long and Int128; when a result may exceed them and you cannot accept wrong answers, use BigInteger.
It is an immutable struct that supports all the usual operators (+ - * / % ++ --, comparisons, bitwise operators and shifts), so it reads like ordinary integer code. Every operation allocates a new value, which makes it much slower than long.
using System.Numerics; BigInteger big = BigInteger.Pow(2, 100); Console.WriteLine(big);
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 readonly struct BigInteger : IComparable<BigInteger>, IEquatable<BigInteger>, IBinaryInteger<BigInteger>, ISignedNumber<BigInteger>
| Member | Description |
|---|---|
BigInteger.Zero, One, MinusOne |
Common constants. |
Parse(string) / TryParse(...) |
Converts decimal text (and, with NumberStyles.HexNumber, hex text). |
Pow(BigInteger, int) |
Raises to a non-negative int exponent. |
ModPow(value, exponent, modulus) |
Computes value^exponent mod modulus without building the huge power. |
DivRem(a, b, out r) |
Quotient and remainder in one call. |
GreatestCommonDivisor(a, b) |
Always non-negative. |
Abs, Negate, Min, Max, Log, Log10 |
Helpers. |
Sign, IsEven, IsZero, IsOne, IsPowerOfTwo |
Properties. |
GetBitLength() |
Number of bits needed for the value, excluding the sign. |
ToByteArray() / new BigInteger(ReadOnlySpan<byte>) |
Two's-complement bytes, little-endian by default. |
Returns: a new BigInteger from each operation.
Errors: DivideByZeroException for division or remainder by zero; FormatException from Parse; OverflowException when an explicit conversion to a smaller type does not fit; ArgumentOutOfRangeException for a negative exponent in Pow.
Usage notes
- No overflow, but real cost. Multiplying two n-digit numbers takes more than linear time, and printing a huge value to decimal is slower still. Do not use
BigIntegerin a hot loop wherelongorInt128fits. - Implicit widening, explicit narrowing. Every built-in integer type converts implicitly to
BigInteger. Going back needs a cast, such as(long)big, which throwsOverflowExceptionif the value does not fit. - Division truncates toward zero like
int, so the remainder takes the sign of the dividend:-17 % 5is-2. - Use
ModPowfor cryptography-style math. ComputingPowfirst and then%creates an enormous intermediate number;ModPowstays small. - Floating-point conversion rounds.
(double)bigloses precision above 2^53 and gives infinity for huge values. - It is immutable.
x++creates a new value and reassigns the variable; other copies are not affected. - Equality compares values:
BigInteger.Parse("5") == 5istrue. The type works as a dictionary key. - Not a decimal type. For exact fractions use
decimalor a rational type you write yourself. - Alternatives.
Int128andUInt128are fixed-size and much faster when 128 bits is enough.