# BigInteger Structure

**.NET Framework 4.5**

Represents an arbitrarily large signed integer.

**Namespace:**System.Numerics

**Assembly:**System.Numerics (in System.Numerics.dll)

The BigInteger type exposes the following members.

Name | Description | |
---|---|---|

BigInteger(Byte[]) | Initializes a new instance of the BigInteger structure using the values in a byte array. | |

BigInteger(Decimal) | Initializes a new instance of the BigInteger structure using a Decimal value. | |

BigInteger(Double) | Initializes a new instance of the BigInteger structure using a double-precision floating-point value. | |

BigInteger(Int32) | Initializes a new instance of the BigInteger structure using a 32-bit signed integer value. | |

BigInteger(Int64) | Initializes a new instance of the BigInteger structure using a 64-bit signed integer value. | |

BigInteger(Single) | Initializes a new instance of the BigInteger structure using a single-precision floating-point value. | |

BigInteger(UInt32) | Initializes a new instance of the BigInteger structure using an unsigned 32-bit integer value. | |

BigInteger(UInt64) | Initializes a new instance of the BigInteger structure with an unsigned 64-bit integer value. |

Name | Description | |
---|---|---|

IsEven | Indicates whether the value of the current BigInteger object is an even number. | |

IsOne | Indicates whether the value of the current BigInteger object is BigInteger.One. | |

IsPowerOfTwo | Indicates whether the value of the current BigInteger object is a power of two. | |

IsZero | Indicates whether the value of the current BigInteger object is BigInteger.Zero. | |

MinusOne | Gets a value that represents the number negative one (-1). | |

One | Gets a value that represents the number one (1). | |

Sign | Gets a number that indicates the sign (negative, positive, or zero) of the current BigInteger object. | |

Zero | Gets a value that represents the number 0 (zero). |

Name | Description | |
---|---|---|

Abs | Gets the absolute value of a BigInteger object. | |

Add | Adds two BigInteger values and returns the result. | |

Compare | Compares two BigInteger values and returns an integer that indicates whether the first value is less than, equal to, or greater than the second value. | |

CompareTo(BigInteger) | Compares this instance to a second BigInteger and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object. | |

CompareTo(Int64) | Compares this instance to a signed 64-bit integer and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the signed 64-bit integer. | |

CompareTo(Object) | Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object. | |

CompareTo(UInt64) | Compares this instance to an unsigned 64-bit integer and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the unsigned 64-bit integer. | |

Divide | Divides one BigInteger value by another and returns the result. | |

DivRem | Divides one BigInteger value by another, returns the result, and returns the remainder in an output parameter. | |

Equals(BigInteger) | Returns a value that indicates whether the current instance and a specified BigInteger object have the same value. | |

Equals(Int64) | Returns a value that indicates whether the current instance and a signed 64-bit integer have the same value. | |

Equals(Object) | Returns a value that indicates whether the current instance and a specified object have the same value. (Overrides ValueType.Equals(Object).) | |

Equals(UInt64) | Returns a value that indicates whether the current instance and an unsigned 64-bit integer have the same value. | |

GetHashCode | Returns the hash code for the current BigInteger object. (Overrides ValueType.GetHashCode().) | |

GetType | Gets the Type of the current instance. (Inherited from Object.) | |

GreatestCommonDivisor | Finds the greatest common divisor of two BigInteger values. | |

Log(BigInteger) | Returns the natural (base e) logarithm of a specified number. | |

Log(BigInteger, Double) | Returns the logarithm of a specified number in a specified base. | |

Log10 | Returns the base 10 logarithm of a specified number. | |

Max | Returns the larger of two BigInteger values. | |

Min | Returns the smaller of two BigInteger values. | |

ModPow | Performs modulus division on a number raised to the power of another number. | |

Multiply | Returns the product of two BigInteger values. | |

Negate | Negates a specified BigInteger value. | |

Parse(String) | Converts the string representation of a number to its BigInteger equivalent. | |

Parse(String, NumberStyles) | Converts the string representation of a number in a specified style to its BigInteger equivalent. | |

Parse(String, IFormatProvider) | Converts the string representation of a number in a specified culture-specific format to its BigInteger equivalent. | |

Parse(String, NumberStyles, IFormatProvider) | Converts the string representation of a number in a specified style and culture-specific format to its BigInteger equivalent. | |

Pow | Raises a BigInteger value to the power of a specified value. | |

Remainder | Performs integer division on two BigInteger values and returns the remainder. | |

Subtract | Subtracts one BigInteger value from another and returns the result. | |

ToByteArray | Converts a BigInteger value to a byte array. | |

ToString() | Converts the numeric value of the current BigInteger object to its equivalent string representation. (Overrides ValueType.ToString().) | |

ToString(IFormatProvider) | Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified culture-specific formatting information. | |

ToString(String) | Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified format. | |

ToString(String, IFormatProvider) | Converts the numeric value of the current BigInteger object to its equivalent string representation by using the specified format and culture-specific format information. | |

TryParse(String, BigInteger) | Tries to convert the string representation of a number to its BigInteger equivalent, and returns a value that indicates whether the conversion succeeded. | |

TryParse(String, NumberStyles, IFormatProvider, BigInteger) | Tries to convert the string representation of a number in a specified style and culture-specific format to its BigInteger equivalent, and returns a value that indicates whether the conversion succeeded. |

Name | Description | |
---|---|---|

Addition | Adds the values of two specified BigInteger objects. | |

BitwiseAnd | Performs a bitwise And operation on two BigInteger values. | |

BitwiseOr | Performs a bitwise Or operation on two BigInteger values. | |

Decrement | Decrements a BigInteger value by 1. | |

Division | Divides a specified BigInteger value by another specified BigInteger value by using integer division. | |

Equality(BigInteger, Int64) | Returns a value that indicates whether a BigInteger value and a signed long integer value are equal. | |

Equality(BigInteger, BigInteger) | Returns a value that indicates whether the values of two BigInteger objects are equal. | |

Equality(BigInteger, UInt64) | Returns a value that indicates whether a BigInteger value and an unsigned long integer value are equal. | |

Equality(Int64, BigInteger) | Returns a value that indicates whether a signed long integer value and a BigInteger value are equal. | |

Equality(UInt64, BigInteger) | Returns a value that indicates whether an unsigned long integer value and a BigInteger value are equal. | |

ExclusiveOr | Performs a bitwise exclusive Or (XOr) operation on two BigInteger values. | |

Explicit(BigInteger to Int32) | Defines an explicit conversion of a BigInteger object to a 32-bit signed integer value. | |

Explicit(BigInteger to Decimal) | Defines an explicit conversion of a BigInteger object to a Decimal value. | |

Explicit(BigInteger to Int16) | Defines an explicit conversion of a BigInteger object to a 16-bit signed integer value. | |

Explicit(BigInteger to SByte) | Defines an explicit conversion of a BigInteger object to a signed 8-bit value. | |

Explicit(BigInteger to UInt16) | Defines an explicit conversion of a BigInteger object to an unsigned 16-bit integer value. | |

Explicit(BigInteger to UInt32) | Defines an explicit conversion of a BigInteger object to an unsigned 32-bit integer value. | |

Explicit(BigInteger to Int64) | Defines an explicit conversion of a BigInteger object to a 64-bit signed integer value. | |

Explicit(BigInteger to Single) | Defines an explicit conversion of a BigInteger object to a single-precision floating-point value. | |

Explicit(BigInteger to Double) | Defines an explicit conversion of a BigInteger object to a Double value. | |

Explicit(BigInteger to UInt64) | Defines an explicit conversion of a BigInteger object to an unsigned 64-bit integer value. | |

Explicit(BigInteger to Byte) | Defines an explicit conversion of a BigInteger object to an unsigned byte value. | |

Explicit(Decimal to BigInteger) | Defines an explicit conversion of a Decimal object to a BigInteger value. | |

Explicit(Double to BigInteger) | Defines an explicit conversion of a Double value to a BigInteger value. | |

Explicit(Single to BigInteger) | Defines an explicit conversion of a Single object to a BigInteger value. | |

GreaterThan(BigInteger, Int64) | Returns a value that indicates whether a BigInteger is greater than a 64-bit signed integer value. | |

GreaterThan(BigInteger, BigInteger) | Returns a value that indicates whether a BigInteger value is greater than another BigInteger value. | |

GreaterThan(BigInteger, UInt64) | Returns a value that indicates whether a BigInteger value is greater than a 64-bit unsigned integer. | |

GreaterThan(Int64, BigInteger) | Returns a value that indicates whether a 64-bit signed integer is greater than a BigInteger value. | |

GreaterThan(UInt64, BigInteger) | Returns a value that indicates whether a BigInteger value is greater than a 64-bit unsigned integer. | |

GreaterThanOrEqual(BigInteger, Int64) | Returns a value that indicates whether a BigInteger value is greater than or equal to a 64-bit signed integer value. | |

GreaterThanOrEqual(BigInteger, BigInteger) | Returns a value that indicates whether a BigInteger value is greater than or equal to another BigInteger value. | |

GreaterThanOrEqual(BigInteger, UInt64) | Returns a value that indicates whether a BigInteger value is greater than or equal to a 64-bit unsigned integer value. | |

GreaterThanOrEqual(Int64, BigInteger) | Returns a value that indicates whether a 64-bit signed integer is greater than or equal to a BigInteger value. | |

GreaterThanOrEqual(UInt64, BigInteger) | Returns a value that indicates whether a 64-bit unsigned integer is greater than or equal to a BigInteger value. | |

Implicit(Byte to BigInteger) | Defines an implicit conversion of an unsigned byte to a BigInteger value. | |

Implicit(Int16 to BigInteger) | Defines an implicit conversion of a signed 16-bit integer to a BigInteger value. | |

Implicit(Int32 to BigInteger) | Defines an implicit conversion of a signed 32-bit integer to a BigInteger value. | |

Implicit(Int64 to BigInteger) | Defines an implicit conversion of a signed 64-bit integer to a BigInteger value. | |

Implicit(SByte to BigInteger) | Defines an implicit conversion of an 8-bit signed integer to a BigInteger value. | |

Implicit(UInt16 to BigInteger) | Defines an implicit conversion of a 16-bit unsigned integer to a BigInteger value. | |

Implicit(UInt32 to BigInteger) | Defines an implicit conversion of a 32-bit unsigned integer to a BigInteger value. | |

Implicit(UInt64 to BigInteger) | Defines an implicit conversion of a 64-bit unsigned integer to a BigInteger value. | |

Increment | Increments a BigInteger value by 1. | |

Inequality(BigInteger, Int64) | Returns a value that indicates whether a BigInteger value and a 64-bit signed integer are not equal. | |

Inequality(BigInteger, BigInteger) | Returns a value that indicates whether two BigInteger objects have different values. | |

Inequality(BigInteger, UInt64) | Returns a value that indicates whether a BigInteger value and a 64-bit unsigned integer are not equal. | |

Inequality(Int64, BigInteger) | Returns a value that indicates whether a 64-bit signed integer and a BigInteger value are not equal. | |

Inequality(UInt64, BigInteger) | Returns a value that indicates whether a 64-bit unsigned integer and a BigInteger value are not equal. | |

LeftShift | Shifts a BigInteger value a specified number of bits to the left. | |

LessThan(BigInteger, Int64) | Returns a value that indicates whether a BigInteger value is less than a 64-bit signed integer. | |

LessThan(BigInteger, BigInteger) | Returns a value that indicates whether a BigInteger value is less than another BigInteger value. | |

LessThan(BigInteger, UInt64) | Returns a value that indicates whether a BigInteger value is less than a 64-bit unsigned integer. | |

LessThan(Int64, BigInteger) | Returns a value that indicates whether a 64-bit signed integer is less than a BigInteger value. | |

LessThan(UInt64, BigInteger) | Returns a value that indicates whether a 64-bit unsigned integer is less than a BigInteger value. | |

LessThanOrEqual(BigInteger, Int64) | Returns a value that indicates whether a BigInteger value is less than or equal to a 64-bit signed integer. | |

LessThanOrEqual(BigInteger, BigInteger) | Returns a value that indicates whether a BigInteger value is less than or equal to another BigInteger value. | |

LessThanOrEqual(BigInteger, UInt64) | Returns a value that indicates whether a BigInteger value is less than or equal to a 64-bit unsigned integer. | |

LessThanOrEqual(Int64, BigInteger) | Returns a value that indicates whether a 64-bit signed integer is less than or equal to a BigInteger value. | |

LessThanOrEqual(UInt64, BigInteger) | Returns a value that indicates whether a 64-bit unsigned integer is less than or equal to a BigInteger value. | |

Modulus | Returns the remainder that results from division with two specified BigInteger values. | |

Multiply | Multiplies two specified BigInteger values. | |

OnesComplement | Returns the bitwise one's complement of a BigInteger value. | |

RightShift | Shifts a BigInteger value a specified number of bits to the right. | |

Subtraction | Subtracts a BigInteger value from another BigInteger value. | |

UnaryNegation | Negates a specified BigInteger value. | |

UnaryPlus | Returns the value of the BigInteger operand. (The sign of the operand is unchanged.) |

Name | Description | |
---|---|---|

IComparable.CompareTo | Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. |

The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds. The members of the BigInteger type closely parallel those of other integral types (the Byte, Int16, Int32, Int64, SByte, UInt16, UInt32, and UInt64 types). This type differs from the other integral types in the .NET Framework, which have a range indicated by their MinValue and MaxValue properties.

Note |
---|

Because the BigInteger type is immutable (see Mutability and the BigInteger Structure) and because it has no upper or lower bounds, an OutOfMemoryException can be thrown for any operation that causes a BigInteger value to grow too large. |

### Instantiating a BigInteger Object

You can instantiate a BigInteger object in several ways:

You can use the new keyword and provide any integral or floating-point value as a parameter to the BigInteger constructor. (Floating-point values are truncated before they are assigned to the BigInteger.) The following example illustrates how to use the new keyword to instantiate BigInteger values.

You can declare a BigInteger variable and assign it a value just as you would any numeric type, as long as that value is an integral type. The following example uses assignment to create a BigInteger value from an Int64.

You can assign a decimal or floating-point value to a BigInteger object if you cast the value or convert it first. The following example explicitly casts (in C#) or converts (in Visual Basic) a Double and a Decimal value to a BigInteger.

These methods enable you to instantiate a BigInteger object whose value is in the range of one of the existing numeric types only. You can instantiate a BigInteger object whose value can exceed the range of the existing numeric types in one of three ways:

You can use the new keyword and provide a byte array of any size to the BigInteger.BigInteger constructor. For example:

You can call the Parse or TryParse methods to convert the string representation of a number to a BigInteger. For example:

You can call a static (Shared in Visual Basic) BigInteger method that performs some operation on a numeric expression and returns a calculated BigInteger result. The following example does this by cubing UInt64.MaxValue and assigning the result to a BigInteger.

The uninitialized value of a BigInteger is Zero.

### Performing Operations on BigInteger Values

You can use a BigInteger instance as you would use any other integral type. BigInteger overloads the standard numeric operators to enable you to perform basic mathematical operations such as addition, subtraction, division, multiplication, subtraction, negation, and unary negation. You can also use the standard numeric operators to compare two BigInteger values with each other. Like the other integral types, BigInteger also supports the bitwise And, Or, XOr, left shift, and right shift operators. For languages that do not support custom operators, the BigInteger structure also provides equivalent methods for performing mathematical operations. These include Add, Divide, Multiply, Negate, Subtract, and several others.

Many members of the BigInteger structure correspond directly to members of the other integral types. In addition, BigInteger adds members such as the following:

Sign, which returns a value that indicates the sign of a BigInteger value.

Abs, which returns the absolute value of a BigInteger value.

DivRem, which returns both the quotient and remainder of a division operation.

GreatestCommonDivisor, which returns the greatest common divisor of two BigInteger values.

Many of these additional members correspond to the members of the Math class, which provides the functionality to work with the primitive numeric types.

### Mutability and the BigInteger Structure

The following example instantiates a BigInteger object and then increments its value by one.

Although this example appears to modify the value of the existing object, this is not the case. BigInteger objects are immutable, which means that internally, the common language runtime actually creates a new BigInteger object and assigns it a value one greater than its previous value. This new object is then returned to the caller.

Note |
---|

The other numeric types in the .NET Framework are also immutable. However, because the BigInteger type has no upper or lower bounds, its values can grow extremely large and have a measurable impact on performance. |

Although this process is transparent to the caller, it does incur a performance penalty. In some cases, especially when repeated operations are performed in a loop on very large BigInteger values, that performance penalty can be significant. For example, in the following example, an operation is performed repetitively up to a million times, and a BigInteger value is incremented by one every time the operation succeeds.

In such a case, you can improve performance by performing all intermediate assignments to an Int32 variable. The final value of the variable can then be assigned to the BigInteger object when the loop exits. The following example provides an illustration.

### Working with Byte Arrays and Hexadecimal Strings

If you convert BigInteger values to byte arrays, or if you convert byte arrays to BigInteger values, you must consider the order of bytes. The BigInteger structure expects the individual bytes in a byte array to appear in little-endian order (that is, the lower-order bytes of the value precede the higher-order bytes). You can round-trip a BigInteger value by calling the ToByteArray method and then passing the resulting byte array to the BigInteger(Byte[]) constructor, as the following example shows.

To instantiate a BigInteger value from a byte array that represents a value of some other integral type, you can pass the integral value to the BitConverter.GetBytes method, and then pass the resulting byte array to the BigInteger(Byte[]) constructor. The following example instantiates a BigInteger value from a byte array that represents an Int16 value.

The BigInteger structure assumes that negative values are stored by using two's complement representation. Because the BigInteger structure represents a numeric value with no fixed length, the BigInteger(Byte[]) constructor always interprets the most significant bit of the last byte in the array as a sign bit. To prevent the BigInteger(Byte[]) constructor from confusing the two's complement representation of a negative value with the sign and magnitude representation of a positive value, positive values in which the most significant bit of the last byte in the byte array would ordinarily be set should include an additional byte whose value is 0. For example, 0xC0 0xBD 0xF0 0xFF is the little-endian hexadecimal representation of either -1,000,000 or 4,293,967,296. Because the most significant bit of the last byte in this array is on, the value of the byte array would be interpreted by the BigInteger(Byte[]) constructor as -1,000,000. To instantiate a BigInteger whose value is positive, a byte array whose elements are 0xC0 0xBD 0xF0 0xFF 0x00 must be passed to the constructor. The following example illustrates this.

Byte arrays created by the ToByteArray method from positive values include this extra zero-value byte. Therefore, the BigInteger structure can successfully round-trip values by assigning them to, and then restoring them from, byte arrays, as the following example shows.

However, you may need to add this additional zero-value byte to byte arrays that are created dynamically by the developer or that are returned by methods that convert unsigned integers to byte arrays (such as BitConverter.GetBytes(UInt16), BitConverter.GetBytes(UInt32), and BitConverter.GetBytes(UInt64)).

When parsing a hexadecimal string, the BigInteger.Parse(String, NumberStyles) and BigInteger.Parse(String, NumberStyles, IFormatProvider) methods assume that if the most significant bit of the first byte in the string is set, or if the first hexadecimal digit of the string represents the lower four bits of a byte value, the value is represented by using two's complement representation. For example, both "FF01" and "F01" represent the decimal value -255. To differentiate positive from negative values, positive values should include a leading zero. The relevant overloads of the ToString method, when they are passed the "X" format string, add a leading zero to the returned hexadecimal string for positive values. This makes it possible to round-trip BigInteger values by using the ToString and Parse methods, as the following example shows.

However, the hexadecimal strings created by calling the ToString methods of the other integral types or the overloads of the ToString method that include a toBase parameter do not indicate the sign of the value or the source data type from which the hexadecimal string was derived. Successfully instantiating a BigInteger value from such a string requires some additional logic. The following example provides one possible implementation.

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.