Int128 Structure |
Namespace: Meta.Numerics.Extended
The Int128 type exposes the following members.
Name | Description | |
---|---|---|
Abs |
Gets the absolute value of a 128 bit integer.
| |
Compare |
Compares two 128-bit integers.
| |
CompareTo |
Compares the current instance to another 128-bit integer.
| |
DivRem |
Computer the quotient and remainder of two 128-bit integers.
| |
Equals(Object) |
Tests whether the current instance equals another object.
(Overrides ValueTypeEquals(Object).) | |
Equals(Int128) |
Tests whether the current instance equals another 128-bit integer.
| |
Equals(Int128, Int128) |
Tests the equality of two 128-bit integers.
| |
GetHashCode |
Return a hash code for the 128-bit integer.
(Overrides ValueTypeGetHashCode.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Parse |
Produces a 128-bit integer from its string representation.
| |
ToString |
Produces a string representation of the 128-bit integer.
(Overrides ValueTypeToString.) | |
TryParse |
Attempts to produce a 128-bit integer from a string representation.
|
Name | Description | |
---|---|---|
Addition |
Computes the sum of two 128-bit integers.
| |
Decrement |
Decrements a 128-bit unsigned integer.
| |
Division |
Computes the quotient of two 128 bit integers.
| |
Equality |
Tests the equality of two 128-bit integers.
| |
(Double to Int128) |
Converts a floating-point value into a 128-bit integer.
| |
(BigInteger to Int128) |
Converts an arbitrary-size big integer into a 128-bit integer.
| |
(Int128 to Int64) |
Converts a 128-bit integer into a 64-bit integer.
| |
GreaterThan |
Indicates whether the first value is greater than the second value.
| |
GreaterThanOrEqual |
Indicates whether the first value is greater than or equal to the second value.
| |
(Int64 to Int128) |
Converts a 64-bit integer into a 128-bit integer.
| |
(Int128 to Double) |
Converts a 128-bit integer into a floating point value.
| |
(Int128 to BigInteger) |
Converts a 128-bit integer into an arbitrary-size big integer.
| |
Increment |
Increments a 128-bit integer.
| |
Inequality |
Tests the inequality of two 128-bit integers.
| |
LessThan |
Indicates whether the first value is less than the second value.
| |
LessThanOrEqual |
Indicates whether the first value is less than or equal to the second value.
| |
Modulus |
Computes the remainder of two 128 bit integers.
| |
Multiply |
Computes the product of two 128-bit integers.
| |
Subtraction |
Computes the difference of two 128-bit integers.
| |
UnaryNegation |
Negates a 128 bit integer.
|
Name | Description | |
---|---|---|
MaxValue |
The maximum value of the signed 128-bit integer.
| |
MinValue |
The minimum value of the signed 128-bit integer.
| |
One |
The unit value of the signed 128-bit integer.
| |
Zero |
The zero value of the signed 128-bit integer.
|
The built-in integer types short, int, and long are fixed-width registers with the characteristics shown below. Int128 is a 128-bit signed integer register with analogous behavior and greatly extended range.
Type | C# Name | Width | MaxValue |
---|---|---|---|
Int16 | short | 16b (2B) | ~32 X 103 |
Int32 | int | 32b (4B) | ~2.1 X 109 |
Int64 | long | 64b (8B) | ~9.2 X 1018 |
Int128 | Int128 | 128b (16B) | ~1.7 X 1038 |
To instantiate a 128-bit signed integer, you can use the constructor Int128(String), or the parsing methods Parse(String) or TryParse(String, Int128) to parse a decimal representation provided as a string. The parsing then occurs at run-time. If the value you want is representable by a built-in integer type (e.g. Int64), you can simply assign a Int128 variable from a built-in integer type. This is particularly useful in source code, since the compiler will parse fixed values of these types at compile-time.
If you know that the numbers you need to work with all fit in a 128 bit register, arithmetic with Int128 is typically significantly faster than with BigInteger. Addition and subtraction are about six times faster, multiplication is about twice as fast, and division is about 50% faster. (Meta.Numerics itself uses Int128 internally in the implementation of some discrete distributions that require very large integers.)
Operations that overflow and underflow Int128 behave like they do for the native unsigned integer types, with results equal to the lower 128 bits of the full result, or wrapping around from MaxValue to MinValue.
Explicit casts between Int128 and the built-in integer types behave like unchecked explicit casts between the built-in integer types: the low-order bits of the binary representation are preserved and the high-order bits are, if necessary, discarded. This preserves values that are representable by the target type, but values that are not representable by the target type may be transformed in ways that, while correct in terms of the bit-level rules, are unexpected in terms of numerical values. While this is like the behavior of the built-in integer types, it is different than the behavior of BigInteger, which performs casts as if checked, throwing an exception if the value is not representable in the target type. If you want checked behavior, your code must explicitly check whether the value is in the range of the target before casting.
Int128 does not support bit-wise logical and shift operations, but the unsigned 128-bit integer type UInt128 does. If you want to perform bit-wise operations on 128-bit registers, use UInt128.