Skip to content

OnixLabs.Numerics.Int256

matthew edited this page May 27, 2026 · 2 revisions

Int256 Struct

Represents a 256-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 → Int256
Implements IConvertible, IMinMaxValue<Int256>, IBinaryInteger<Int256>, ISignedNumber<Int256>, IValueEquatable<Int256>, IValueComparable<Int256>

Definition

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

Remarks

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

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

Conversions — widening conversions from all built-in signed and unsigned integer types (including char, Int128, and UInt128) are implicit. Narrowing conversions to smaller integer types, floating-point types (Half, float, double, decimal, Float128, Float256), and the counterpart unsigned type UInt256 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 32-byte representation to a Span<byte>, and TryReadBigEndian / TryReadLittleEndian reconstruct the value from a span of any length, applying sign-extension when shorter than 32 bytes.

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

Examples

Construction and arithmetic

using OnixLabs.Numerics;

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

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

Console.WriteLine(Int256.Sign(sum));     // -1, 0, or 1
Console.WriteLine(Int256.Abs(a));        // 1000000000000000000
Console.WriteLine(Int256.MinValue);      // -57896044618658097711785492504343953926634992332820282019728792003956564819968
Console.WriteLine(Int256.MaxValue);      //  57896044618658097711785492504343953926634992332820282019728792003956564819967

Parsing, formatting, and conversion

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

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

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

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

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

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

Fields and Static Properties

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

Zero

public static Int256 Zero { get; }

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

One

public static Int256 One { get; }

Gets the Int256 value representing positive one.

NegativeOne

public static Int256 NegativeOne { get; }

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

MinValue

public static Int256 MinValue { get; }

Gets the smallest possible Int256 value: −2²⁵⁵ (the sign bit set, all other bits clear). Equal to −57896044618658097711785492504343953926634992332820282019728792003956564819968.

MaxValue

public static Int256 MaxValue { get; }

Gets the largest possible Int256 value: 2²⁵⁵ − 1 (the sign bit clear, all other bits set). Equal to 57896044618658097711785492504343953926634992332820282019728792003956564819967.

AllBitsSet

public static Int256 AllBitsSet { get; }

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

Constructors

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

Int256(UInt128, UInt128)

public Int256(UInt128 upper, UInt128 lower)

Initializes a new instance of the Int256 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. The most-significant bit is the sign bit.
lower UInt128 The lower 128 bits.

Properties

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

UpperBits

public UInt128 UpperBits { get; }

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

LowerBits

public UInt128 LowerBits { get; }

Gets the lower 128 bits of the current Int256 value.

Methods

Static Methods

Arithmetic

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

Comparison and clamping

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

Binary-integer operations

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

Predicates

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

Parsing

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

Equality

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

Instance Methods

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

public static Int256 Abs(Int256 value)

Returns the absolute value of the specified Int256.

Parameter Type Description
value Int256 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 Int256.

Negate(Int256)

public static Int256 Negate(Int256 value)

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

Parameter Type Description
value Int256 The value to negate.

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

Sign(Int256)

public static int Sign(Int256 value)

Returns an integer indicating the sign of the specified Int256.

Parameter Type Description
value Int256 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(Int256, Int256)

public static Int256 CopySign(Int256 value, Int256 sign)

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

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

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

DivRem(Int256, Int256)

public static (Int256 Quotient, Int256 Remainder) DivRem(Int256 left, Int256 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 Int256 The dividend.
right Int256 The divisor.

Returns — A tuple (Quotient, Remainder).

Exceptions

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

Compare(Int256, Int256)

public static int Compare(Int256 left, Int256 right)

Compares two Int256 values using signed semantics.

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

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

Min(Int256, Int256)

public static Int256 Min(Int256 x, Int256 y)

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

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

Returns — The lesser of x and y.

Max(Int256, Int256)

public static Int256 Max(Int256 x, Int256 y)

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

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

Returns — The greater of x and y.

Clamp(Int256, Int256, Int256)

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

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

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

public static Int256 MinMagnitude(Int256 x, Int256 y)

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

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

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

MaxMagnitude(Int256, Int256)

public static Int256 MaxMagnitude(Int256 x, Int256 y)

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

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

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

LeadingZeroCount(Int256)

public static Int256 LeadingZeroCount(Int256 value)

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

Parameter Type Description
value Int256 The value to inspect.

Returns — An Int256 in the range [0, 256].

TrailingZeroCount(Int256)

public static Int256 TrailingZeroCount(Int256 value)

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

Parameter Type Description
value Int256 The value to inspect.

Returns — An Int256 in the range [0, 256].

PopCount(Int256)

public static Int256 PopCount(Int256 value)

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

Parameter Type Description
value Int256 The value to inspect.

Returns — An Int256 in the range [0, 256].

Log2(Int256)

public static Int256 Log2(Int256 value)

Returns the truncated base-2 logarithm of value.

Parameter Type Description
value Int256 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(Int256, int)

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

Returns — value rotated left by rotateAmount bits.

RotateRight(Int256, int)

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

Returns — value rotated right by rotateAmount bits.

IsZero(Int256)

public static bool IsZero(Int256 value)

Returns true when value is zero.

IsNegative(Int256)

public static bool IsNegative(Int256 value)

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

IsPositive(Int256)

public static bool IsPositive(Int256 value)

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

IsEvenInteger(Int256)

public static bool IsEvenInteger(Int256 value)

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

IsOddInteger(Int256)

public static bool IsOddInteger(Int256 value)

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

IsPow2(Int256)

public static bool IsPow2(Int256 value)

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

Parse(string, IFormatProvider?)

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

Parses the decimal string representation of an Int256.

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

Returns — The parsed Int256 value.

Exceptions

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

Parse(string, NumberStyles, IFormatProvider?)

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

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

Exceptions

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

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

Parses a span representation of an Int256.

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

Returns — The parsed Int256 value.

Exceptions

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

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

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

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

Exceptions

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

TryParse(string?, out Int256)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

TryParse(string?, IFormatProvider?, out Int256)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

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

Returns — true if parsing succeeded; otherwise, false.

TryParse(ReadOnlySpan<char>, out Int256)

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

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

Returns — true if parsing succeeded; otherwise, false.

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

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

Tries to read an Int256 from a big-endian byte span. The span may be shorter than 32 bytes; it will be sign-extended (or zero-extended when isUnsigned is true) to fill the 256-bit representation. A span longer than 32 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 Int256 The parsed value on success; otherwise, default.

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

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

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

Tries to read an Int256 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 Int256 The parsed value on success; otherwise, default.

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

Equals(Int256, Int256)

public static bool Equals(Int256 left, Int256 right)

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

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

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


CompareTo(Int256)

public int CompareTo(Int256 other)

Compares this instance with another Int256 value using signed semantics.

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

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

Exceptions

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

Equals(Int256)

public bool Equals(Int256 other)

Determines whether this instance is equal to another Int256 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 Int256 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 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, 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 "-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 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 +(Int256) Int256 Returns the operand unchanged (unary plus).
operator -(Int256) Int256 Returns the two's-complement negation; wraps for MinValue.
operator checked -(Int256) Int256 Returns the negation; throws OverflowException for MinValue.
operator ++(Int256) Int256 Increments by one, wrapping on overflow.
operator checked ++(Int256) Int256 Increments by one; throws OverflowException on overflow.
operator --(Int256) Int256 Decrements by one, wrapping on underflow.
operator checked --(Int256) Int256 Decrements by one; throws OverflowException on underflow.
operator +(Int256, Int256) Int256 Adds two values, wrapping on overflow.
operator checked +(Int256, Int256) Int256 Adds two values; throws OverflowException on overflow.
operator -(Int256, Int256) Int256 Subtracts one value from another, wrapping on overflow.
operator checked -(Int256, Int256) Int256 Subtracts one value from another; throws OverflowException on overflow.
operator *(Int256, Int256) Int256 Multiplies two values, wrapping on overflow.
operator checked *(Int256, Int256) Int256 Multiplies two values; throws OverflowException on overflow.
operator /(Int256, Int256) Int256 Divides, truncating toward zero.
operator %(Int256, Int256) Int256 Returns the remainder (sign follows the dividend).

Bitwise and Shift Operators

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

Comparison Operators

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

Implicit Conversion Operators (to Int256)

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

Explicit Conversion Operators (from Int256)

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

Explicit Conversion Operators (to Int256)

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

operator +(Int256) (unary)

public static Int256 operator +(Int256 value)

Returns the operand unchanged (identity operation).

operator -(Int256) (unary)

public static Int256 operator -(Int256 value)

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

operator checked -(Int256) (unary)

public static Int256 operator checked -(Int256 value)

Returns the negation of value.

Exceptions

Exception Condition
OverflowException value equals MinValue.

operator ++(Int256) (increment)

public static Int256 operator ++(Int256 value)

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

operator checked ++(Int256) (increment)

public static Int256 operator checked ++(Int256 value)

Returns value incremented by one.

Exceptions

Exception Condition
OverflowException value is MaxValue.

operator --(Int256) (decrement)

public static Int256 operator --(Int256 value)

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

operator checked --(Int256) (decrement)

public static Int256 operator checked --(Int256 value)

Returns value decremented by one.

Exceptions

Exception Condition
OverflowException value is MinValue.

operator +(Int256, Int256)

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

Computes the sum of two values. Wraps on overflow.

operator checked +(Int256, Int256)

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

Computes the sum of two values.

Exceptions

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

operator -(Int256, Int256)

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

Computes the difference of two values. Wraps on overflow.

operator checked -(Int256, Int256)

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

Computes the difference of two values.

Exceptions

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

operator *(Int256, Int256)

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

Computes the product of two values. Wraps on overflow.

operator checked *(Int256, Int256)

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

Computes the product of two values.

Exceptions

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

operator /(Int256, Int256)

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

Divides left by right, truncating toward zero.

Exceptions

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

operator %(Int256, Int256)

public static Int256 operator %(Int256 left, Int256 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 &(Int256, Int256)

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

Returns the bitwise AND of left and right.

operator |(Int256, Int256)

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

Returns the bitwise OR of left and right.

operator ^(Int256, Int256)

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

Returns the bitwise XOR of left and right.

operator ~(Int256) (bitwise complement)

public static Int256 operator ~(Int256 value)

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

operator <<(Int256, int)

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

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

operator >>(Int256, int)

public static Int256 operator >>(Int256 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, 255].

operator >>>(Int256, int)

public static Int256 operator >>>(Int256 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, 255].

operator ==(Int256, Int256)

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

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

operator !=(Int256, Int256)

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

Returns true when left and right represent different values.

operator <(Int256, Int256)

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

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

operator <=(Int256, Int256)

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

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

operator >(Int256, Int256)

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

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

operator >=(Int256, Int256)

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

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

See Also

ONIXLabs .NET Library


Clone this wiki locally