BigInteger 構造体
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
任意の大きさを持つ符号付き整数を表します。
public value class BigInteger : IComparable, IComparable<System::Numerics::BigInteger>, IEquatable<System::Numerics::BigInteger>, IFormattable
public value class BigInteger : IComparable, IComparable<System::Numerics::BigInteger>, IEquatable<System::Numerics::BigInteger>, ISpanFormattable
public value class BigInteger : IComparable<System::Numerics::BigInteger>, IEquatable<System::Numerics::BigInteger>, IParsable<System::Numerics::BigInteger>, ISpanParsable<System::Numerics::BigInteger>, System::Numerics::IAdditionOperators<System::Numerics::BigInteger, System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IAdditiveIdentity<System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IBinaryInteger<System::Numerics::BigInteger>, System::Numerics::IBinaryNumber<System::Numerics::BigInteger>, System::Numerics::IBitwiseOperators<System::Numerics::BigInteger, System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IComparisonOperators<System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IDecrementOperators<System::Numerics::BigInteger>, System::Numerics::IDivisionOperators<System::Numerics::BigInteger, System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IEqualityOperators<System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IIncrementOperators<System::Numerics::BigInteger>, System::Numerics::IModulusOperators<System::Numerics::BigInteger, System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IMultiplicativeIdentity<System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IMultiplyOperators<System::Numerics::BigInteger, System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::INumber<System::Numerics::BigInteger>, System::Numerics::INumberBase<System::Numerics::BigInteger>, System::Numerics::IShiftOperators<System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::ISignedNumber<System::Numerics::BigInteger>, System::Numerics::ISubtractionOperators<System::Numerics::BigInteger, System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IUnaryNegationOperators<System::Numerics::BigInteger, System::Numerics::BigInteger>, System::Numerics::IUnaryPlusOperators<System::Numerics::BigInteger, System::Numerics::BigInteger>
public struct BigInteger : IComparable, IComparable<System.Numerics.BigInteger>, IEquatable<System.Numerics.BigInteger>, IFormattable
public readonly struct BigInteger : IComparable, IComparable<System.Numerics.BigInteger>, IEquatable<System.Numerics.BigInteger>, IFormattable
public readonly struct BigInteger : IComparable, IComparable<System.Numerics.BigInteger>, IEquatable<System.Numerics.BigInteger>, ISpanFormattable
public readonly struct BigInteger : IComparable<System.Numerics.BigInteger>, IEquatable<System.Numerics.BigInteger>, IParsable<System.Numerics.BigInteger>, ISpanParsable<System.Numerics.BigInteger>, System.Numerics.IAdditionOperators<System.Numerics.BigInteger,System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IAdditiveIdentity<System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IBinaryInteger<System.Numerics.BigInteger>, System.Numerics.IBinaryNumber<System.Numerics.BigInteger>, System.Numerics.IBitwiseOperators<System.Numerics.BigInteger,System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IComparisonOperators<System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IDecrementOperators<System.Numerics.BigInteger>, System.Numerics.IDivisionOperators<System.Numerics.BigInteger,System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IEqualityOperators<System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IIncrementOperators<System.Numerics.BigInteger>, System.Numerics.IModulusOperators<System.Numerics.BigInteger,System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IMultiplicativeIdentity<System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IMultiplyOperators<System.Numerics.BigInteger,System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.INumber<System.Numerics.BigInteger>, System.Numerics.INumberBase<System.Numerics.BigInteger>, System.Numerics.IShiftOperators<System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.ISignedNumber<System.Numerics.BigInteger>, System.Numerics.ISubtractionOperators<System.Numerics.BigInteger,System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IUnaryNegationOperators<System.Numerics.BigInteger,System.Numerics.BigInteger>, System.Numerics.IUnaryPlusOperators<System.Numerics.BigInteger,System.Numerics.BigInteger>
[System.Serializable]
public struct BigInteger : IComparable, IComparable<System.Numerics.BigInteger>, IEquatable<System.Numerics.BigInteger>, IFormattable
type BigInteger = struct
interface IFormattable
type BigInteger = struct
interface ISpanFormattable
interface IFormattable
type BigInteger = struct
interface IFormattable
interface IParsable<BigInteger>
interface ISpanFormattable
interface ISpanParsable<BigInteger>
interface IAdditionOperators<BigInteger, BigInteger, BigInteger>
interface IAdditiveIdentity<BigInteger, BigInteger>
interface IBinaryInteger<BigInteger>
interface IBinaryNumber<BigInteger>
interface IBitwiseOperators<BigInteger, BigInteger, BigInteger>
interface IComparisonOperators<BigInteger, BigInteger>
interface IEqualityOperators<BigInteger, BigInteger>
interface IDecrementOperators<BigInteger>
interface IDivisionOperators<BigInteger, BigInteger, BigInteger>
interface IIncrementOperators<BigInteger>
interface IModulusOperators<BigInteger, BigInteger, BigInteger>
interface IMultiplicativeIdentity<BigInteger, BigInteger>
interface IMultiplyOperators<BigInteger, BigInteger, BigInteger>
interface INumber<BigInteger>
interface INumberBase<BigInteger>
interface ISubtractionOperators<BigInteger, BigInteger, BigInteger>
interface IUnaryNegationOperators<BigInteger, BigInteger>
interface IUnaryPlusOperators<BigInteger, BigInteger>
interface IShiftOperators<BigInteger, BigInteger>
interface ISignedNumber<BigInteger>
[<System.Serializable>]
type BigInteger = struct
interface IFormattable
Public Structure BigInteger
Implements IComparable, IComparable(Of BigInteger), IEquatable(Of BigInteger), IFormattable
Public Structure BigInteger
Implements IComparable, IComparable(Of BigInteger), IEquatable(Of BigInteger), ISpanFormattable
Public Structure BigInteger
Implements IAdditionOperators(Of BigInteger, BigInteger, BigInteger), IAdditiveIdentity(Of BigInteger, BigInteger), IBinaryInteger(Of BigInteger), IBinaryNumber(Of BigInteger), IBitwiseOperators(Of BigInteger, BigInteger, BigInteger), IComparable(Of BigInteger), IComparisonOperators(Of BigInteger, BigInteger), IDecrementOperators(Of BigInteger), IDivisionOperators(Of BigInteger, BigInteger, BigInteger), IEqualityOperators(Of BigInteger, BigInteger), IEquatable(Of BigInteger), IIncrementOperators(Of BigInteger), IModulusOperators(Of BigInteger, BigInteger, BigInteger), IMultiplicativeIdentity(Of BigInteger, BigInteger), IMultiplyOperators(Of BigInteger, BigInteger, BigInteger), INumber(Of BigInteger), INumberBase(Of BigInteger), IParsable(Of BigInteger), IShiftOperators(Of BigInteger, BigInteger), ISignedNumber(Of BigInteger), ISpanParsable(Of BigInteger), ISubtractionOperators(Of BigInteger, BigInteger, BigInteger), IUnaryNegationOperators(Of BigInteger, BigInteger), IUnaryPlusOperators(Of BigInteger, BigInteger)
- 継承
- 属性
- 実装
-
IComparable IComparable<BigInteger> IEquatable<BigInteger> IFormattable ISpanFormattable IComparable<TOther> IComparable<TSelf> IEquatable<TOther> IEquatable<TSelf> IParsable<BigInteger> IParsable<TSelf> ISpanParsable<BigInteger> ISpanParsable<TSelf> IAdditionOperators<BigInteger,BigInteger,BigInteger> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<BigInteger,BigInteger> IAdditiveIdentity<TSelf,TSelf> IBinaryInteger<BigInteger> IBinaryNumber<BigInteger> IBinaryNumber<TSelf> IBitwiseOperators<BigInteger,BigInteger,BigInteger> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<BigInteger,BigInteger> IComparisonOperators<TSelf,TSelf> IDecrementOperators<BigInteger> IDecrementOperators<TSelf> IDivisionOperators<BigInteger,BigInteger,BigInteger> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<BigInteger,BigInteger> IEqualityOperators<TSelf,TOther> IEqualityOperators<TSelf,TSelf> IIncrementOperators<BigInteger> IIncrementOperators<TSelf> IModulusOperators<BigInteger,BigInteger,BigInteger> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<BigInteger,BigInteger> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<BigInteger,BigInteger,BigInteger> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<BigInteger> INumber<TSelf> INumberBase<BigInteger> INumberBase<TSelf> IShiftOperators<BigInteger,BigInteger> IShiftOperators<TSelf,TSelf> ISignedNumber<BigInteger> ISubtractionOperators<BigInteger,BigInteger,BigInteger> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<BigInteger,BigInteger> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<BigInteger,BigInteger> IUnaryPlusOperators<TSelf,TSelf>
注釈
この BigInteger 型は、理論上の値に上限または下限がない任意の大きな整数を表す不変型です。 型のメンバーは、BigInteger他の整数型 (Byte, , Int16, , SByteInt64UInt16Int32UInt32型) のメンバーとUInt64密接に並行しています。 この型は、.NET Framework内の他の整数型とは異なり、その範囲はプロパティMinValue
によってMaxValue
示されます。
注意
BigInteger型は不変であるため (変更可能性と BigInteger 構造体を参照)、上限または下限がないため、OutOfMemoryException値がBigInteger大きくなりすぎる操作に対して an をスローできます。
BigInteger オブジェクトのインスタンス化
オブジェクトは、いくつかの方法でインスタンス化 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
Dim bigIntFromDouble As New BigInteger(179032.6541) Console.WriteLine(bigIntFromDouble) Dim bigIntFromInt64 As New BigInteger(934157136952) Console.WriteLine(bigIntFromInt64) ' The example displays the following output: ' 179032 ' 934157136952
変数を BigInteger 宣言し、その値が整数型である限り、任意の数値型と同様に値を割り当てることができます。 次の例では、代入を使用して BigInteger Int64.
long longValue = 6315489358112; BigInteger assignedFromLong = longValue; Console.WriteLine(assignedFromLong); // The example displays the following output: // 6315489358112
Dim longValue As Long = 6315489358112 Dim assignedFromLong As BigInteger = longValue Console.WriteLine(assignedFromLong) ' The example displays the following output: ' 6315489358112
値をキャストするか、最初に変換する場合は、 BigInteger オブジェクトに 10 進値または浮動小数点値を割り当てることができます。 次の例では、(C# では) 明示的にキャストするか、(Visual Basic では) a Double と値を Decimal BigInteger.
BigInteger assignedFromDouble = (BigInteger) 179032.6541; Console.WriteLine(assignedFromDouble); BigInteger assignedFromDecimal = (BigInteger) 64312.65m; Console.WriteLine(assignedFromDecimal); // The example displays the following output: // 179032 // 64312
Dim assignedFromDouble As BigInteger = CType(179032.6541, BigInteger) Console.WriteLine(assignedFromDouble) Dim assignedFromDecimal As BigInteger = CType(64312.65d, BigInteger) Console.WriteLine(assignedFromDecimal) ' The example displays the following output: ' 179032 ' 64312
これらのメソッドを使用すると、既存の数値型のいずれか 1 つの範囲内にある値を持つオブジェクトをインスタンス化 BigInteger できます。 次の 3 つの方法のいずれかで、既存の数値型の範囲を超える値を持つオブジェクトをインスタンス化 BigInteger できます。
このキーワードを
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).
Dim byteArray() As Byte = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0} Dim newBigInt As 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).
またはTryParseメソッドをParse呼び出して、数値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
Dim positiveString As String = "91389681247993671255432112000000" Dim negativeString As string = "-90315837410896312071002088037140000" Dim posBigInt As BigInteger = 0 Dim negBigInt As BigInteger = 0 Try posBigInt = BigInteger.Parse(positiveString) Console.WriteLine(posBigInt) Catch e As FormatException Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", _ positiveString) End Try If BigInteger.TryParse(negativeString, negBigInt) Then Console.WriteLine(negBigInt) Else Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", _ negativeString) End If ' The example displays the following output: ' 9.1389681247993671255432112E+31 ' -9.0315837410896312071002088037E+34
数式に対して何らかの操作を
static
実行し、計算結果BigIntegerを返す (Shared
Visual Basic では) BigInteger メソッドを呼び出すことができます。 次の例では、結果をキューに UInt64.MaxValue 入れ、結果 BigIntegerを代入することによってこれを行います。BigInteger number = BigInteger.Pow(UInt64.MaxValue, 3); Console.WriteLine(number); // The example displays the following output: // 6277101735386680762814942322444851025767571854389858533375
Dim number As BigInteger = BigInteger.Pow(UInt64.MaxValue, 3) Console.WriteLine(number) ' The example displays the following output: ' 6277101735386680762814942322444851025767571854389858533375
a BigInteger の初期化されていない値は Zero.
BigInteger 値に対する操作の実行
他の整数型を BigInteger 使用する場合と同様に、インスタンスを使用できます。 BigInteger では、加算、減算、除算、乗算、減算、否定、単項否定などの基本的な数学演算を実行できるように、標準の数値演算子がオーバーロードされます。 標準の数値演算子を使用して、2 つの BigInteger 値を相互に比較することもできます。 他の整数型と同様に、ビット演算子And
、 BigInteger Or``XOr
左シフト演算子、左シフト演算子、右シフト演算子もサポートします。 カスタム演算子をサポートしていない言語の場合、 BigInteger 構造体には数学演算を実行するための同等のメソッドも用意されています。 これには、、Divide、、Multiply、、Negate、Subtract、その他のいくつかが含Addまれます。
構造体の多くのメンバーは、 BigInteger 他の整数型のメンバーに直接対応します。 さらに、 BigInteger 次のようなメンバーを追加します。
Sign: 値の符号を示す値を BigInteger 返します。
Abs:値の絶対値 BigInteger を返します。
DivRemは、除算演算の商と剰余の両方を返します。
GreatestCommonDivisor2 つの BigInteger 値の最大の共通除数を返します。
これらの追加メンバーの多くは、プリミティブ数値型を Math 操作する機能を提供するクラスのメンバーに対応しています。
変更可能性と BigInteger 構造体
次の例では、オブジェクトを BigInteger インスタンス化し、その値を 1 ずつインクリメントします。
BigInteger number = BigInteger.Multiply(Int64.MaxValue, 3);
number++;
Console.WriteLine(number);
Dim number As BigInteger = BigInteger.Multiply(Int64.MaxValue, 3)
number += 1
Console.WriteLine(number)
この例では既存のオブジェクトの値を変更するように見えますが、これは当てはりません。 BigInteger オブジェクトは不変です。つまり、内部的には、共通言語ランタイムは実際には新しい BigInteger オブジェクトを作成し、以前の値より 1 大きい値を割り当てます。 この新しいオブジェクトは、呼び出し元に返されます。
注意
.NET の他の数値型も変更できません。 ただし、型には BigInteger 上限または下限がないため、値が非常に大きくなり、パフォーマンスに測定可能な影響を与える可能性があります。
このプロセスは呼び出し元には透過的ですが、パフォーマンスの低下が発生します。 場合によっては、特に非常に大きな BigInteger 値のループで繰り返し操作が実行される場合、パフォーマンスの低下が大きくなる可能性があります。 たとえば、次の例では、100 万回まで操作が繰り返し実行され BigInteger 、操作が成功するたびに値が 1 ずつインクリメントされます。
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++;
}
Dim number As BigInteger = Int64.MaxValue ^ 5
Dim repetitions As Integer = 1000000
' Perform some repetitive operation 1 million times.
For ctr As Integer = 0 To repetitions
' Perform some operation. If it fails, exit the loop.
If Not SomeOperationSucceeds() Then Exit For
' The following code executes if the operation succeeds.
number += 1
Next
このような場合は、変数に対するすべての中間代入を実行することで、パフォーマンスを Int32 向上させることができます。 その後、ループが終了したときに、変数の最終的な値を BigInteger オブジェクトに割り当てることができます。 具体的な例を次に示します。
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;
Dim number As BigInteger = Int64.MaxValue ^ 5
Dim repetitions As Integer = 1000000
Dim actualRepetitions As Integer = 0
' Perform some repetitive operation 1 million times.
For ctr As Integer = 0 To repetitions
' Perform some operation. If it fails, exit the loop.
If Not SomeOperationSucceeds() Then Exit For
' The following code executes if the operation succeeds.
actualRepetitions += 1
Next
number += actualRepetitions
バイト配列と 16 進文字列の操作
値をバイト配列に変換 BigInteger する場合、またはバイト配列を値に変換する BigInteger 場合は、バイトの順序を考慮する必要があります。 構造体は BigInteger 、バイト配列内の個々のバイトがリトル エンディアン順 (つまり、値の下位バイトが上位バイトの前) で表示されることを想定しています。 次の例に示すように、メソッドをBigIntegerToByteArray呼び出し、結果のバイト配列をコンストラクターにBigInteger(Byte[])渡すことで、値をラウンドトリップできます。
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
Dim number As BigInteger = BigInteger.Pow(Int64.MaxValue, 2)
Console.WriteLine(number)
' Write the BigInteger value to a byte array.
Dim bytes() As Byte = number.ToByteArray()
' Display the byte array.
For Each byteValue As Byte In bytes
Console.Write("0x{0:X2} ", byteValue)
Next
Console.WriteLine()
' Restore the BigInteger value from a Byte array.
Dim newNumber As BigInteger = 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
他の整数型の値を表すバイト配列から値をインスタンス化 BigInteger するには、整数値をメソッドに BitConverter.GetBytes 渡し、結果のバイト配列をコンストラクターに BigInteger(Byte[]) 渡します。 次の例では、値を BigInteger 表すバイト配列から値を Int16 インスタンス化します。
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
Dim originalValue As Short = 30000
Console.WriteLine(originalValue)
' Convert the Int16 value to a byte array.
Dim bytes() As Byte = BitConverter.GetBytes(originalValue)
' Display the byte array.
For Each byteValue As Byte In bytes
Console.Write("0x{0} ", byteValue.ToString("X2"))
Next
Console.WriteLine()
' Pass byte array to the BigInteger constructor.
Dim number As BigInteger = New BigInteger(bytes)
Console.WriteLine(number)
' The example displays the following output:
' 30000
' 0x30 0x75
' 30000
この構造体は BigInteger 、負の値が 2 の補数表現を使用して格納されることを前提としています。 構造体は BigInteger 固定長のない数値を表すので、 BigInteger(Byte[]) コンストラクターは常に配列内の最後のバイトの最上位ビットを符号ビットとして解釈します。 コンストラクターが BigInteger(Byte[]) 負の値の 2 つの補数表現を正の値の符号と大きさ表現と混同しないようにするには、通常、バイト配列の最後のバイトの最上位ビットが設定される正の値に、値が 0 の追加バイトを含める必要があります。 たとえば、0xC0 0xBD 0xF0 0xFFは、-1,000,000 または 4,293,967,296 のいずれかのリトル エンディアン 16 進表現です。 この配列の最後のバイトの最上位ビットがオンであるため、バイト配列の値はコンストラクターによって 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
Dim negativeNumber As Integer = -1000000
Dim positiveNumber As UInteger = 4293967296
Dim negativeBytes() As Byte = BitConverter.GetBytes(negativeNumber)
Dim negativeBigInt As New BigInteger(negativeBytes)
Console.WriteLine(negativeBigInt.ToString("N0"))
Dim tempPosBytes() As Byte = BitConverter.GetBytes(positiveNumber)
Dim positiveBytes(tempposBytes.Length) As Byte
Array.Copy(tempPosBytes, positiveBytes, tempPosBytes.Length)
Dim positiveBigInt As New BigInteger(positiveBytes)
Console.WriteLine(positiveBigInt.ToString("N0"))
' The example displays the following output:
' -1,000,000
' 4,293,967,296
正の値からメソッドによって ToByteArray 作成されたバイト配列には、この余分な 0 値バイトが含まれます。 したがって、次の BigInteger 例に示すように、構造体は値をバイト配列に割り当ててから復元することで、値を正常にラウンドトリップできます。
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
Dim positiveValue As BigInteger = 15777216
Dim negativeValue As BigInteger = -1000000
Console.WriteLine("Positive value: " + positiveValue.ToString("N0"))
Dim bytes() As Byte = positiveValue.ToByteArray()
For Each byteValue As Byte In bytes
Console.Write("{0:X2} ", byteValue)
Next
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()
For Each byteValue As Byte In bytes
Console.Write("{0:X2} ", byteValue)
Next
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
ただし、開発者によって動的に作成されるバイト配列、または符号なし整数をバイト配列に変換するメソッドによって返されるバイト配列に、この追加の 0 値バイトを追加する必要がある場合があります (例: BitConverter.GetBytes(UInt16), BitConverter.GetBytes(UInt32)、、 BitConverter.GetBytes(UInt64))。
16 進文字列を解析する場合、and BigInteger.Parse(String, NumberStyles, IFormatProvider) メソッドは、BigInteger.Parse(String, NumberStyles)文字列の最初のバイトの最上位ビットが設定されている場合、または文字列の最初の 16 進数がバイト値の下位 4 ビットを表す場合、値は 2 の補数表現を使用して表されることを前提としています。 たとえば、"FF01" と "F01" の両方が 10 進値 -255 を表します。 正の値と負の値を区別するには、正の値に先行ゼロを含める必要があります。 メソッドの ToString 関連するオーバーロードは、"X" 書式指定文字列を渡すときに、正の値を返す 16 進文字列に先頭に 0 を追加します。 これにより、次の例に示すように、and Parse メソッドをToString使用して値をラウンドトリップBigIntegerできます。
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.
Dim negativeNumber As BigInteger = -1000000
Dim positiveNumber As BigInteger = 15777216
Dim negativeHex As String = negativeNumber.ToString("X")
Dim positiveHex As string = positiveNumber.ToString("X")
Dim negativeNumber2, positiveNumber2 As BigInteger
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.
ただし、他の整数型のメソッドまたはパラメーターを含むtoBase
メソッドのToStringオーバーロードを呼び出ToString
すことによって作成された 16 進文字列は、16 進数文字列の派生元の値またはソース データ型の符号を示すものではありません。 このような文字列から値を BigInteger 正常にインスタンス化するには、追加のロジックが必要です。 次の例では、1 つの可能な実装を示します。
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.
Imports System.Globalization
Imports System.Numerics
Public Structure HexValue
Public Sign As Integer
Public Value As String
End Structure
Module Example
Public Sub Main()
Dim positiveNumber As UInteger = 4039543321
Dim negativeNumber As Integer = -255423975
' Convert the numbers to hex strings.
Dim hexValue1, hexValue2 As HexValue
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.
Dim hexString As String
Dim positiveBigInt, negativeBigInt As BigInteger
hexString = CStr(IIf(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 = CStr(IIf(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)
End Sub
End Module
' The example displays the following output:
' Converted 4039543321 to F0C68A19 and back to 4039543321.
' Converted -255423975 to f0c68a19 and back to -255423975.
コンストラクター
BigInteger(Byte[]) |
バイト配列の値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(Decimal) |
Decimal 値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(Double) |
倍精度浮動小数点値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(Int32) |
32 ビット符号付き整数値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(Int64) |
64 ビット符号付き整数値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean) |
バイトの読み取り専用スパンを使用して、BigInteger 構造体の新しいインスタンスを初期化します。任意で、符号付きエンコードとエンディアンのバイト順を示します。 |
BigInteger(Single) |
単精度浮動小数点値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(UInt32) |
32 ビット符号なし整数値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(UInt64) |
64 ビット符号なし整数値を使用して、BigInteger 構造体の新しいインスタンスを初期化します。 |
プロパティ
IsEven |
現在の BigInteger オブジェクトの値が偶数かどうかを示します。 |
IsOne |
現在の BigInteger オブジェクトの値が One かどうかを示します。 |
IsPowerOfTwo |
現在の BigInteger オブジェクトの値が 2 の累乗かどうかを示します。 |
IsZero |
現在の BigInteger オブジェクトの値が Zero かどうかを示します。 |
MinusOne |
負の 1 (-1) を表す値を取得します。 |
One |
正の 1 (1) を表す値を取得します。 |
Sign |
現在の BigInteger オブジェクトの符号 (負、正、または 0) を示す数値を取得します。 |
Zero |
0 (ゼロ) を表す値を取得します。 |
メソッド
演算子
明示的なインターフェイスの実装
IBinaryInteger<BigInteger>.GetByteCount() | |
IBinaryInteger<BigInteger>.GetShortestBitLength() | |
IBinaryInteger<BigInteger>.TryWriteBigEndian(Span<Byte>, Int32) | |
IBinaryInteger<BigInteger>.TryWriteLittleEndian(Span<Byte>, Int32) | |
IComparable.CompareTo(Object) |
このインスタンスと指定したオブジェクトを比較し、このインスタンスの値が指定したオブジェクトの値よりも小さいか、同じか、または大きいかを示す整数を返します。 |