Click or drag to resize

Int128 Structure

Represents a signed integer with a 128 bit register width.

Namespace:  Meta.Numerics.Extended
Assembly:  Meta.Numerics (in Meta.Numerics.dll) Version: 4.1.4
Syntax
public struct Int128 : IEquatable<Int128>, 
	IComparable<Int128>

The Int128 type exposes the following members.

Constructors
  NameDescription
Public methodInt128
Initializes a new 128-bit integer with the given decimal representation.
Top
Methods
  NameDescription
Public methodStatic memberAbs
Gets the absolute value of a 128 bit integer.
Public methodStatic memberCompare
Compares two 128-bit integers.
Public methodCompareTo
Compares the current instance to another 128-bit integer.
Public methodStatic memberDivRem
Computer the quotient and remainder of two 128-bit integers.
Public methodEquals(Object)
Tests whether the current instance equals another object.
(Overrides ValueTypeEquals(Object).)
Public methodEquals(Int128)
Tests whether the current instance equals another 128-bit integer.
Public methodStatic memberEquals(Int128, Int128)
Tests the equality of two 128-bit integers.
Public methodGetHashCode
Return a hash code for the 128-bit integer.
(Overrides ValueTypeGetHashCode.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodStatic memberParse
Produces a 128-bit integer from its string representation.
Public methodToString
Produces a string representation of the 128-bit integer.
(Overrides ValueTypeToString.)
Public methodStatic memberTryParse
Attempts to produce a 128-bit integer from a string representation.
Top
Operators
  NameDescription
Public operatorStatic memberAddition
Computes the sum of two 128-bit integers.
Public operatorStatic memberDecrement
Decrements a 128-bit unsigned integer.
Public operatorStatic memberDivision
Computes the quotient of two 128 bit integers.
Public operatorStatic memberEquality
Tests the equality of two 128-bit integers.
Public operatorStatic member(Double to Int128)
Converts a floating-point value into a 128-bit integer.
Public operatorStatic member(BigInteger to Int128)
Converts an arbitrary-size big integer into a 128-bit integer.
Public operatorStatic member(Int128 to Int64)
Converts a 128-bit integer into a 64-bit integer.
Public operatorStatic memberGreaterThan
Indicates whether the first value is greater than the second value.
Public operatorStatic memberGreaterThanOrEqual
Indicates whether the first value is greater than or equal to the second value.
Public operatorStatic member(Int64 to Int128)
Converts a 64-bit integer into a 128-bit integer.
Public operatorStatic member(Int128 to Double)
Converts a 128-bit integer into a floating point value.
Public operatorStatic member(Int128 to BigInteger)
Converts a 128-bit integer into an arbitrary-size big integer.
Public operatorStatic memberIncrement
Increments a 128-bit integer.
Public operatorStatic memberInequality
Tests the inequality of two 128-bit integers.
Public operatorStatic memberLessThan
Indicates whether the first value is less than the second value.
Public operatorStatic memberLessThanOrEqual
Indicates whether the first value is less than or equal to the second value.
Public operatorStatic memberModulus
Computes the remainder of two 128 bit integers.
Public operatorStatic memberMultiply
Computes the product of two 128-bit integers.
Public operatorStatic memberSubtraction
Computes the difference of two 128-bit integers.
Public operatorStatic memberUnaryNegation
Negates a 128 bit integer.
Top
Fields
  NameDescription
Public fieldStatic memberMaxValue
The maximum value of the signed 128-bit integer.
Public fieldStatic memberMinValue
The minimum value of the signed 128-bit integer.
Public fieldStatic memberOne
The unit value of the signed 128-bit integer.
Public fieldStatic memberZero
The zero value of the signed 128-bit integer.
Top
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.

See Also