Skip to content

OnixLabs.Numerics.Int512

matthew edited this page May 27, 2026 · 2 revisions

Int512 Struct

Represents a 512-bit signed integer stored in two's-complement format, 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 → Int512
Implements IConvertible, IMinMaxValue<Int512>, IBinaryInteger<Int512>, ISignedNumber<Int512>, IValueEquatable<Int512>, IValueComparable<Int512>

Definition

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

Remarks

Int512 is a 512-bit signed integer represented in two's-complement, spanning the range −2⁵¹¹ to 2⁵¹¹ − 1. Internally the value is stored as two UInt256 halves: UpperBits (whose high bit encodes the sign) and LowerBits.

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

Conversions — widening conversions from all built-in signed and unsigned integer types (including char, Int128, UInt128, Int256, and UInt256) are implicit. Narrowing conversions to smaller integer types, floating-point types (Half, float, double, decimal, Float128, Float256), and the counterpart unsigned type UInt512 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 -) provide a checked variant. The standard (unchecked) variants wrap on overflow; the checked variants throw OverflowException.

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, applying sign-extension when shorter than 64 bytes.

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

Examples

Construction and arithmetic

using OnixLabs.Numerics;

Int512 a = 1_000_000_000_000_000_000L;   // implicit from long
Int512 b = Int512.Parse("-999999999999999999999999999999999999999999999999999999999999999");

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

Console.WriteLine(Int512.Sign(sum));     // -1, 0, or 1
Console.WriteLine(Int512.Abs(a));        // 1000000000000000000
Console.WriteLine(Int512.MinValue);      // -6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042048
Console.WriteLine(Int512.MaxValue);      //  6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042047

Parsing, formatting, and conversion

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

// Parse from decimal string
Int512 value = Int512.Parse("123456789012345678901234567890123456789012345678901234567890");

// Format as hexadecimal (two's-complement, upper-case)
string hex = value.ToString("X");

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

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

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

Fields and Static Properties

Member Type Description
Zero Int512 The value zero.
One Int512 The value one.
NegativeOne Int512 The value negative one (all bits set).
MinValue Int512 The smallest representable value: −2⁵¹¹.
MaxValue Int512 The largest representable value: 2⁵¹¹ − 1.
AllBitsSet Int512 The value with every bit set to one (equal to NegativeOne).

Zero

public static Int512 Zero { get; }

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

One

public static Int512 One { get; }

Gets the Int512 value representing positive one.

NegativeOne

public static Int512 NegativeOne { get; }

Gets the Int512 value representing negative one, whose bit pattern is all ones in two's-complement.

MinValue

public static Int512 MinValue { get; }

Gets the smallest possible Int512 value: −2⁵¹¹ (the sign bit set, all other bits clear). Equal to −6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042048.

MaxValue

public static Int512 MaxValue { get; }

Gets the largest possible Int512 value: 2⁵¹¹ − 1 (the sign bit clear, all other bits set). Equal to 6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042047.

AllBitsSet

public static Int512 AllBitsSet { get; }

Gets the Int512 value whose entire 512-bit pattern is ones. This is identical to NegativeOne under two's-complement semantics.

Constructors

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

Int512(UInt256, UInt256)

public Int512(UInt256 upper, UInt256 lower)

Initializes a new instance of the Int512 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. The most-significant bit is the sign bit.
lower UInt256 The lower 256 bits.

Properties

Property Type Description
UpperBits UInt256 Gets the upper 256 bits of this value; the high bit encodes the sign.
LowerBits UInt256 Gets the lower 256 bits of this value.

UpperBits

public UInt256 UpperBits { get; }

Gets the upper 256 bits of the current Int512 value. The most-significant bit of this half is the sign bit: set means the value is negative.

LowerBits

public UInt256 LowerBits { get; }

Gets the lower 256 bits of the current Int512 value.

Methods

Static Methods

Arithmetic

Method Returns Description
Abs(Int512) Int512 Returns the absolute value.
Negate(Int512) Int512 Returns the negation of the value.
Sign(Int512) int Returns −1, 0, or 1 indicating the sign.
CopySign(Int512, Int512) Int512 Copies the sign of one value onto the magnitude of another.
DivRem(Int512, Int512) (Int512 Quotient, Int512 Remainder) Computes the quotient and remainder in one operation.

Comparison and clamping

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

Binary-integer operations

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

Predicates

Method Returns Description
IsZero(Int512) bool Returns true when the value equals zero.
IsNegative(Int512) bool Returns true when the sign bit is set.
IsPositive(Int512) bool Returns true when the sign bit is clear.
IsEvenInteger(Int512) bool Returns true when the value is even.
IsOddInteger(Int512) bool Returns true when the value is odd.
IsPow2(Int512) bool Returns true when the value is a positive power of two.

Parsing

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

Equality

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

Instance Methods

Method Returns Description
CompareTo(Int512) int Compares this instance with another Int512.
CompareTo(object?) int Compares this instance with another object.
Equals(Int512) bool Determines whether this instance is equal to another Int512.
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 in two's-complement.
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(Int512)

public static Int512 Abs(Int512 value)

Returns the absolute value of the specified Int512.

Parameter Type Description
value Int512 The value whose absolute value is to be computed.

Returns — The absolute value of value.

Exceptions

Exception Condition
OverflowException value equals MinValue, whose absolute value cannot be represented as a positive Int512.

Negate(Int512)

public static Int512 Negate(Int512 value)

Returns the arithmetic negation of the specified value. Equivalent to applying the unary - operator.

Parameter Type Description
value Int512 The value to negate.

Returns — The negation of value. Note that negating MinValue wraps to itself under unchecked semantics.

Sign(Int512)

public static int Sign(Int512 value)

Returns an integer indicating the sign of the specified Int512.

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

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

CopySign(Int512, Int512)

public static Int512 CopySign(Int512 value, Int512 sign)

Returns a value with the absolute magnitude of value and the sign of sign.

Parameter Type Description
value Int512 The value whose magnitude is used.
sign Int512 The value whose sign is used.

Returns — A value combining the magnitude of value with the sign of sign.

DivRem(Int512, Int512)

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

Computes both the quotient and the remainder of dividing left by right in a single operation. The remainder has the same sign as the dividend (left), following C# integer division semantics.

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

Returns — A tuple (Quotient, Remainder).

Exceptions

Exception Condition
DivideByZeroException right is zero.
OverflowException left is MinValue and right is NegativeOne.

Compare(Int512, Int512)

public static int Compare(Int512 left, Int512 right)

Compares two Int512 values using signed semantics.

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

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

Min(Int512, Int512)

public static Int512 Min(Int512 x, Int512 y)

Returns the smaller of two Int512 values (signed comparison).

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

Returns — The lesser of x and y.

Max(Int512, Int512)

public static Int512 Max(Int512 x, Int512 y)

Returns the larger of two Int512 values (signed comparison).

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

Returns — The greater of x and y.

Clamp(Int512, Int512, Int512)

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

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

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

public static Int512 MinMagnitude(Int512 x, Int512 y)

Returns the value with the smaller absolute magnitude. When magnitudes are equal, the negative value is preferred.

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

Returns — The value of x or y with the smaller absolute magnitude.

MaxMagnitude(Int512, Int512)

public static Int512 MaxMagnitude(Int512 x, Int512 y)

Returns the value with the larger absolute magnitude. When magnitudes are equal, the positive value is preferred.

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

Returns — The value of x or y with the larger absolute magnitude.

LeadingZeroCount(Int512)

public static Int512 LeadingZeroCount(Int512 value)

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

Parameter Type Description
value Int512 The value to inspect.

Returns — An Int512 in the range [0, 512].

TrailingZeroCount(Int512)

public static Int512 TrailingZeroCount(Int512 value)

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

Parameter Type Description
value Int512 The value to inspect.

Returns — An Int512 in the range [0, 512].

PopCount(Int512)

public static Int512 PopCount(Int512 value)

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

Parameter Type Description
value Int512 The value to inspect.

Returns — An Int512 in the range [0, 512].

Log2(Int512)

public static Int512 Log2(Int512 value)

Returns the truncated base-2 logarithm of value.

Parameter Type Description
value Int512 The value whose logarithm is to be computed. Must be non-negative.

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

Exceptions

Exception Condition
ArgumentOutOfRangeException value is negative.

RotateLeft(Int512, int)

public static Int512 RotateLeft(Int512 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 Int512 The value to rotate.
rotateAmount int The number of bit positions to rotate.

Returns — value rotated left by rotateAmount bits.

RotateRight(Int512, int)

public static Int512 RotateRight(Int512 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 Int512 The value to rotate.
rotateAmount int The number of bit positions to rotate.

Returns — value rotated right by rotateAmount bits.

IsZero(Int512)

public static bool IsZero(Int512 value)

Returns true when value is zero.

IsNegative(Int512)

public static bool IsNegative(Int512 value)

Returns true when the sign bit (bit 511) is set, i.e., when value is strictly negative.

IsPositive(Int512)

public static bool IsPositive(Int512 value)

Returns true when the sign bit is clear, i.e., when value is zero or positive.

IsEvenInteger(Int512)

public static bool IsEvenInteger(Int512 value)

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

IsOddInteger(Int512)

public static bool IsOddInteger(Int512 value)

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

IsPow2(Int512)

public static bool IsPow2(Int512 value)

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

Parse(string, IFormatProvider?)

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

Parses the decimal string representation of an Int512.

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

Returns — The parsed Int512 value.

Exceptions

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

Parse(string, NumberStyles, IFormatProvider?)

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

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

Exceptions

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

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

Parses a span representation of an Int512.

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

Returns — The parsed Int512 value.

Exceptions

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

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

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

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

Exceptions

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

TryParse(string?, out Int512)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

TryParse(string?, IFormatProvider?, out Int512)

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

Attempts to parse a string into an Int512 value using a format provider.

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

Returns — true if parsing succeeded; otherwise, false.

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

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

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

Returns — true if parsing succeeded; otherwise, false.

TryParse(ReadOnlySpan<char>, out Int512)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

Tries to read an Int512 from a big-endian byte span. The span may be shorter than 64 bytes; it will be sign-extended (or zero-extended when isUnsigned is true) to fill the 512-bit representation. A span longer than 64 bytes succeeds only when all the excess bytes are consistent with the sign extension byte.

Parameter Type Description
source ReadOnlySpan<byte> The big-endian byte span to read from.
isUnsigned bool true to treat the most-significant bit of source as a magnitude bit rather than a sign bit.
value out Int512 The parsed value on success; otherwise, default.

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

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

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

Tries to read an Int512 from a little-endian byte span. The same length-flexibility and sign-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 true to treat the most-significant bit of source as a magnitude bit.
value out Int512 The parsed value on success; otherwise, default.

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

Equals(Int512, Int512)

public static bool Equals(Int512 left, Int512 right)

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

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

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


CompareTo(Int512)

public int CompareTo(Int512 other)

Compares this instance with another Int512 value using signed semantics.

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

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

Exceptions

Exception Condition
ArgumentException obj is not null and is not an Int512.

Equals(Int512)

public bool Equals(Int512 other)

Determines whether this instance is equal to another Int512 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 an Int512 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 just the two UInt256 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 64.

GetShortestBitLength()

public int GetShortestBitLength()

Returns the minimum number of bits needed to represent this value in two's-complement form, excluding redundant sign bits. For zero the result is 0; for positive values it equals ⌊log₂(value)⌋ + 1; for negative values it reflects the magnitude of the inverted representation.

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 "-123456789012345678901234567890123456789012345678901234567890".

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 two's-complement). For X format, negative values are rendered as two's-complement hex, consistent with the BCL int/long 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 +(Int512) Int512 Returns the operand unchanged (unary plus).
operator -(Int512) Int512 Returns the two's-complement negation; wraps for MinValue.
operator checked -(Int512) Int512 Returns the negation; throws OverflowException for MinValue.
operator ++(Int512) Int512 Increments by one, wrapping on overflow.
operator checked ++(Int512) Int512 Increments by one; throws OverflowException on overflow.
operator --(Int512) Int512 Decrements by one, wrapping on underflow.
operator checked --(Int512) Int512 Decrements by one; throws OverflowException on underflow.
operator +(Int512, Int512) Int512 Adds two values, wrapping on overflow.
operator checked +(Int512, Int512) Int512 Adds two values; throws OverflowException on overflow.
operator -(Int512, Int512) Int512 Subtracts one value from another, wrapping on overflow.
operator checked -(Int512, Int512) Int512 Subtracts one value from another; throws OverflowException on overflow.
operator *(Int512, Int512) Int512 Multiplies two values, wrapping on overflow.
operator checked *(Int512, Int512) Int512 Multiplies two values; throws OverflowException on overflow.
operator /(Int512, Int512) Int512 Divides, truncating toward zero.
operator %(Int512, Int512) Int512 Returns the remainder (sign follows the dividend).

Bitwise and Shift Operators

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

Comparison Operators

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

Implicit Conversion Operators (to Int512)

Operator Description
implicit operator Int512(sbyte) Widens a signed byte; sign-extends to 512 bits.
implicit operator Int512(byte) Widens an unsigned byte; zero-extends to 512 bits.
implicit operator Int512(short) Widens a 16-bit signed integer; sign-extends.
implicit operator Int512(ushort) Widens a 16-bit unsigned integer; zero-extends.
implicit operator Int512(int) Widens a 32-bit signed integer; sign-extends.
implicit operator Int512(uint) Widens a 32-bit unsigned integer; zero-extends.
implicit operator Int512(long) Widens a 64-bit signed integer; sign-extends.
implicit operator Int512(ulong) Widens a 64-bit unsigned integer; zero-extends.
implicit operator Int512(Int128) Widens a 128-bit signed integer; sign-extends.
implicit operator Int512(UInt128) Widens a 128-bit unsigned integer; zero-extends.
implicit operator Int512(Int256) Widens a 256-bit signed integer; sign-extends.
implicit operator Int512(UInt256) Widens a 256-bit unsigned integer; zero-extends.
implicit operator Int512(char) Widens a char code-point value; zero-extends.
implicit operator BigInteger(Int512) Lossless widening to BigInteger.

Explicit Conversion Operators (from Int512)

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

Explicit Conversion Operators (to Int512)

Operator Description
explicit operator Int512(UInt512) Reinterprets the 512-bit pattern as signed (bit-cast).
explicit operator checked Int512(UInt512) Throws OverflowException if value exceeds MaxValue.
explicit operator UInt512(Int512) Reinterprets the 512-bit pattern as unsigned (bit-cast).
explicit operator checked UInt512(Int512) Throws OverflowException if the value is negative.
explicit operator Int512(BigInteger) Truncates to the low 512 bits (wrapping).
explicit operator checked Int512(BigInteger) Throws OverflowException if outside Int512 range.
explicit operator Int512(Float128) Truncates toward zero; returns Zero for NaN, infinity, or zero.
explicit operator checked Int512(Float128) Throws OverflowException for NaN, infinity, or out-of-range.
explicit operator Int512(Float256) Truncates toward zero; returns Zero for NaN, infinity, or zero.
explicit operator checked Int512(Float256) Throws OverflowException for NaN, infinity, or out-of-range.

operator +(Int512) (unary)

public static Int512 operator +(Int512 value)

Returns the operand unchanged (identity operation).

operator -(Int512) (unary)

public static Int512 operator -(Int512 value)

Returns the two's-complement negation of value. When value is MinValue, the result wraps back to MinValue.

operator checked -(Int512) (unary)

public static Int512 operator checked -(Int512 value)

Returns the negation of value.

Exceptions

Exception Condition
OverflowException value equals MinValue.

operator ++(Int512) (increment)

public static Int512 operator ++(Int512 value)

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

operator checked ++(Int512) (increment)

public static Int512 operator checked ++(Int512 value)

Returns value incremented by one.

Exceptions

Exception Condition
OverflowException value is MaxValue.

operator --(Int512) (decrement)

public static Int512 operator --(Int512 value)

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

operator checked --(Int512) (decrement)

public static Int512 operator checked --(Int512 value)

Returns value decremented by one.

Exceptions

Exception Condition
OverflowException value is MinValue.

operator +(Int512, Int512)

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

Computes the sum of two values. Wraps on overflow.

operator checked +(Int512, Int512)

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

Computes the sum of two values.

Exceptions

Exception Condition
OverflowException The result is outside the range of Int512.

operator -(Int512, Int512)

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

Computes the difference of two values. Wraps on overflow.

operator checked -(Int512, Int512)

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

Computes the difference of two values.

Exceptions

Exception Condition
OverflowException The result is outside the range of Int512.

operator *(Int512, Int512)

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

Computes the product of two values. Wraps on overflow.

operator checked *(Int512, Int512)

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

Computes the product of two values.

Exceptions

Exception Condition
OverflowException The result is outside the range of Int512.

operator /(Int512, Int512)

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

Divides left by right, truncating toward zero.

Exceptions

Exception Condition
DivideByZeroException right is zero.
OverflowException left is MinValue and right is NegativeOne.

operator %(Int512, Int512)

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

Returns the remainder of dividing left by right. The result has the same sign as left (the dividend), following C# integer modulus rules. When left is MinValue and right is NegativeOne, the result is zero.

Exceptions

Exception Condition
DivideByZeroException right is zero.

operator &(Int512, Int512)

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

Returns the bitwise AND of left and right.

operator |(Int512, Int512)

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

Returns the bitwise OR of left and right.

operator ^(Int512, Int512)

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

Returns the bitwise XOR of left and right.

operator ~(Int512) (bitwise complement)

public static Int512 operator ~(Int512 value)

Returns the bitwise complement of value (all bits flipped).

operator <<(Int512, int)

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

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

operator >>(Int512, int)

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

Shifts value right by shiftAmount bit positions, preserving the sign (arithmetic right shift). Vacated high bits are filled with the sign bit. The shift amount is masked to [0, 511].

operator >>>(Int512, int)

public static Int512 operator >>>(Int512 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 ==(Int512, Int512)

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

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

operator !=(Int512, Int512)

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

Returns true when left and right represent different values.

operator <(Int512, Int512)

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

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

operator <=(Int512, Int512)

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

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

operator >(Int512, Int512)

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

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

operator >=(Int512, Int512)

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

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

See Also

ONIXLabs .NET Library


Clone this wiki locally