Int128 Structure

Represents a signed integer with a 128 bit register width.

Definition

Namespace: Meta.Numerics.Extended
Assembly: Meta.Numerics (in Meta.Numerics.dll) Version: 4.2.0+6d77d64445f7d5d91b12e331399c4362ecb25333
C#
public readonly struct Int128 : IEquatable<Int128>, 
	IComparable<Int128>
Inheritance
Object    ValueType    Int128
Implements
IComparableInt128, IEquatableInt128

Remarks

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.

TypeC# NameWidthMaxValue
Int16short16b (2B)~32 X 103
Int32int32b (4B)~2.1 X 109
Int64long64b (8B)~9.2 X 1018
Int128Int128128b (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.

Constructors

Int128 Initializes a new 128-bit integer with the given decimal representation.

Methods

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(Int128) Tests whether the current instance equals another 128-bit integer.
Equals(Object) Tests whether the current instance equals another object.
(Overrides ValueTypeEquals(Object))
Equals(Int128, Int128) Tests the equality of two 128-bit integers.
GetHashCode Return a hash code for the 128-bit integer.
(Overrides ValueTypeGetHashCode)
GetTypeGets 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.

Operators

Addition(Int128, Int128) Computes the sum of two 128-bit integers.
Decrement(Int128) Decrements a 128-bit unsigned integer.
Division(Int128, Int128) Computes the quotient of two 128 bit integers.
Equality(Int128, Int128) Tests the equality of two 128-bit integers.
(BigInteger to Int128) Converts an arbitrary-size big integer into a 128-bit integer.
(Double to Int128) Converts a floating-point value into a 128-bit integer.
(Int128 to Int64) Converts a 128-bit integer into a 64-bit integer.
GreaterThan(Int128, Int128) Indicates whether the first value is greater than the second value.
GreaterThanOrEqual(Int128, Int128) Indicates whether the first value is greater than or equal to the second value.
(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.
(Int64 to Int128) Converts a 64-bit integer into a 128-bit integer.
Increment(Int128) Increments a 128-bit integer.
Inequality(Int128, Int128) Tests the inequality of two 128-bit integers.
LessThan(Int128, Int128) Indicates whether the first value is less than the second value.
LessThanOrEqual(Int128, Int128) Indicates whether the first value is less than or equal to the second value.
Modulus(Int128, Int128) Computes the remainder of two 128 bit integers.
Multiply(Int128, Int128) Computes the product of two 128-bit integers.
Subtraction(Int128, Int128) Computes the difference of two 128-bit integers.
UnaryNegation(Int128) Negates a 128 bit integer.

Fields

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.

See Also