Skip to content

OnixLabs.Numerics.UInt256

matthew edited this page May 27, 2026 · 2 revisions

UInt256 Struct

Represents a 256-bit unsigned integer stored using two UInt128 halves, with a full generic-math surface that integrates seamlessly with .NET's numeric type system.

Namespace OnixLabs.Numerics
Assembly OnixLabs.Numerics.dll (NuGet)
Inheritance object → ValueType → UInt256
Implements IConvertible, IMinMaxValue<UInt256>, IBinaryInteger<UInt256>, IUnsignedNumber<UInt256>, IValueEquatable<UInt256>, IValueComparable<UInt256>

Definition

[StructLayout(LayoutKind.Sequential)]
public readonly partial struct UInt256 :
    IConvertible,
    IMinMaxValue<UInt256>,
    IBinaryInteger<UInt256>,
    IUnsignedNumber<UInt256>,
    IValueEquatable<UInt256>,
    IValueComparable<UInt256>

Remarks

UInt256 is a 256-bit unsigned integer spanning the range 0 to 2²⁵⁶ − 1. Internally the value is stored as two UInt128 halves: UpperBits and LowerBits.

Because UInt256 implements IBinaryInteger<UInt256> and IUnsignedNumber<UInt256> it participates fully in .NET's generic-math infrastructure. Any generic algorithm constrained to IBinaryInteger<T>, INumber<T>, or INumberBase<T> will accept UInt256 without modification.

Conversions — widening conversions from all built-in unsigned integer types (including char, UInt128) are implicit. Narrowing conversions to smaller integer types, signed integer types, floating-point types (Half, float, double, decimal, Float128, Float256), and the counterpart signed type Int256 are explicit. A lossless implicit widening to BigInteger is also provided. Every narrowing operator has a checked variant that throws OverflowException when the source value is out of range.

Checked arithmetic — all binary arithmetic operators (+, -, *) and the unary - operator provide a checked variant. The standard (unchecked) variants wrap on overflow or underflow; the checked variants throw OverflowException.

Unary negation — operator -(UInt256) returns the two's-complement negation (wrapping). operator checked -(UInt256) throws OverflowException unless value is zero.

Formatting and parsing — ToString supports the standard .NET numeric format strings (D, G, N, X, R, …) and respects the supplied IFormatProvider. Symmetric Parse and TryParse overloads accept both string and ReadOnlySpan<char>, with optional NumberStyles control.

Serialisation — TryWriteBigEndian / TryWriteLittleEndian write the fixed 32-byte representation to a Span<byte>, and TryReadBigEndian / TryReadLittleEndian reconstruct the value from a span of any length, zero-extending when shorter than 32 bytes.

Relationship to siblings — UInt256 is the unsigned counterpart of Int256. Both share the same 256-bit memory layout; (UInt256)int256Value reinterprets the bits directly. The 512-bit sibling UInt512 offers the same API surface at double the precision; UInt256 widens implicitly to UInt512.

Examples

Construction and arithmetic

using OnixLabs.Numerics;

UInt256 a = 1_000_000_000_000_000_000UL;   // implicit from ulong
UInt256 b = UInt256.Parse("999999999999999999999999999999");

UInt256 sum  = a + b;
UInt256 diff = b - a;
UInt256 prod = checked(a * 1000);          // throws OverflowException if result exceeds UInt256 range

Console.WriteLine(UInt256.Sign(sum));      // 0 or 1 (never -1)
Console.WriteLine(UInt256.Abs(a));         // 1000000000000000000
Console.WriteLine(UInt256.MinValue);       // 0
Console.WriteLine(UInt256.MaxValue);       // 115792089237316195423570985008687907853269984665640564039457584007913129639935

Parsing, formatting, and conversion

using OnixLabs.Numerics;
using System.Globalization;
using System.Numerics;

// Parse from decimal string
UInt256 value = UInt256.Parse("123456789012345678901234567890");

// Format as hexadecimal
string hex = value.ToString("X");

// Round-trip through BigInteger
BigInteger big = value;                   // implicit
UInt256 back = checked((UInt256)big);

// Binary-integer operations
UInt256 lzc  = UInt256.LeadingZeroCount(value);
UInt256 pop  = UInt256.PopCount(value);
bool    pow2 = UInt256.IsPow2(value);

// Byte serialisation
Span<byte> buf = stackalloc byte[32];
value.TryWriteLittleEndian(buf, out int written);   // written == 32
UInt256.TryReadLittleEndian(buf, isUnsigned: true, out UInt256 restored);
Console.WriteLine(restored == value);               // True

Fields and Static Properties

Member Type Description
Zero UInt256 The value zero.
One UInt256 The value one.
MinValue UInt256 The smallest representable value: 0.
MaxValue UInt256 The largest representable value: 2²⁵⁶ − 1.
AllBitsSet UInt256 The value with every bit set to one (equal to MaxValue).

Zero

public static UInt256 Zero { get; }

Gets the UInt256 value representing zero. This is the default value of the struct.

One

public static UInt256 One { get; }

Gets the UInt256 value representing one.

MinValue

public static UInt256 MinValue { get; }

Gets the smallest possible UInt256 value: 0 (all bits clear). Equal to default.

MaxValue

public static UInt256 MaxValue { get; }

Gets the largest possible UInt256 value: 2²⁵⁶ − 1 (all bits set). Equal to 115792089237316195423570985008687907853269984665640564039457584007913129639935.

AllBitsSet

public static UInt256 AllBitsSet { get; }

Gets the UInt256 value whose entire 256-bit pattern is ones. This is identical to MaxValue.

Constructors

Constructor Description
UInt256(UInt128, UInt128) Initializes a new instance from the specified upper and lower 128-bit halves.

UInt256(UInt128, UInt128)

public UInt256(UInt128 upper, UInt128 lower)

Initializes a new instance of the UInt256 struct by directly supplying the 128-bit upper and lower halves of the 256-bit value.

Parameter Type Description
upper UInt128 The upper 128 bits.
lower UInt128 The lower 128 bits.

Properties

Property Type Description
UpperBits UInt128 Gets the upper 128 bits of this value.
LowerBits UInt128 Gets the lower 128 bits of this value.

UpperBits

public UInt128 UpperBits { get; }

Gets the upper 128 bits of the current UInt256 value.

LowerBits

public UInt128 LowerBits { get; }

Gets the lower 128 bits of the current UInt256 value.

Methods

Static Methods

Arithmetic

Method Returns Description
Abs(UInt256) UInt256 Returns the value unchanged (absolute value is identity for unsigned types).
Sign(UInt256) int Returns 0 when the value is zero; otherwise 1.
DivRem(UInt256, UInt256) (UInt256 Quotient, UInt256 Remainder) Computes the quotient and remainder in one operation.
BigMul(UInt128, UInt128) UInt256 Computes the exact 256-bit product of two UInt128 values.
BigMul(UInt256, UInt256, out UInt256) UInt256 Computes the full 512-bit product of two UInt256 values, returning the high half.

Comparison and clamping

Method Returns Description
Compare(UInt256, UInt256) int Compares two values and returns a signed ordering indicator.
Min(UInt256, UInt256) UInt256 Returns the smaller of two values.
Max(UInt256, UInt256) UInt256 Returns the larger of two values.
Clamp(UInt256, UInt256, UInt256) UInt256 Clamps a value to an inclusive range.
MinMagnitude(UInt256, UInt256) UInt256 Returns the value with the smaller magnitude (equivalent to Min for unsigned types).
MaxMagnitude(UInt256, UInt256) UInt256 Returns the value with the larger magnitude (equivalent to Max for unsigned types).

Binary-integer operations

Method Returns Description
LeadingZeroCount(UInt256) UInt256 Returns the number of leading zero bits.
TrailingZeroCount(UInt256) UInt256 Returns the number of trailing zero bits.
PopCount(UInt256) UInt256 Returns the number of set bits (population count).
Log2(UInt256) UInt256 Returns the floor base-2 logarithm.
RotateLeft(UInt256, int) UInt256 Rotates the bit pattern left.
RotateRight(UInt256, int) UInt256 Rotates the bit pattern right.

Predicates

Method Returns Description
IsZero(UInt256) bool Returns true when the value equals zero.
IsEvenInteger(UInt256) bool Returns true when the value is even.
IsOddInteger(UInt256) bool Returns true when the value is odd.
IsPow2(UInt256) bool Returns true when the value is a positive power of two.

Parsing

Method Returns Description
Parse(string, IFormatProvider?) UInt256 Parses a decimal string representation.
Parse(string, NumberStyles, IFormatProvider?) UInt256 Parses a string with explicit number styles.
Parse(ReadOnlySpan&lt;char&gt;, IFormatProvider?) UInt256 Parses a span representation.
Parse(ReadOnlySpan&lt;char&gt;, NumberStyles, IFormatProvider?) UInt256 Parses a span with explicit number styles.
TryParse(string?, out UInt256) bool Attempts to parse a string; returns false on failure.
TryParse(string?, IFormatProvider?, out UInt256) bool Attempts to parse a string with a format provider.
TryParse(string?, NumberStyles, IFormatProvider?, out UInt256) bool Attempts to parse a string with number styles and a format provider.
TryParse(ReadOnlySpan&lt;char&gt;, out UInt256) bool Attempts to parse a span; returns false on failure.
TryParse(ReadOnlySpan&lt;char&gt;, IFormatProvider?, out UInt256) bool Attempts to parse a span with a format provider.
TryParse(ReadOnlySpan&lt;char&gt;, NumberStyles, IFormatProvider?, out UInt256) bool Attempts to parse a span with number styles and a format provider.

Binary serialisation

Method Returns Description
TryReadBigEndian(ReadOnlySpan&lt;byte&gt;, bool, out UInt256) bool Reads a value from big-endian bytes.
TryReadLittleEndian(ReadOnlySpan&lt;byte&gt;, bool, out UInt256) bool Reads a value from little-endian bytes.
ReadBigEndian(ReadOnlySpan&lt;byte&gt;, bool) UInt256 Reads a value from big-endian bytes; throws on overflow.
ReadLittleEndian(ReadOnlySpan&lt;byte&gt;, bool) UInt256 Reads a value from little-endian bytes; throws on overflow.

Equality

Method Returns Description
Equals(UInt256, UInt256) bool Determines whether two UInt256 values are equal.

Instance Methods

Method Returns Description
CompareTo(UInt256) int Compares this instance with another UInt256.
CompareTo(object?) int Compares this instance with another object.
Equals(UInt256) bool Determines whether this instance is equal to another UInt256.
Equals(object?) bool Determines whether this instance is equal to another object.
GetHashCode() int Returns a hash code derived from all 256 bits.
GetByteCount() int Returns the fixed byte count (always 32).
GetShortestBitLength() int Returns the minimum number of bits needed to represent this value.
TryFormat(Span&lt;char&gt;, out int, ReadOnlySpan&lt;char&gt;, IFormatProvider?) bool Formats the value into a character span.
TryFormat(Span&lt;byte&gt;, out int, ReadOnlySpan&lt;char&gt;, IFormatProvider?) bool Formats the value into a UTF-8 byte span.
TryWriteBigEndian(Span&lt;byte&gt;, out int) bool Writes the value to a big-endian byte span.
TryWriteLittleEndian(Span&lt;byte&gt;, out int) bool Writes the value to a little-endian byte span.
ToString() string Returns the decimal string representation using the current culture.
ToString(string?) string Returns the string representation using the specified format.
ToString(string?, IFormatProvider?) string Returns the string representation using the specified format and provider.

Abs(UInt256)

public static UInt256 Abs(UInt256 value)

Returns the absolute value of the specified UInt256. Because all UInt256 values are non-negative, this method returns value unchanged.

Parameter Type Description
value UInt256 The value.

Returns — value unchanged.

Sign(UInt256)

public static int Sign(UInt256 value)

Returns an integer indicating the sign of the specified UInt256.

Parameter Type Description
value UInt256 The value whose sign is to be returned.

Returns — 0 if value is zero; 1 if value is positive. Never returns −1.

DivRem(UInt256, UInt256)

public static (UInt256 Quotient, UInt256 Remainder) DivRem(UInt256 left, UInt256 right)

Computes both the quotient and the remainder of dividing left by right in a single operation.

Parameter Type Description
left UInt256 The dividend.
right UInt256 The divisor.

Returns — A tuple (Quotient, Remainder).

Exceptions

Exception Condition
DivideByZeroException right is zero.

BigMul(UInt128, UInt128)

public static UInt256 BigMul(UInt128 a, UInt128 b)

Multiplies two UInt128 values and returns the exact 256-bit product. This operation never overflows.

Parameter Type Description
a UInt128 The first multiplicand.
b UInt128 The second multiplicand.

Returns — The exact 256-bit product of a and b.

BigMul(UInt256, UInt256, out UInt256)

public static UInt256 BigMul(UInt256 left, UInt256 right, out UInt256 low)

Multiplies two UInt256 values and returns the full 512-bit product split into two UInt256 halves. This is used internally by operator checked * to detect overflow.

Parameter Type Description
left UInt256 The left multiplicand.
right UInt256 The right multiplicand.
low out UInt256 The low 256 bits of the product.

Returns — The high 256 bits of the product. If non-zero, the product exceeds the UInt256 range.

Compare(UInt256, UInt256)

public static int Compare(UInt256 left, UInt256 right)

Compares two UInt256 values using unsigned semantics.

Parameter Type Description
left UInt256 The left value.
right UInt256 The right value.

Returns — A negative integer if left precedes right; zero if they are equal; a positive integer if left follows right.

Min(UInt256, UInt256)

public static UInt256 Min(UInt256 x, UInt256 y)

Returns the smaller of two UInt256 values (unsigned comparison).

Parameter Type Description
x UInt256 The first value.
y UInt256 The second value.

Returns — The lesser of x and y.

Max(UInt256, UInt256)

public static UInt256 Max(UInt256 x, UInt256 y)

Returns the larger of two UInt256 values (unsigned comparison).

Parameter Type Description
x UInt256 The first value.
y UInt256 The second value.

Returns — The greater of x and y.

Clamp(UInt256, UInt256, UInt256)

public static UInt256 Clamp(UInt256 value, UInt256 min, UInt256 max)

Returns value clamped to the inclusive range [min, max].

Parameter Type Description
value UInt256 The value to clamp.
min UInt256 The lower bound of the range.
max UInt256 The upper bound of the range.

Returns — min if value < min; max if value > max; otherwise value.

Exceptions

Exception Condition
ArgumentException min is greater than max.

MinMagnitude(UInt256, UInt256)

public static UInt256 MinMagnitude(UInt256 x, UInt256 y)

Returns the value with the smaller magnitude. For unsigned types, magnitude equals value, so this is equivalent to Min.

Parameter Type Description
x UInt256 The first value.
y UInt256 The second value.

Returns — The smaller of x and y.

MaxMagnitude(UInt256, UInt256)

public static UInt256 MaxMagnitude(UInt256 x, UInt256 y)

Returns the value with the larger magnitude. For unsigned types, magnitude equals value, so this is equivalent to Max.

Parameter Type Description
x UInt256 The first value.
y UInt256 The second value.

Returns — The larger of x and y.

LeadingZeroCount(UInt256)

public static UInt256 LeadingZeroCount(UInt256 value)

Returns the number of leading zero bits in the 256-bit representation of value.

Parameter Type Description
value UInt256 The value to inspect.

Returns — A UInt256 in the range [0, 256].

TrailingZeroCount(UInt256)

public static UInt256 TrailingZeroCount(UInt256 value)

Returns the number of trailing zero bits in the 256-bit representation of value.

Parameter Type Description
value UInt256 The value to inspect.

Returns — A UInt256 in the range [0, 256].

PopCount(UInt256)

public static UInt256 PopCount(UInt256 value)

Returns the population count: the number of bits set to one in the 256-bit representation of value.

Parameter Type Description
value UInt256 The value to inspect.

Returns — A UInt256 in the range [0, 256].

Log2(UInt256)

public static UInt256 Log2(UInt256 value)

Returns the truncated base-2 logarithm of value.

Parameter Type Description
value UInt256 The value whose logarithm is to be computed.

Returns — The floor of log₂(value), or zero when value is zero.

RotateLeft(UInt256, int)

public static UInt256 RotateLeft(UInt256 value, int rotateAmount)

Rotates the 256-bit pattern of value to the left by rotateAmount bit positions. The rotate amount is masked to the range [0, 255].

Parameter Type Description
value UInt256 The value to rotate.
rotateAmount int The number of bit positions to rotate.

Returns — value rotated left by rotateAmount bits.

RotateRight(UInt256, int)

public static UInt256 RotateRight(UInt256 value, int rotateAmount)

Rotates the 256-bit pattern of value to the right by rotateAmount bit positions. The rotate amount is masked to the range [0, 255].

Parameter Type Description
value UInt256 The value to rotate.
rotateAmount int The number of bit positions to rotate.

Returns — value rotated right by rotateAmount bits.

IsZero(UInt256)

public static bool IsZero(UInt256 value)

Returns true when value is zero.

IsEvenInteger(UInt256)

public static bool IsEvenInteger(UInt256 value)

Returns true when value is even (least-significant bit is zero).

IsOddInteger(UInt256)

public static bool IsOddInteger(UInt256 value)

Returns true when value is odd (least-significant bit is one).

IsPow2(UInt256)

public static bool IsPow2(UInt256 value)

Returns true when value is a positive integer that is an exact power of two.

Parse(string, IFormatProvider?)

public static UInt256 Parse(string value, IFormatProvider? provider = null)

Parses the decimal string representation of a UInt256.

Parameter Type Description
value string The string to parse.
provider IFormatProvider? An optional culture-specific format provider.

Returns — The parsed UInt256 value.

Exceptions

Exception Condition
FormatException value is not in a valid format.
OverflowException value is outside the range of UInt256.

Parse(string, NumberStyles, IFormatProvider?)

public static UInt256 Parse(string value, NumberStyles style, IFormatProvider? provider = null)

Parses a string representation of a UInt256 using the specified NumberStyles.

Parameter Type Description
value string The string to parse.
style NumberStyles A bitwise combination of number-style flags.
provider IFormatProvider? An optional culture-specific format provider.

Returns — The parsed UInt256 value.

Exceptions

Exception Condition
FormatException value is not in a valid format.
OverflowException value is outside the range of UInt256.

Parse(ReadOnlySpan<char>, IFormatProvider?)

public static UInt256 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)

Parses a span representation of a UInt256.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
provider IFormatProvider? An optional culture-specific format provider.

Returns — The parsed UInt256 value.

Exceptions

Exception Condition
FormatException value is not in a valid format.
OverflowException value is outside the range of UInt256.

Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?)

public static UInt256 Parse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider = null)

Parses a span representation of a UInt256 using the specified NumberStyles.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
style NumberStyles A bitwise combination of number-style flags.
provider IFormatProvider? An optional culture-specific format provider.

Returns — The parsed UInt256 value.

Exceptions

Exception Condition
FormatException value is not in a valid format.
OverflowException value is outside the range of UInt256.

TryParse(string?, out UInt256)

public static bool TryParse(string? value, out UInt256 result)

Attempts to parse a string into a UInt256 value. Returns false without throwing if parsing fails.

Parameter Type Description
value string? The string to parse.
result out UInt256 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

TryParse(string?, IFormatProvider?, out UInt256)

public static bool TryParse(string? value, IFormatProvider? provider, out UInt256 result)

Attempts to parse a string into a UInt256 value using a format provider.

Parameter Type Description
value string? The string to parse.
provider IFormatProvider? An optional culture-specific format provider.
result out UInt256 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

TryParse(string?, NumberStyles, IFormatProvider?, out UInt256)

public static bool TryParse(string? value, NumberStyles style, IFormatProvider? provider, out UInt256 result)

Attempts to parse a string into a UInt256 value using the specified number styles and format provider.

Parameter Type Description
value string? The string to parse.
style NumberStyles A bitwise combination of number-style flags.
provider IFormatProvider? An optional culture-specific format provider.
result out UInt256 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

TryParse(ReadOnlySpan<char>, out UInt256)

public static bool TryParse(ReadOnlySpan<char> value, out UInt256 result)

Attempts to parse a character span into a UInt256 value using the current culture.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
result out UInt256 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

TryParse(ReadOnlySpan<char>, IFormatProvider?, out UInt256)

public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out UInt256 result)

Attempts to parse a character span into a UInt256 value using a format provider.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
provider IFormatProvider? An optional culture-specific format provider.
result out UInt256 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

TryParse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?, out UInt256)

public static bool TryParse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider, out UInt256 result)

Attempts to parse a character span into a UInt256 value using the specified number styles and format provider.

Parameter Type Description
value ReadOnlySpan<char> The character span to parse.
style NumberStyles A bitwise combination of number-style flags.
provider IFormatProvider? An optional culture-specific format provider.
result out UInt256 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

TryReadBigEndian(ReadOnlySpan<byte>, bool, out UInt256)

public static bool TryReadBigEndian(ReadOnlySpan<byte> source, bool isUnsigned, out UInt256 value)

Tries to read a UInt256 from a big-endian byte span. The span may be shorter than 32 bytes; missing high bytes are treated as zero. A span longer than 32 bytes succeeds only when all excess bytes are zero.

Parameter Type Description
source ReadOnlySpan<byte> The big-endian byte span to read from.
isUnsigned bool Indicates whether the source represents an unsigned value.
value out UInt256 The parsed value on success; otherwise, default.

Returns — true if the value was read successfully; otherwise, false.

TryReadLittleEndian(ReadOnlySpan<byte>, bool, out UInt256)

public static bool TryReadLittleEndian(ReadOnlySpan<byte> source, bool isUnsigned, out UInt256 value)

Tries to read a UInt256 from a little-endian byte span. The same length-flexibility rules as TryReadBigEndian apply, but applied to the little-endian representation.

Parameter Type Description
source ReadOnlySpan<byte> The little-endian byte span to read from.
isUnsigned bool Indicates whether the source represents an unsigned value.
value out UInt256 The parsed value on success; otherwise, default.

Returns — true if the value was read successfully; otherwise, false.

ReadBigEndian(ReadOnlySpan<byte>, bool)

public static UInt256 ReadBigEndian(ReadOnlySpan<byte> source, bool isUnsigned)

Reads a UInt256 from a big-endian byte span. Throws if the span contains more significant bytes than fit in a 256-bit value.

Parameter Type Description
source ReadOnlySpan<byte> The big-endian byte span to read from.
isUnsigned bool Indicates whether the source represents an unsigned value.

Returns — The parsed UInt256 value.

Exceptions

Exception Condition
OverflowException source contains excess non-zero bytes beyond 32.

ReadLittleEndian(ReadOnlySpan<byte>, bool)

public static UInt256 ReadLittleEndian(ReadOnlySpan<byte> source, bool isUnsigned)

Reads a UInt256 from a little-endian byte span. Throws if the span contains more significant bytes than fit in a 256-bit value.

Parameter Type Description
source ReadOnlySpan<byte> The little-endian byte span to read from.
isUnsigned bool Indicates whether the source represents an unsigned value.

Returns — The parsed UInt256 value.

Exceptions

Exception Condition
OverflowException source contains excess non-zero bytes beyond 32.

Equals(UInt256, UInt256)

public static bool Equals(UInt256 left, UInt256 right)

Determines whether two UInt256 values are equal by comparing all 256 bits.

Parameter Type Description
left UInt256 The first value.
right UInt256 The second value.

Returns — true if all bits are identical; otherwise, false.


CompareTo(UInt256)

public int CompareTo(UInt256 other)

Compares this instance with another UInt256 value using unsigned semantics.

Parameter Type Description
other UInt256 The value to compare with this instance.

Returns — A negative integer if this instance precedes other; zero if equal; a positive integer if this instance follows other.

CompareTo(object?)

public int CompareTo(object? obj)

Compares this instance with another object.

Parameter Type Description
obj object? The object to compare with this instance. A null reference sorts before any UInt256.

Returns — A signed ordering indicator as per CompareTo(UInt256).

Exceptions

Exception Condition
ArgumentException obj is not null and is not a UInt256.

Equals(UInt256)

public bool Equals(UInt256 other)

Determines whether this instance is equal to another UInt256 value.

Returns — true if the two values are equal; otherwise, false.

Equals(object?)

public override bool Equals(object? obj)

Determines whether this instance is equal to another object.

Returns — true if obj is a UInt256 with the same value; otherwise, false.

GetHashCode()

public override int GetHashCode()

Returns a hash code for this instance. The hash is computed over all 32 bytes of the value rather than just the two UInt128 halves, which avoids collisions between values such as 2^k and 2^(k+32) that would arise from naively XOR-ing the 64-bit sub-halves.

Returns — A 32-bit signed integer hash code.

GetByteCount()

public int GetByteCount()

Returns the number of bytes required to serialise this value in either endianness.

Returns — Always returns 32.

GetShortestBitLength()

public int GetShortestBitLength()

Returns the minimum number of bits needed to represent this value, excluding leading zeros. For zero the result is 0; for positive values it equals ⌊log₂(value)⌋ + 1.

Returns — The shortest bit length in the range [0, 256].

TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)

Tries to format the value into a character span. Uses the same format specifiers as ToString(string?, IFormatProvider?).

Parameter Type Description
destination Span<char> The span into which to write the formatted value.
charsWritten out int The number of characters written on success; 0 on failure.
format ReadOnlySpan<char> A format specifier span, or empty for default.
provider IFormatProvider? An optional culture-specific format provider.

Returns — true if destination was large enough and formatting succeeded; otherwise, false.

TryFormat(Span<byte>, out int, ReadOnlySpan<char>, IFormatProvider?)

public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider)

Tries to format the value as UTF-8 into a byte span.

Parameter Type Description
utf8Destination Span<byte> The byte span into which to write the UTF-8 formatted value.
bytesWritten out int The number of bytes written on success; 0 on failure.
format ReadOnlySpan<char> A format specifier span, or empty for default.
provider IFormatProvider? An optional culture-specific format provider.

Returns — true if utf8Destination was large enough and formatting succeeded; otherwise, false.

TryWriteBigEndian(Span<byte>, out int)

public bool TryWriteBigEndian(Span<byte> destination, out int bytesWritten)

Writes the current value as a 32-byte big-endian representation to destination.

Parameter Type Description
destination Span<byte> The target span; must be at least 32 bytes.
bytesWritten out int 32 on success; 0 if destination is too short.

Returns — true if the value was written successfully; otherwise, false.

TryWriteLittleEndian(Span<byte>, out int)

public bool TryWriteLittleEndian(Span<byte> destination, out int bytesWritten)

Writes the current value as a 32-byte little-endian representation to destination.

Parameter Type Description
destination Span<byte> The target span; must be at least 32 bytes.
bytesWritten out int 32 on success; 0 if destination is too short.

Returns — true if the value was written successfully; otherwise, false.

ToString()

public override string ToString()

Returns the decimal string representation of the current value using the current culture.

Returns — A decimal string such as "115792089237316195423570985008687907853269984665640564039457584007913129639935".

ToString(string?)

public string ToString(string? format)

Returns the string representation of the current value using the specified format and the current culture.

Parameter Type Description
format string? A standard numeric format string, or null for the default decimal format.

Returns — The formatted string.

ToString(string?, IFormatProvider?)

public string ToString(string? format, IFormatProvider? formatProvider)

Returns the string representation of the current value using the specified format and format provider. Supported standard specifiers include D (decimal), G/R (general/round-trip), N (number with group separators), and X (hexadecimal). The X format renders without a leading sign byte, consistent with the BCL uint/ulong behaviour.

Parameter Type Description
format string? A standard numeric format string, or null for the default decimal format.
formatProvider IFormatProvider? An optional culture-specific format provider.

Returns — The formatted string.

Operators

Arithmetic Operators

Operator Returns Description
operator +(UInt256) UInt256 Returns the operand unchanged (unary plus).
operator -(UInt256) UInt256 Returns the two's-complement negation; wraps for non-zero values.
operator checked -(UInt256) UInt256 Returns the negation; throws OverflowException for any non-zero value.
operator ++(UInt256) UInt256 Increments by one, wrapping on overflow.
operator checked ++(UInt256) UInt256 Increments by one; throws OverflowException on overflow.
operator --(UInt256) UInt256 Decrements by one, wrapping on underflow.
operator checked --(UInt256) UInt256 Decrements by one; throws OverflowException on underflow.
operator +(UInt256, UInt256) UInt256 Adds two values, wrapping on overflow.
operator checked +(UInt256, UInt256) UInt256 Adds two values; throws OverflowException on overflow.
operator -(UInt256, UInt256) UInt256 Subtracts one value from another, wrapping on underflow.
operator checked -(UInt256, UInt256) UInt256 Subtracts one value from another; throws OverflowException on underflow.
operator *(UInt256, UInt256) UInt256 Multiplies two values, wrapping on overflow.
operator checked *(UInt256, UInt256) UInt256 Multiplies two values; throws OverflowException on overflow.
operator /(UInt256, UInt256) UInt256 Divides, truncating toward zero.
operator %(UInt256, UInt256) UInt256 Returns the remainder.

Bitwise and Shift Operators

Operator Returns Description
operator &(UInt256, UInt256) UInt256 Bitwise AND.
operator |(UInt256, UInt256) UInt256 Bitwise OR.
operator ^(UInt256, UInt256) UInt256 Bitwise XOR.
operator ~(UInt256) UInt256 Bitwise complement.
operator <<(UInt256, int) UInt256 Left shift; shift amount is masked to [0, 255].
operator >>(UInt256, int) UInt256 Logical right shift (zero-filling); shift amount masked to [0, 255].
operator >>>(UInt256, int) UInt256 Logical right shift (zero-filling); shift amount masked to [0, 255].

Comparison Operators

Operator Returns Description
operator ==(UInt256, UInt256) bool Returns true when the values are equal.
operator !=(UInt256, UInt256) bool Returns true when the values are not equal.
operator <(UInt256, UInt256) bool Returns true when the left value is less than the right.
operator <=(UInt256, UInt256) bool Returns true when the left value is less than or equal to the right.
operator >(UInt256, UInt256) bool Returns true when the left value is greater than the right.
operator >=(UInt256, UInt256) bool Returns true when the left value is greater than or equal to the right.

Implicit Conversion Operators (to UInt256)

Operator Description
implicit operator UInt256(byte) Widens an unsigned byte; zero-extends to 256 bits.
implicit operator UInt256(ushort) Widens a 16-bit unsigned integer; zero-extends.
implicit operator UInt256(uint) Widens a 32-bit unsigned integer; zero-extends.
implicit operator UInt256(ulong) Widens a 64-bit unsigned integer; zero-extends.
implicit operator UInt256(UInt128) Widens a 128-bit unsigned integer; zero-extends.
implicit operator UInt256(char) Widens a char code-point value; zero-extends.
implicit operator BigInteger(UInt256) Lossless widening to BigInteger.

Explicit Conversion Operators (from UInt256)

Operator Description
explicit operator byte(UInt256) Truncates to the low 8 bits.
explicit operator checked byte(UInt256) Throws OverflowException if above byte.MaxValue.
explicit operator sbyte(UInt256) Truncates to the low 8 bits as a signed byte.
explicit operator checked sbyte(UInt256) Throws OverflowException if above sbyte.MaxValue.
explicit operator short(UInt256) Truncates to the low 16 bits as a signed short.
explicit operator checked short(UInt256) Throws OverflowException if above short.MaxValue.
explicit operator ushort(UInt256) Truncates to the low 16 bits.
explicit operator checked ushort(UInt256) Throws OverflowException if above ushort.MaxValue.
explicit operator int(UInt256) Truncates to the low 32 bits as a signed int.
explicit operator checked int(UInt256) Throws OverflowException if above int.MaxValue.
explicit operator uint(UInt256) Truncates to the low 32 bits.
explicit operator checked uint(UInt256) Throws OverflowException if above uint.MaxValue.
explicit operator long(UInt256) Truncates to the low 64 bits as a signed long.
explicit operator checked long(UInt256) Throws OverflowException if above long.MaxValue.
explicit operator ulong(UInt256) Truncates to the low 64 bits.
explicit operator checked ulong(UInt256) Throws OverflowException if above ulong.MaxValue.
explicit operator Int128(UInt256) Truncates to the low 128 bits as a signed Int128.
explicit operator checked Int128(UInt256) Throws OverflowException if above Int128.MaxValue.
explicit operator UInt128(UInt256) Truncates to the low 128 bits.
explicit operator checked UInt128(UInt256) Throws OverflowException if UpperBits is non-zero.
explicit operator char(UInt256) Truncates to the low 16 bits as a char.
explicit operator checked char(UInt256) Throws OverflowException if above char.MaxValue.
explicit operator float(UInt256) Converts to the nearest float (via double).
explicit operator double(UInt256) Converts to the nearest double.
explicit operator Half(UInt256) Converts to the nearest Half (via double).
explicit operator decimal(UInt256) Converts via BigInteger; throws OverflowException if outside decimal range.
explicit operator Float128(UInt256) Converts to the nearest Float128 via BigDecimal.
explicit operator Float256(UInt256) Converts to the nearest Float256 via BigDecimal.

Explicit Conversion Operators (to UInt256)

Operator Description
explicit operator UInt256(sbyte) Two's-complement bit-pattern widening; wraps negative values.
explicit operator checked UInt256(sbyte) Throws OverflowException if negative.
explicit operator UInt256(short) Two's-complement bit-pattern widening; wraps negative values.
explicit operator checked UInt256(short) Throws OverflowException if negative.
explicit operator UInt256(int) Two's-complement bit-pattern widening; wraps negative values.
explicit operator checked UInt256(int) Throws OverflowException if negative.
explicit operator UInt256(long) Two's-complement bit-pattern widening; wraps negative values.
explicit operator checked UInt256(long) Throws OverflowException if negative.
explicit operator UInt256(Int128) Two's-complement bit-pattern widening; wraps negative values.
explicit operator checked UInt256(Int128) Throws OverflowException if negative.
explicit operator UInt256(BigInteger) Truncates to the low 256 bits (wrapping).
explicit operator checked UInt256(BigInteger) Throws OverflowException if negative or exceeds MaxValue.
explicit operator UInt256(Float128) Truncates toward zero; returns Zero for NaN, infinity, negative, or zero.
explicit operator checked UInt256(Float128) Throws OverflowException for NaN, infinity, negative, or out-of-range.
explicit operator UInt256(Float256) Truncates toward zero; returns Zero for NaN, infinity, negative, or zero.
explicit operator checked UInt256(Float256) Throws OverflowException for NaN, infinity, negative, or out-of-range.

operator +(UInt256) (unary)

public static UInt256 operator +(UInt256 value)

Returns the operand unchanged (identity operation).

operator -(UInt256) (unary)

public static UInt256 operator -(UInt256 value)

Returns the two's-complement negation of value (i.e., Zero - value). When value is zero the result is zero; otherwise the result wraps within the unsigned range.

operator checked -(UInt256) (unary)

public static UInt256 operator checked -(UInt256 value)

Returns the negation of value.

Exceptions

Exception Condition
OverflowException value is non-zero (any unsigned negation other than zero overflows).

operator ++(UInt256) (increment)

public static UInt256 operator ++(UInt256 value)

Returns value incremented by one. Wraps to Zero when value is MaxValue.

operator checked ++(UInt256) (increment)

public static UInt256 operator checked ++(UInt256 value)

Returns value incremented by one.

Exceptions

Exception Condition
OverflowException value is MaxValue.

operator --(UInt256) (decrement)

public static UInt256 operator --(UInt256 value)

Returns value decremented by one. Wraps to MaxValue when value is Zero.

operator checked --(UInt256) (decrement)

public static UInt256 operator checked --(UInt256 value)

Returns value decremented by one.

Exceptions

Exception Condition
OverflowException value is Zero.

operator +(UInt256, UInt256)

public static UInt256 operator +(UInt256 left, UInt256 right)

Computes the sum of two values. Wraps on overflow.

operator checked +(UInt256, UInt256)

public static UInt256 operator checked +(UInt256 left, UInt256 right)

Computes the sum of two values.

Exceptions

Exception Condition
OverflowException The result exceeds MaxValue.

operator -(UInt256, UInt256)

public static UInt256 operator -(UInt256 left, UInt256 right)

Computes the difference of two values. Wraps on underflow.

operator checked -(UInt256, UInt256)

public static UInt256 operator checked -(UInt256 left, UInt256 right)

Computes the difference of two values.

Exceptions

Exception Condition
OverflowException left is less than right (result would be negative).

operator *(UInt256, UInt256)

public static UInt256 operator *(UInt256 left, UInt256 right)

Computes the product of two values. Keeps the low 256 bits; high bits wrap on overflow.

operator checked *(UInt256, UInt256)

public static UInt256 operator checked *(UInt256 left, UInt256 right)

Computes the product of two values.

Exceptions

Exception Condition
OverflowException The product exceeds MaxValue.

operator /(UInt256, UInt256)

public static UInt256 operator /(UInt256 left, UInt256 right)

Divides left by right, truncating toward zero.

Exceptions

Exception Condition
DivideByZeroException right is zero.

operator %(UInt256, UInt256)

public static UInt256 operator %(UInt256 left, UInt256 right)

Returns the remainder of dividing left by right. The result is always non-negative.

Exceptions

Exception Condition
DivideByZeroException right is zero.

operator &(UInt256, UInt256)

public static UInt256 operator &(UInt256 left, UInt256 right)

Returns the bitwise AND of left and right.

operator |(UInt256, UInt256)

public static UInt256 operator |(UInt256 left, UInt256 right)

Returns the bitwise OR of left and right.

operator ^(UInt256, UInt256)

public static UInt256 operator ^(UInt256 left, UInt256 right)

Returns the bitwise XOR of left and right.

operator ~(UInt256) (bitwise complement)

public static UInt256 operator ~(UInt256 value)

Returns the bitwise complement of value (all bits flipped). The result equals MaxValue - value.

operator <<(UInt256, int)

public static UInt256 operator <<(UInt256 value, int shiftAmount)

Shifts value left by shiftAmount bit positions. The shift amount is masked to [0, 255].

operator >>(UInt256, int)

public static UInt256 operator >>(UInt256 value, int shiftAmount)

Shifts value right by shiftAmount bit positions, filling vacated high bits with zero (logical right shift, identical to >>>). The shift amount is masked to [0, 255].

operator >>>(UInt256, int)

public static UInt256 operator >>>(UInt256 value, int shiftAmount)

Shifts value right by shiftAmount bit positions, filling vacated high bits with zero (unsigned logical right shift). The shift amount is masked to [0, 255].

operator ==(UInt256, UInt256)

public static bool operator ==(UInt256 left, UInt256 right)

Returns true when left and right represent the same 256-bit value.

operator !=(UInt256, UInt256)

public static bool operator !=(UInt256 left, UInt256 right)

Returns true when left and right represent different values.

operator <(UInt256, UInt256)

public static bool operator <(UInt256 left, UInt256 right)

Returns true when left is strictly less than right (unsigned comparison).

operator <=(UInt256, UInt256)

public static bool operator <=(UInt256 left, UInt256 right)

Returns true when left is less than or equal to right (unsigned comparison).

operator >(UInt256, UInt256)

public static bool operator >(UInt256 left, UInt256 right)

Returns true when left is strictly greater than right (unsigned comparison).

operator >=(UInt256, UInt256)

public static bool operator >=(UInt256 left, UInt256 right)

Returns true when left is greater than or equal to right (unsigned comparison).

See Also

ONIXLabs .NET Library


Clone this wiki locally