本文為機器翻譯文章。如需檢視英文版，請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上，即可在快顯視窗顯示英文原文。 譯文 原文

# BigInteger 結構

.NET Framework (current version)

## 語法

```[SerializableAttribute]
public struct BigInteger : IFormattable, IComparable, IComparable<BigInteger>,
IEquatable<BigInteger>
```

## 建構函式

BigInteger(Byte[])

BigInteger(Decimal)

BigInteger(Double)

BigInteger(Int32)

BigInteger(Int64)

BigInteger(Single)

BigInteger(UInt32)

BigInteger(UInt64)

IsEven

IsOne

IsPowerOfTwo

IsZero

MinusOne

One

Sign

Zero

## 方法

Abs(BigInteger)

Compare(BigInteger, BigInteger)

CompareTo(BigInteger)

CompareTo(Int64)

CompareTo(Object)

CompareTo(UInt64)

Divide(BigInteger, BigInteger)

DivRem(BigInteger, BigInteger, BigInteger)

Equals(BigInteger)

Equals(Int64)

Equals(Object)

Equals(UInt64)

GetHashCode()

GetType()

GreatestCommonDivisor(BigInteger, BigInteger)

Log(BigInteger)

Log(BigInteger, Double)

Log10(BigInteger)

Max(BigInteger, BigInteger)

Min(BigInteger, BigInteger)

ModPow(BigInteger, BigInteger, BigInteger)

Multiply(BigInteger, BigInteger)

Negate(BigInteger)

Parse(String)

Parse(String, IFormatProvider)

Parse(String, NumberStyles)

Parse(String, NumberStyles, IFormatProvider)

Pow(BigInteger, Int32)

Remainder(BigInteger, BigInteger)

Subtract(BigInteger, BigInteger)

ToByteArray()

ToString()

ToString(IFormatProvider)

ToString(String)

ToString(String, IFormatProvider)

TryParse(String, BigInteger)

TryParse(String, NumberStyles, IFormatProvider, BigInteger)

## 運算子

BitwiseAnd(BigInteger, BigInteger)

BitwiseOr(BigInteger, BigInteger)

Decrement(BigInteger)

Division(BigInteger, BigInteger)

Equality(BigInteger, BigInteger)

Equality(BigInteger, Int64)

Equality(BigInteger, UInt64)

Equality(Int64, BigInteger)

Equality(UInt64, BigInteger)

ExclusiveOr(BigInteger, BigInteger)

Explicit(BigInteger to Byte)

Explicit(BigInteger to Decimal)

Explicit(BigInteger to Double)

Explicit(BigInteger to Int16)

Explicit(BigInteger to Int32)

Explicit(BigInteger to Int64)

Explicit(BigInteger to SByte)

Explicit(BigInteger to Single)

Explicit(BigInteger to UInt16)

Explicit(BigInteger to UInt32)

Explicit(BigInteger to UInt64)

Explicit(Decimal to BigInteger)

Explicit(Double to BigInteger)

Explicit(Single to BigInteger)

GreaterThan(BigInteger, BigInteger)

GreaterThan(BigInteger, Int64)

GreaterThan(BigInteger, UInt64)

GreaterThan(Int64, BigInteger)

GreaterThan(UInt64, BigInteger)

GreaterThanOrEqual(BigInteger, BigInteger)

GreaterThanOrEqual(BigInteger, Int64)

GreaterThanOrEqual(BigInteger, UInt64)

GreaterThanOrEqual(Int64, BigInteger)

GreaterThanOrEqual(UInt64, BigInteger)

Implicit(Byte to BigInteger)

Implicit(Int16 to BigInteger)

Implicit(Int32 to BigInteger)

Implicit(Int64 to BigInteger)

Implicit(SByte to BigInteger)

Implicit(UInt16 to BigInteger)

Implicit(UInt32 to BigInteger)

Implicit(UInt64 to BigInteger)

Increment(BigInteger)

Inequality(BigInteger, BigInteger)

Inequality(BigInteger, Int64)

Inequality(BigInteger, UInt64)

Inequality(Int64, BigInteger)

Inequality(UInt64, BigInteger)

LeftShift(BigInteger, Int32)

LessThan(BigInteger, BigInteger)

LessThan(BigInteger, Int64)

LessThan(BigInteger, UInt64)

LessThan(Int64, BigInteger)

LessThan(UInt64, BigInteger)

LessThanOrEqual(BigInteger, BigInteger)

LessThanOrEqual(BigInteger, Int64)

LessThanOrEqual(BigInteger, UInt64)

LessThanOrEqual(Int64, BigInteger)

LessThanOrEqual(UInt64, BigInteger)

Modulus(BigInteger, BigInteger)

Multiply(BigInteger, BigInteger)

OnesComplement(BigInteger)

RightShift(BigInteger, Int32)

BigInteger 值向右移動指定的位元數。

Subtraction(BigInteger, BigInteger)

UnaryNegation(BigInteger)

UnaryPlus(BigInteger)

## 註解

BigInteger 型別是不可變的類型表示任意大整數的值在理論上沒有上限或下限。 成員 BigInteger 類型極為相似的其他整數類資料類型 ( Byte, ，Int16, ，Int32, ，Int64, ，SByte, ，UInt16, ，UInt32, ，和 UInt64 類型)。 此型別不同於其他整數類資料類型中 .NET Framework, ，具有所指定範圍內其 MinValueMaxValue 屬性。

### BigInteger 物件具現化

• 您可以使用 new 關鍵字，並做為參數提供的任何整數或浮點值 BigInteger 建構函式。 (浮點數的值會被截斷，指派給之前 BigInteger。) 下列範例說明如何使用 new 關鍵字來具現化 BigInteger 值。

```BigInteger bigIntFromDouble = new BigInteger(179032.6541);
Console.WriteLine(bigIntFromDouble);
BigInteger bigIntFromInt64 = new BigInteger(934157136952);
Console.WriteLine(bigIntFromInt64);
// The example displays the following output:
//   179032
//   934157136952
```
• 您可以宣告 BigInteger 變數並指派值，就像您一樣任何數字類型，只要這個值是整數類型。 下列範例會使用指派建立 BigIntegerInt64

```long longValue = 6315489358112;
BigInteger assignedFromLong = longValue;
Console.WriteLine(assignedFromLong);
// The example displays the following output:
//   6315489358112
```
• 您可以指定十進位或浮點值 BigInteger 物件，您將值轉型或將它轉換到第一次。 下列範例明確轉換 （C# 中），或將轉換 （在 Visual Basic) DoubleDecimalBigInteger

```BigInteger assignedFromDouble = (BigInteger) 179032.6541;
Console.WriteLine(assignedFromDouble);
BigInteger assignedFromDecimal = (BigInteger) 64312.65m;
Console.WriteLine(assignedFromDecimal);
// The example displays the following output:
//   179032
//   64312
```

• 您可以使用 new 關鍵字，並提供任何大小的位元組陣列 BigInteger.BigInteger 建構函式。 例如：

```byte[] byteArray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
BigInteger newBigInt = new BigInteger(byteArray);
Console.WriteLine("The value of newBigInt is {0} (or 0x{0:x}).", newBigInt);
// The example displays the following output:
//   The value of newBigInt is 4759477275222530853130 (or 0x102030405060708090a).
```
• 您可以呼叫 ParseTryParse 方法的數字的字串表示轉換成 BigInteger 例如：

```string positiveString = "91389681247993671255432112000000";
string negativeString = "-90315837410896312071002088037140000";
BigInteger posBigInt = 0;
BigInteger negBigInt = 0;

try {
posBigInt = BigInteger.Parse(positiveString);
Console.WriteLine(posBigInt);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
positiveString);
}

if (BigInteger.TryParse(negativeString, out negBigInt))
Console.WriteLine(negBigInt);
else
Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
negativeString);

// The example displays the following output:
//   9.1389681247993671255432112E+31
//   -9.0315837410896312071002088037E+34
```
• 您可以呼叫 static (Shared 在 Visual Basic 中) BigInteger 方法來執行數值運算式上的某些作業並傳回導出 BigInteger 結果。 下列範例的做法是 cube UInt64.MaxValue 並指派至結果 BigInteger

```BigInteger number = BigInteger.Pow(UInt64.MaxValue, 3);
Console.WriteLine(number);
// The example displays the following output:
//    6277101735386680762814942322444851025767571854389858533375
```

### BigInteger 值上執行的作業

• Sign, 它會傳回值，指出正負號的 BigInteger 值。

• Abs, 它會傳回數值的絕對值 BigInteger 值。

• DivRem, 它會傳回商數及除法運算的餘數。

• GreatestCommonDivisor, 它會傳回兩個最大公因數 BigInteger 值。

### 可變動性和 BigInteger 結構

```BigInteger number = BigInteger.Multiply(Int64.MaxValue, 3);
number++;
Console.WriteLine(number);
```

.NET Framework 中的其他數值類型也是固定不變。 不過，因為 BigInteger 類型都沒有上限或下限範圍，其值可以變得非常大，並對效能有明顯的影響。

```BigInteger number = Int64.MaxValue ^ 5;
int repetitions = 1000000;
// Perform some repetitive operation 1 million times.
for (int ctr = 0; ctr <= repetitions; ctr++)
{
// Perform some operation. If it fails, exit the loop.
if (! SomeOperationSucceeds()) break;
// The following code executes if the operation succeeds.
number++;
}
```

```BigInteger number = Int64.MaxValue ^ 5;
int repetitions = 1000000;
int actualRepetitions = 0;
// Perform some repetitive operation 1 million times.
for (int ctr = 0; ctr <= repetitions; ctr++)
{
// Perform some operation. If it fails, exit the loop.
if (! SomeOperationSucceeds()) break;
// The following code executes if the operation succeeds.
actualRepetitions++;
}
number += actualRepetitions;
```

### 使用位元組陣列的十六進位字串

```BigInteger number = BigInteger.Pow(Int64.MaxValue, 2);
Console.WriteLine(number);

// Write the BigInteger value to a byte array.
byte[] bytes = number.ToByteArray();

// Display the byte array.
foreach (byte byteValue in bytes)
Console.Write("0x{0:X2} ", byteValue);
Console.WriteLine();

// Restore the BigInteger value from a Byte array.
BigInteger newNumber = new BigInteger(bytes);
Console.WriteLine(newNumber);
// The example displays the following output:
//    8.5070591730234615847396907784E+37
//    0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x3F
//
//    8.5070591730234615847396907784E+37
```

```short originalValue = 30000;
Console.WriteLine(originalValue);

// Convert the Int16 value to a byte array.
byte[] bytes = BitConverter.GetBytes(originalValue);

// Display the byte array.
foreach (byte byteValue in bytes)
Console.Write("0x{0} ", byteValue.ToString("X2"));
Console.WriteLine();

// Pass byte array to the BigInteger constructor.
BigInteger number = new BigInteger(bytes);
Console.WriteLine(number);
// The example displays the following output:
//       30000
//       0x30 0x75
//       30000
```

BigInteger 結構假設負數的值會儲存使用二的補數表示。 因為 BigInteger 結構代表數值，但是沒有固定的長度， BigInteger(Byte[]) 建構函式一律會解譯為正負號位元陣列中的最後一個位元組的最大顯著性位元。 若要避免 BigInteger(Byte[]) 建構函式從混淆二的補數表示負數值的正數值，正以位元組陣列中的最後一個位元組的最大顯著性位元會通常設定的值應該包含額外的位元組值為 0 的符號與量值表示法。 例如，0xC0 0xBD 0xF0 0xFF 是-1,000,000 或 4,293,967,296 由小到大十六進位表示法。 由於此陣列中的最後一個位元組的最大顯著性位元為開啟，位元組陣列的值會被解譯由 BigInteger(Byte[]) -1,000,000 建構函式。 若要具現化 BigInteger 其值為正數，位元組陣列，其元素為 0xC0 0xBD 0xF0 0xFF 0x00 必須傳遞給建構函式。 下列範例將說明這點。

```int negativeNumber = -1000000;
uint positiveNumber = 4293967296;

byte[] negativeBytes = BitConverter.GetBytes(negativeNumber);
BigInteger negativeBigInt = new BigInteger(negativeBytes);
Console.WriteLine(negativeBigInt.ToString("N0"));

byte[] tempPosBytes = BitConverter.GetBytes(positiveNumber);
byte[] positiveBytes = new byte[tempPosBytes.Length + 1];
Array.Copy(tempPosBytes, positiveBytes, tempPosBytes.Length);
BigInteger positiveBigInt = new BigInteger(positiveBytes);
Console.WriteLine(positiveBigInt.ToString("N0"));
// The example displays the following output:
//    -1,000,000
//    4,293,967,296
```

```BigInteger positiveValue = 15777216;
BigInteger negativeValue  = -1000000;

Console.WriteLine("Positive value: " + positiveValue.ToString("N0"));
byte[] bytes = positiveValue.ToByteArray();

foreach (byte byteValue in bytes)
Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
positiveValue = new BigInteger(bytes);
Console.WriteLine("Restored positive value: " + positiveValue.ToString("N0"));

Console.WriteLine();

Console.WriteLine("Negative value: " + negativeValue.ToString("N0"));
bytes = negativeValue.ToByteArray();
foreach (byte byteValue in bytes)
Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
negativeValue = new BigInteger(bytes);
Console.WriteLine("Restored negative value: " + negativeValue.ToString("N0"));
// The example displays the following output:
//       Positive value: 15,777,216
//       C0 BD F0 00
//       Restored positive value: 15,777,216
//
//       Negative value: -1,000,000
//       C0 BD F0
//       Restored negative value: -1,000,000
```

```BigInteger negativeNumber = -1000000;
BigInteger positiveNumber  = 15777216;

string negativeHex = negativeNumber.ToString("X");
string positiveHex = positiveNumber.ToString("X");

BigInteger negativeNumber2, positiveNumber2;
negativeNumber2 = BigInteger.Parse(negativeHex,
NumberStyles.HexNumber);
positiveNumber2 = BigInteger.Parse(positiveHex,
NumberStyles.HexNumber);

Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
negativeNumber, negativeHex, negativeNumber2);
Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
positiveNumber, positiveHex, positiveNumber2);
// The example displays the following output:
//       Converted -1,000,000 to F0BDC0 back to -1,000,000.
//       Converted 15,777,216 to 0F0BDC0 back to 15,777,216.
```

```using System;
using System.Globalization;
using System.Numerics;

public struct HexValue
{
public int Sign;
public string Value;
}

public class Example
{
public static void Main()
{
uint positiveNumber = 4039543321;
int negativeNumber = -255423975;

// Convert the numbers to hex strings.
HexValue hexValue1, hexValue2;
hexValue1.Value = positiveNumber.ToString("X");
hexValue1.Sign = Math.Sign(positiveNumber);

hexValue2.Value = Convert.ToString(negativeNumber, 16);
hexValue2.Sign = Math.Sign(negativeNumber);

// Round-trip the hexadecimal values to BigInteger values.
string hexString;
BigInteger positiveBigInt, negativeBigInt;

hexString = (hexValue1.Sign == 1 ? "0" : "") + hexValue1.Value;
positiveBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber);
Console.WriteLine("Converted {0} to {1} and back to {2}.",
positiveNumber, hexValue1.Value, positiveBigInt);

hexString = (hexValue2.Sign == 1 ? "0" : "") + hexValue2.Value;
negativeBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber);
Console.WriteLine("Converted {0} to {1} and back to {2}.",
negativeNumber, hexValue2.Value, negativeBigInt);
}
}
// The example displays the following output:
//       Converted 4039543321 to F0C68A19 and back to 4039543321.
//       Converted -255423975 to f0c68a19 and back to -255423975.
```

.NET Framework

Silverlight

Windows Phone