Click or drag to resize

UInt128 Structure

Represents a 128-bit unsigned integer.

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

The UInt128 type exposes the following members.

Constructors
  NameDescription
Public methodUInt128
Initializes a new 128-bit unsigned integer with the given base-10 representation.
Top
Methods
  NameDescription
Public methodStatic memberCompare
Compares two unsigned 128-bit integers.
Public methodCompareTo
Compares the current instance to another unsigned 128-bit integer.
Public methodStatic memberDivRem(UInt128, UInt128, UInt128)
Computes the quotient and remaineder of two unsigned 128-bit integers.
Public methodStatic memberDivRem(UInt128, UInt32, UInt32)
Divides a 128-bit unsigned integer by a 32-bit unsigned integer.
Public methodEquals(Object)
Tests whether the current instance is equal to another object.
(Overrides ValueTypeEquals(Object).)
Public methodEquals(UInt128)
Tests whether the current instance is equal to another unsigned 128-bit integer.
Public methodStatic memberEquals(UInt128, UInt128)
Tests whether two unsigned 128-bit integers are equal.
Public methodGetHashCode
Gets a hash code for the current instance.
(Overrides ValueTypeGetHashCode.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodStatic memberParse
Creates an unsigned 128-bit integer from its string representation.
Public methodToString
Produces a string representation of the unsigned 128-bit integer.
(Overrides ValueTypeToString.)
Public methodStatic memberTryParse
Attempts to parse the given string as an unsigned 128-bit integer.
Top
Operators
  NameDescription
Public operatorStatic memberAddition
Adds two 128-bit unsigned integers.
Public operatorStatic memberBitwiseAnd
Computes the bitwise AND of two arguments.
Public operatorStatic memberBitwiseOr
Computes the bitwise OR of two arguments.
Public operatorStatic memberDecrement
Decrements a 128-bit unsigned integer.
Public operatorStatic memberDivision
Divides one 128-bit unsigned integer by another.
Public operatorStatic memberEquality
Tests whether two unsigned 128-bit integers are equal.
Public operatorStatic memberExclusiveOr
Computes the bitwise XOR of two arguments.
Public operatorStatic member(Double to UInt128)
Converts a floating-point value into an unsigned 128-bit integer.
Public operatorStatic member(BigInteger to UInt128)
Converts an arbitrary-size big integer into an unsigned 128-bit integer.
Public operatorStatic member(UInt128 to UInt64)
Converts an unsigned 128-bit integer into an unsigned 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(UInt64 to UInt128)
Converts an unsigned 64-bit integer to an unsigned 128-bit integer.
Public operatorStatic member(UInt128 to BigInteger)
Converts an unsigned 128-bit integer into an arbitrary-size big integer.
Public operatorStatic member(UInt128 to Double)
Converts an unsigned 128-bit integer into a floating point value.
Public operatorStatic memberIncrement
Increments a 128-bit unsigned integer.
Public operatorStatic memberInequality
Tests whether two unsigned 128-bit integers are unequal.
Public operatorStatic memberLeftShift
Returns the unsigned 128-bit binary integer obtained by shifting all bits left by the given number of places.
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 when one 128-bit unsigned integer is divided by another.
Public operatorStatic memberMultiply
Multiplies two 128-bit unsigned integers.
Public operatorStatic memberOnesComplement
Computes the bitwise negation of the argument.
Public operatorStatic memberRightShift
Returns the unsigned 128-bit binary integer obtained by shifting all bits right by the given number of places.
Public operatorStatic memberSubtraction
Subtracts one 128 bit unsigned integer from another.
Top
Fields
  NameDescription
Public fieldStatic memberMaxValue
The greatest representable unsigned 128-bit integer.
Public fieldStatic memberMinValue
The least representable unsigned 128-bit integer.
Public fieldStatic memberOne
The unit value of the unsigned 128-bit integer.
Public fieldStatic memberZero
The zero value of the unsigned 128-bit integer.
Top
Remarks

The built-in unsigned integer types ushort, uint, and ulong are fixed-width registers with the characteristics shown below. UInt128 is a 128-bit unsigned integer register with analogous behavior and greatly extended range.

TypeC# NameWidthMaxValue
UInt16ushort16b (2B)~65 X 103
UInt32uint32b (4B)~4.2 X 109
UInt64ulong64b (8B)~18 X 1018
UInt128UInt128128b (16B)~3.4 X 1038

To instantiate a 128-bit unsigned integer, you can use the constructor UInt128(String), or the parsing methods Parse(String) or TryParse(String, UInt128) 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 unsigned integer type (e.g. UInt64), you can simply assign a UInt128 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.

Operations that overflow and underflow UInt128 behave like they do for the native unsigned integer types, with results modulo 2128, or wrapping around from MaxValue to Zero.

Explicit casts between UInt128 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.

If you know that the numbers you need to work with all fit in a 128 bit unsigned register, arithmetic with UInt128 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.

UInt128 also supports bitwise logical and shift operations.

See Also