Skip to content

OnixLabs.Numerics.UInt512

matthew edited this page May 27, 2026 · 2 revisions

UInt512 Struct

Represents a 512-bit unsigned integer 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 → UInt512
Implements IConvertible, IMinMaxValue<UInt512>, IBinaryInteger<UInt512>, IUnsignedNumber<UInt512>, IValueEquatable<UInt512>, IValueComparable<UInt512>

Definition

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

Remarks

UInt512 is a 512-bit unsigned integer spanning the range 0 to 2⁵¹² − 1. Internally the value is stored as two UInt256 halves: UpperBits (the most-significant 256 bits) and LowerBits (the least-significant 256 bits).

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

Conversions — widening conversions from all built-in unsigned integer types (byte, ushort, uint, ulong, UInt128, char) and from UInt256 are implicit. Narrowing conversions to smaller integer types, floating-point types (Half, float, double, decimal, Float128, Float256), and the signed counterparts (sbyte, short, int, long, Int128) are explicit. A lossless implicit widening to BigInteger is also provided. Signed-integer source types whose values are negative can be converted with wrapping (two's-complement) or with a checked variant that throws OverflowException. Every narrowing operator from UInt512 has a checked variant that throws OverflowException when the value is out of range.

Checked arithmetic — all binary arithmetic operators (+, -, *) and their unary counterparts provide a checked variant. The standard (unchecked) variants wrap on overflow or underflow; the checked variants throw OverflowException. The unary - operator performs a two's-complement negation (wrapping); its checked variant throws OverflowException for any non-zero value, because no non-zero unsigned value has a representable negation within the unsigned range.

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 64-byte representation to a Span<byte>, and TryReadBigEndian / TryReadLittleEndian reconstruct the value from a span of any length, zero-extending when shorter than 64 bytes. Throwing variants ReadBigEndian / ReadLittleEndian are also provided.

Wide multiplication — BigMul(UInt256, UInt256) computes the exact 512-bit product of two 256-bit operands. BigMul(UInt512, UInt512, out UInt512) computes the full 1024-bit product, returning the high 512 bits and writing the low 512 bits to an out parameter.

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

Examples

Construction and arithmetic

using OnixLabs.Numerics;

UInt512 a = 1_000_000_000_000_000_000UL;   // implicit from ulong
UInt512 b = UInt512.Parse("999999999999999999999999999999999999999999999999999999999999999");

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

Console.WriteLine(UInt512.Sign(sum));       // 0 or 1
Console.WriteLine(UInt512.Abs(a));          // 1000000000000000000
Console.WriteLine(UInt512.MinValue);        // 0
Console.WriteLine(UInt512.MaxValue);        // 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095

Parsing, formatting, and conversion

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

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

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

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

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

// Wide multiplication
UInt512 high = UInt512.BigMul(UInt512.MaxValue, UInt512.MaxValue, out UInt512 low);

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

Fields and Static Properties

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

Zero

public static UInt512 Zero { get; }

Gets the UInt512 value representing zero. This is the default value of the struct and is equal to MinValue.

One

public static UInt512 One { get; }

Gets the UInt512 value representing positive one.

MinValue

public static UInt512 MinValue { get; }

Gets the smallest possible UInt512 value: 0. Equal to Zero.

MaxValue

public static UInt512 MaxValue { get; }

Gets the largest possible UInt512 value: 2⁵¹² − 1 (all 512 bits set). Equal to 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095.

AllBitsSet

public static UInt512 AllBitsSet { get; }

Gets the UInt512 value whose entire 512-bit pattern is ones. This is identical to MaxValue.

Constructors

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

UInt512(UInt256, UInt256)

public UInt512(UInt256 upper, UInt256 lower)

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

Parameter Type Description
upper UInt256 The upper 256 bits.
lower UInt256 The lower 256 bits.

Properties

Property Type Description
UpperBits UInt256 Gets the upper 256 bits of this value.
LowerBits UInt256 Gets the lower 256 bits of this value.

UpperBits

public UInt256 UpperBits { get; }

Gets the upper 256 bits of the current UInt512 value.

LowerBits

public UInt256 LowerBits { get; }

Gets the lower 256 bits of the current UInt512 value.

Methods

Static Methods

Arithmetic

Method Returns Description
Abs(UInt512) UInt512 Returns the value unchanged (unsigned absolute value is the identity).
Sign(UInt512) int Returns 0 if the value is zero; otherwise 1.
DivRem(UInt512, UInt512) (UInt512 Quotient, UInt512 Remainder) Computes the quotient and remainder in one operation.
BigMul(UInt256, UInt256) UInt512 Computes the exact 512-bit product of two 256-bit values.
BigMul(UInt512, UInt512, out UInt512) UInt512 Computes the full 1024-bit product; returns the high 512 bits.
DivRemBy256(UInt512, UInt256, out UInt256) UInt256 Divides a 512-bit dividend by a 256-bit divisor; returns the 256-bit quotient.

Comparison and clamping

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

Binary-integer operations

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

Predicates

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

Parsing

Method Returns Description
Parse(string, IFormatProvider?) UInt512 Parses a decimal string representation.
Parse(string, NumberStyles, IFormatProvider?) UInt512 Parses a string with explicit number styles.
Parse(ReadOnlySpan&lt;char&gt;, IFormatProvider?) UInt512 Parses a span representation.
Parse(ReadOnlySpan&lt;char&gt;, NumberStyles, IFormatProvider?) UInt512 Parses a span with explicit number styles.
TryParse(string?, out UInt512) bool Attempts to parse a string; returns false on failure.
TryParse(string?, IFormatProvider?, out UInt512) bool Attempts to parse a string with a format provider.
TryParse(string?, NumberStyles, IFormatProvider?, out UInt512) bool Attempts to parse a string with number styles and a format provider.
TryParse(ReadOnlySpan&lt;char&gt;, out UInt512) bool Attempts to parse a span; returns false on failure.
TryParse(ReadOnlySpan&lt;char&gt;, IFormatProvider?, out UInt512) bool Attempts to parse a span with a format provider.
TryParse(ReadOnlySpan&lt;char&gt;, NumberStyles, IFormatProvider?, out UInt512) 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 UInt512) bool Reads a value from big-endian bytes.
TryReadLittleEndian(ReadOnlySpan&lt;byte&gt;, bool, out UInt512) bool Reads a value from little-endian bytes.
ReadBigEndian(ReadOnlySpan&lt;byte&gt;, bool) UInt512 Reads a value from big-endian bytes; throws on overflow.
ReadLittleEndian(ReadOnlySpan&lt;byte&gt;, bool) UInt512 Reads a value from little-endian bytes; throws on overflow.

Equality

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

Instance Methods

Method Returns Description
CompareTo(UInt512) int Compares this instance with another UInt512.
CompareTo(object?) int Compares this instance with another object.
Equals(UInt512) bool Determines whether this instance is equal to another UInt512.
Equals(object?) bool Determines whether this instance is equal to another object.
GetHashCode() int Returns a hash code derived from all 512 bits.
GetByteCount() int Returns the fixed byte count (always 64).
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(UInt512)

public static UInt512 Abs(UInt512 value)

Returns the absolute value of the specified UInt512. Because UInt512 is unsigned, this is always the identity operation.

Parameter Type Description
value UInt512 The value.

Returns — value unchanged.

Sign(UInt512)

public static int Sign(UInt512 value)

Returns an integer indicating the sign of the specified UInt512.

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

Returns — 0 if value is zero; 1 if value is non-zero.

DivRem(UInt512, UInt512)

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

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

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

Returns — A tuple (Quotient, Remainder).

Exceptions

Exception Condition
DivideByZeroException right is zero.

BigMul(UInt256, UInt256)

public static UInt512 BigMul(UInt256 a, UInt256 b)

Multiplies two UInt256 values and returns the exact 512-bit product.

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

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

BigMul(UInt512, UInt512, out UInt512)

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

Multiplies two UInt512 values and returns the full 1024-bit product split into two UInt512 halves.

Parameter Type Description
left UInt512 The left multiplicand.
right UInt512 The right multiplicand.
low out UInt512 When this method returns, contains the low 512 bits of the product.

Returns — The high 512 bits of the product.

DivRemBy256(UInt512, UInt256, out UInt256)

public static UInt256 DivRemBy256(UInt512 dividend, UInt256 divisor, out UInt256 remainder)

Divides a 512-bit dividend by a 256-bit divisor, returning the 256-bit quotient and the remainder. The caller is responsible for ensuring the quotient fits in 256 bits (i.e. dividend.UpperBits < divisor).

Parameter Type Description
dividend UInt512 The 512-bit dividend.
divisor UInt256 The 256-bit divisor.
remainder out UInt256 When this method returns, contains the 256-bit remainder.

Returns — The 256-bit quotient.

Exceptions

Exception Condition
DivideByZeroException divisor is zero.

Compare(UInt512, UInt512)

public static int Compare(UInt512 left, UInt512 right)

Compares two UInt512 values using unsigned semantics.

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

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

Min(UInt512, UInt512)

public static UInt512 Min(UInt512 x, UInt512 y)

Returns the smaller of two UInt512 values.

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

Returns — The lesser of x and y.

Max(UInt512, UInt512)

public static UInt512 Max(UInt512 x, UInt512 y)

Returns the larger of two UInt512 values.

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

Returns — The greater of x and y.

Clamp(UInt512, UInt512, UInt512)

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

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

Parameter Type Description
value UInt512 The value to clamp.
min UInt512 The lower bound of the range.
max UInt512 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(UInt512, UInt512)

public static UInt512 MinMagnitude(UInt512 x, UInt512 y)

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

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

Returns — The lesser of x and y.

MaxMagnitude(UInt512, UInt512)

public static UInt512 MaxMagnitude(UInt512 x, UInt512 y)

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

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

Returns — The greater of x and y.

LeadingZeroCount(UInt512)

public static UInt512 LeadingZeroCount(UInt512 value)

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

Parameter Type Description
value UInt512 The value to inspect.

Returns — A UInt512 in the range [0, 512].

TrailingZeroCount(UInt512)

public static UInt512 TrailingZeroCount(UInt512 value)

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

Parameter Type Description
value UInt512 The value to inspect.

Returns — A UInt512 in the range [0, 512].

PopCount(UInt512)

public static UInt512 PopCount(UInt512 value)

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

Parameter Type Description
value UInt512 The value to inspect.

Returns — A UInt512 in the range [0, 512].

Log2(UInt512)

public static UInt512 Log2(UInt512 value)

Returns the truncated base-2 logarithm of value.

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

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

RotateLeft(UInt512, int)

public static UInt512 RotateLeft(UInt512 value, int rotateAmount)

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

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

Returns — value rotated left by rotateAmount bits.

RotateRight(UInt512, int)

public static UInt512 RotateRight(UInt512 value, int rotateAmount)

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

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

Returns — value rotated right by rotateAmount bits.

IsZero(UInt512)

public static bool IsZero(UInt512 value)

Returns true when value is zero.

IsEvenInteger(UInt512)

public static bool IsEvenInteger(UInt512 value)

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

IsOddInteger(UInt512)

public static bool IsOddInteger(UInt512 value)

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

IsPow2(UInt512)

public static bool IsPow2(UInt512 value)

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

Parse(string, IFormatProvider?)

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

Parses the decimal string representation of a UInt512.

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

Returns — The parsed UInt512 value.

Exceptions

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

Parse(string, NumberStyles, IFormatProvider?)

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

Parses a string representation of a UInt512 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 UInt512 value.

Exceptions

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

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

Parses a span representation of a UInt512.

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

Returns — The parsed UInt512 value.

Exceptions

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

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

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

Parses a span representation of a UInt512 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 UInt512 value.

Exceptions

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

TryParse(string?, out UInt512)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

TryParse(string?, IFormatProvider?, out UInt512)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

Attempts to parse a string into a UInt512 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 UInt512 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

TryParse(ReadOnlySpan<char>, out UInt512)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

Attempts to parse a character span into a UInt512 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 UInt512 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

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

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

Attempts to parse a character span into a UInt512 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 UInt512 The parsed value on success; otherwise, default.

Returns — true if parsing succeeded; otherwise, false.

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

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

Tries to read a UInt512 from a big-endian byte span. The span may be shorter than 64 bytes; it will be zero-extended to fill the 512-bit representation. A span longer than 64 bytes succeeds only when all the excess bytes are zero.

Parameter Type Description
source ReadOnlySpan<byte> The big-endian byte span to read from.
isUnsigned bool Reserved for interface symmetry; ignored for the unsigned UInt512 type.
value out UInt512 The parsed value on success; otherwise, default.

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

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

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

Tries to read a UInt512 from a little-endian byte span. The same length-flexibility and zero-extension 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 Reserved for interface symmetry; ignored for the unsigned UInt512 type.
value out UInt512 The parsed value on success; otherwise, default.

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

ReadBigEndian(ReadOnlySpan<byte>, bool)

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

Reads a UInt512 from a big-endian byte span, throwing if the source contains non-zero bytes beyond the 64-byte window.

Parameter Type Description
source ReadOnlySpan<byte> The big-endian byte span to read from.
isUnsigned bool Reserved for interface symmetry; ignored for the unsigned UInt512 type.

Returns — The read value.

Exceptions

Exception Condition
OverflowException The source has more significant bytes than fit in a UInt512.

ReadLittleEndian(ReadOnlySpan<byte>, bool)

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

Reads a UInt512 from a little-endian byte span, throwing if the source contains non-zero bytes beyond the 64-byte window.

Parameter Type Description
source ReadOnlySpan<byte> The little-endian byte span to read from.
isUnsigned bool Reserved for interface symmetry; ignored for the unsigned UInt512 type.

Returns — The read value.

Exceptions

Exception Condition
OverflowException The source has more significant bytes than fit in a UInt512.

Equals(UInt512, UInt512)

public static bool Equals(UInt512 left, UInt512 right)

Determines whether two UInt512 values are equal by comparing all 512 bits.

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

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


CompareTo(UInt512)

public int CompareTo(UInt512 other)

Compares this instance with another UInt512 value using unsigned semantics.

Parameter Type Description
other UInt512 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 UInt512.

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

Exceptions

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

Equals(UInt512)

public bool Equals(UInt512 other)

Determines whether this instance is equal to another UInt512 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 UInt512 with the same value; otherwise, false.

GetHashCode()

public override int GetHashCode()

Returns a hash code for this instance. The hash is computed over all 64 bytes of the value rather than combining the UInt256 half hashes, which avoids collisions between values such as 2^k and 2^(k+32) that would arise from naively XOR-ing 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 64.

GetShortestBitLength()

public int GetShortestBitLength()

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

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

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 64-byte big-endian representation to destination.

Parameter Type Description
destination Span<byte> The target span; must be at least 64 bytes.
bytesWritten out int 64 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 64-byte little-endian representation to destination.

Parameter Type Description
destination Span<byte> The target span; must be at least 64 bytes.
bytesWritten out int 64 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 "123456789012345678901234567890".

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). For X and R format, the output is routed through NumberInfoFormatter to match .NET integer conventions and to avoid BigInteger's positive-sign-disambiguation leading zero on top-bit-set values.

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 +(UInt512) UInt512 Returns the operand unchanged (unary plus).
operator -(UInt512) UInt512 Returns the two's-complement negation; wraps for non-zero values.
operator checked -(UInt512) UInt512 Returns zero for zero input; throws OverflowException for any non-zero input.
operator ++(UInt512) UInt512 Increments by one, wrapping on overflow.
operator checked ++(UInt512) UInt512 Increments by one; throws OverflowException on overflow.
operator --(UInt512) UInt512 Decrements by one, wrapping on underflow.
operator checked --(UInt512) UInt512 Decrements by one; throws OverflowException on underflow.
operator +(UInt512, UInt512) UInt512 Adds two values, wrapping on overflow.
operator checked +(UInt512, UInt512) UInt512 Adds two values; throws OverflowException on overflow.
operator -(UInt512, UInt512) UInt512 Subtracts one value from another, wrapping on underflow.
operator checked -(UInt512, UInt512) UInt512 Subtracts one value from another; throws OverflowException on underflow.
operator *(UInt512, UInt512) UInt512 Multiplies two values, keeping the low 512 bits on overflow.
operator checked *(UInt512, UInt512) UInt512 Multiplies two values; throws OverflowException on overflow.
operator /(UInt512, UInt512) UInt512 Divides, truncating toward zero.
operator %(UInt512, UInt512) UInt512 Returns the remainder.

Bitwise and Shift Operators

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

Comparison Operators

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

Implicit Conversion Operators (to UInt512)

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

Explicit Conversion Operators (from signed integer types to UInt512)

Operator Description
explicit operator UInt512(sbyte) Two's-complement wrap for negative values; zero-extends non-negative values.
explicit operator checked UInt512(sbyte) Throws OverflowException if value is negative.
explicit operator UInt512(short) Two's-complement wrap for negative values; zero-extends non-negative values.
explicit operator checked UInt512(short) Throws OverflowException if value is negative.
explicit operator UInt512(int) Two's-complement wrap for negative values; zero-extends non-negative values.
explicit operator checked UInt512(int) Throws OverflowException if value is negative.
explicit operator UInt512(long) Two's-complement wrap for negative values; zero-extends non-negative values.
explicit operator checked UInt512(long) Throws OverflowException if value is negative.
explicit operator UInt512(Int128) Two's-complement wrap for negative values; zero-extends non-negative values.
explicit operator checked UInt512(Int128) Throws OverflowException if value is negative.
explicit operator UInt512(BigInteger) Truncates to the low 512 bits (wrapping).
explicit operator checked UInt512(BigInteger) Throws OverflowException if value is negative or exceeds MaxValue.

Explicit Conversion Operators (from UInt512)

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

Explicit Conversion Operators (to UInt512 from floating-point types)

Operator Description
explicit operator UInt512(Float128) Truncates toward zero; returns Zero for NaN, infinity, negative, or zero input.
explicit operator checked UInt512(Float128) Throws OverflowException for NaN, infinity, negative, or out-of-range input.
explicit operator UInt512(Float256) Truncates toward zero; returns Zero for NaN, infinity, negative, or zero input.
explicit operator checked UInt512(Float256) Throws OverflowException for NaN, infinity, negative, or out-of-range input.

operator +(UInt512) (unary)

public static UInt512 operator +(UInt512 value)

Returns the operand unchanged (identity operation).

operator -(UInt512) (unary)

public static UInt512 operator -(UInt512 value)

Returns the two's-complement negation of value. Because UInt512 is unsigned, a non-zero result wraps: the negation of x is 2⁵¹² − x.

operator checked -(UInt512) (unary)

public static UInt512 operator checked -(UInt512 value)

Returns Zero when value is zero.

Exceptions

Exception Condition
OverflowException value is non-zero (no non-zero unsigned value has a representable negation).

operator ++(UInt512) (increment)

public static UInt512 operator ++(UInt512 value)

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

operator checked ++(UInt512) (increment)

public static UInt512 operator checked ++(UInt512 value)

Returns value incremented by one.

Exceptions

Exception Condition
OverflowException value is MaxValue.

operator --(UInt512) (decrement)

public static UInt512 operator --(UInt512 value)

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

operator checked --(UInt512) (decrement)

public static UInt512 operator checked --(UInt512 value)

Returns value decremented by one.

Exceptions

Exception Condition
OverflowException value is Zero.

operator +(UInt512, UInt512)

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

Computes the sum of two values. Wraps on overflow.

operator checked +(UInt512, UInt512)

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

Computes the sum of two values.

Exceptions

Exception Condition
OverflowException The result exceeds MaxValue.

operator -(UInt512, UInt512)

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

Computes the difference of two values. Wraps on underflow.

operator checked -(UInt512, UInt512)

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

Computes the difference of two values.

Exceptions

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

operator *(UInt512, UInt512)

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

Computes the product of two values, keeping only the low 512 bits on overflow.

operator checked *(UInt512, UInt512)

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

Computes the product of two values.

Exceptions

Exception Condition
OverflowException The result exceeds MaxValue.

operator /(UInt512, UInt512)

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

Divides left by right, truncating toward zero.

Exceptions

Exception Condition
DivideByZeroException right is zero.

operator %(UInt512, UInt512)

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

Returns the remainder of dividing left by right.

Exceptions

Exception Condition
DivideByZeroException right is zero.

operator &(UInt512, UInt512)

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

Returns the bitwise AND of left and right.

operator |(UInt512, UInt512)

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

Returns the bitwise OR of left and right.

operator ^(UInt512, UInt512)

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

Returns the bitwise XOR of left and right.

operator ~(UInt512) (bitwise complement)

public static UInt512 operator ~(UInt512 value)

Returns the bitwise complement of value (all bits flipped). Equal to MaxValue - value.

operator <<(UInt512, int)

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

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

operator >>(UInt512, int)

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

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

operator >>>(UInt512, int)

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

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

operator ==(UInt512, UInt512)

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

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

operator !=(UInt512, UInt512)

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

Returns true when left and right represent different values.

operator <(UInt512, UInt512)

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

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

operator <=(UInt512, UInt512)

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

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

operator >(UInt512, UInt512)

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

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

operator >=(UInt512, UInt512)

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

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

See Also

ONIXLabs .NET Library


Clone this wiki locally