This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 4.0+

# The BigInteger Structure

Under some circumstances the standard integer data types provided by the .NET framework provide a range of values that is restrictive. The BigInteger structure, introduced in version 4.0 of the framework, removes the size limits.

## BigInteger Methods

The BigInteger structure includes a number of methods. Some are identical to those of other integer types, except that they work with larger values. Others repeat the functionality provided by some of the operators described above. These are useful for languages that do not support overloaded operators but are unnecessary for C# developers. I will not describe these two groups of methods in this article. The remaining methods are described in the following sections.

### Abs

The Abs method is a static method of the BigInteger type. It returns the absolute value of a BigInteger. ie. If the initial value is positive it is returned unchanged. If it is negative, the sign is removed in the return value. The method, and some of the others listed below, are similar to those found in the Math class but work with larger values.

```BigInteger big1 = BigInteger.Parse("100000000000000000000");
BigInteger big2 = BigInteger.Parse("-100000000000000000000");

BigInteger abs = BigInteger.Abs(big1);  // 100000000000000000000
abs = BigInteger.Abs(big2);             // 100000000000000000000```

### Max and Min

The Max and Min methods each accept two arguments, both BigIntegers. Max returns the higher of the two numbers and Min returns the lower value.

```BigInteger big1 = BigInteger.Parse("100000000000000000000");
BigInteger big2 = BigInteger.Parse("-100000000000000000000");

BigInteger max = BigInteger.Max(big1, big2); // 100000000000000000000
BigInteger min = BigInteger.Min(big1, big2); // -100000000000000000000```

### Log and Log10

Log and Log10 are used to obtain the logarithm of a BigInteger value. When Log is used with only the BigInteger parameter, the natural logarithm is returned. You can add a second parameter, containing a double, to specify the base to use. Log10 only accepts a single BigInteger argument and returns the base-10 logarithm of that value.

```double log = BigInteger.Log(big);       // 46.051701859880907
double log2 = BigInteger.Log(big, 2);   // 66.438561897747235
double log10 = BigInteger.Log10(big);   // 19.999999999999996```

### Pow

The Pow method allows you to raise a BigInteger to a given power. The method differs slightly from Math.Pow as the power value is provided as an integer, rather than a double.

`BigInteger big = BigInteger.Pow(2, 100); // 1267650600228229401496703205376`

### ModPow

ModPow combines raising a value to a specified power and executing a modulus operation. The method accepts three parameters, each a BigInteger. The first argument is raised to the power of the second. A modulus division is then performed upon the result, using the value in the third parameter as the divisor.

The code below raises 2 to the power of 100 and then finds the remainder of dividing the result by 1,000. Note that the returned value is the last three digits of the result of the previous, Pow example.

`BigInteger big = BigInteger.ModPow(2, 100, 1000); // 376`

### GreatestCommonDivisor

The last method we will review is GreatestCommonDivisor. This returns the largest integer value that a pair of BigIntegers can both be divided by without remainder. A GCD algorithm is described in the "Rational Number Arithmetic" article. With the introduction of BigIntegers, this calculation is built into the .NET framework.

```BigInteger big1 = BigInteger.Parse("41758540882408627201");
BigInteger big2 = BigInteger.Parse("19120211505964799");
BigInteger gcd = BigInteger.GreatestCommonDivisor(big1, big2); // 479001599```

## BigInteger Properties

In the final section of this article we will examine the properties provided by the BigInteger structure. These fall into two categories. Firstly there are several static properties that can be used to generate fixed BigInteger values without using a constructor, assignment operator or parse method. Secondly there are some instance properties that return information about a BigInteger value.

### Static Properties

Three static properties are available in the BigInteger structure. Each simply returns a predefined BigInteger value. The properties generate values of one, zero or negative one. Some developers may prefer to use these properties for readability. Others may prefer to simply assign a literal when creating a new instance.

```BigInteger big;
big = BigInteger.Zero;      // 0
big = BigInteger.One;       // 1
big = BigInteger.MinusOne;  // -1```

### Instance Properties

There are five instance properties provided by the BigInteger structure in .NET 4.0. The first four of these each return a Boolean value that describes the value. You can use these to quickly check if the value is one or zero, if the value is even or if it is an exact power of 2. Examples of each are shown below:

```bool result;
result = BigInteger.One.IsOne;          // true
result = BigInteger.One.IsZero;         // false
result = BigInteger.One.IsEven;         // false
result = BigInteger.One.IsPowerOfTwo;   // true```

The fifth property returns an integer that represents the sign of the BigInteger. If the BigInteger is negative the return value of the Sign property is -1. If positive the result is 1 and if the original value is zero, the result will also be zero.

int sign = BigInteger.One.Sign; // 1
5 November 2010