Skip to content
Tutorials Center
My learning

Topics

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.

C#
using System.Numerics;

BigInteger big = BigInteger.Pow(2, 100);
Console.WriteLine(big);

Try it

C# Factorials and powers

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# Division, modular math and parsing

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 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 BigInteger in a hot loop where long or Int128 fits.
  • Implicit widening, explicit narrowing. Every built-in integer type converts implicitly to BigInteger. Going back needs a cast, such as (long)big, which throws OverflowException if the value does not fit.
  • Division truncates toward zero like int, so the remainder takes the sign of the dividend: -17 % 5 is -2.
  • Use ModPow for cryptography-style math. Computing Pow first and then % creates an enormous intermediate number; ModPow stays small.
  • Floating-point conversion rounds. (double)big loses 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") == 5 is true. The type works as a dictionary key.
  • Not a decimal type. For exact fractions use decimal or a rational type you write yourself.
  • Alternatives. Int128 and UInt128 are fixed-size and much faster when 128 bits is enough.