Table of Contents

NumericExtensions Class

Definition

Namespace
Bodu.Extensions
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
NumericExtensions.Digits.cs

Provides bit-level and byte-level transformations over the integral primitive types - endian conversion, byte and word swaps, bit reversal and rotation, and unchecked arithmetic helpers - for hashing, serialization, and protocol code.

public static class NumericExtensions
Inheritance
NumericExtensions
Inherited Members

Remarks

Hash algorithms, network protocols, and binary file formats routinely require operations that the BCL either does not expose (bit reversal, word swap), exposes only on a single type ( BinaryPrimitives covers endianness but not rotation), or hides behind generic-math abstractions. This class collects those operations as a single, type-overloaded surface so the same call site can target byte, ushort, uint, or ulong without wrapping the value in a generic helper.

The API surface is grouped into: byte-order conversion (GetBytes, ReverseBytes), within-value rearrangement (ReverseWords, ReverseBits), bit rotation (RotateBitsLeft, RotateBitsRight ), and unchecked arithmetic helpers under Unchecked. ReverseBits additionally accepts a bitLength overload for callers reversing only the low-order portion of a value, and GetBytes accepts a flag to control endianness explicitly rather than depending on host order.

All operations are pure, branch-light, allocation-free for the value-typed overloads, and deterministic. Where a byte[] is returned (for example, GetBytes or ReverseBits(byte[])) a fresh array is allocated each call - callers wanting buffer reuse should slice into a pre-allocated Span<T> via the BCL primitives instead.

// Swap byte order for big-endian wire encoding.
uint host = 0x11_22_33_44u;
uint network = host.ReverseBytes(); // => 0x44_33_22_11

// Bit reversal - useful in CRC and DSP code.
byte mask = 0b1011_0001;
byte mirrored = mask.ReverseBits(); // => 0b1000_1101

// Cyclic bit rotation - common in hashing primitives.
uint rotated = 0x0000_00FFu.RotateBitsLeft(8); // => 0x0000_FF00

Methods

GetBytes<T>(T, bool)

Returns a byte array representing the binary encoding of the specified numeric value, in the requested byte order.

public static byte[] GetBytes<T>(this T value, bool asBigEndian = false) where T : INumberBase<T>

Parameters

value T

The numeric value to convert.

asBigEndian bool

true to return bytes in big-endian order (most significant byte first); false to return bytes in little-endian order (least significant byte first). Defaults to false.

Returns

byte[]

A byte array containing the binary representation of value in the specified byte order.

Type Parameters

T

The numeric type to convert.

Remarks

BitConverter is used to obtain the binary representation of each value in the system's native byte order. The resulting array is reversed when the requested byte order differs from the platform's native endianness.

Supported types are byte, sbyte, short , ushort, int, uint, long, ulong, float, and double.

For float and double, the encoding follows the IEEE 754 standard.

The INumberBase<TSelf> constraint rejects non-numeric types (such as reference types) at compile time. Numeric types that BitConverter does not support - for example decimal, Int128, or BigInteger - still satisfy the constraint and fail at run time with InvalidOperationException.

Exceptions

InvalidOperationException

T is a numeric type outside the fixed set supported by BitConverter.

GreatestCommonDivisor(short, short)

Returns the greatest common divisor of two non-negative short values.

public static short GreatestCommonDivisor(this short value, short other)

Parameters

value short

The first value.

other short

The second value.

Returns

short

The greatest common divisor of value and other.

Exceptions

ArgumentOutOfRangeException

value or other is negative.

GreatestCommonDivisor(short[])

Returns the greatest common divisor of every element in the supplied array of non-negative short values.

public static short GreatestCommonDivisor(this short[] values)

Parameters

values short[]

The values whose common divisor is to be computed.

Returns

short

The greatest common divisor of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

ArgumentOutOfRangeException

At least one element in values is negative.

GreatestCommonDivisor(int, int)

Returns the greatest common divisor of two non-negative int values.

public static int GreatestCommonDivisor(this int value, int other)

Parameters

value int

The first value.

other int

The second value.

Returns

int

The greatest common divisor of value and other.

Exceptions

ArgumentOutOfRangeException

value or other is negative.

GreatestCommonDivisor(int[])

Returns the greatest common divisor of every element in the supplied array of non-negative int values.

public static int GreatestCommonDivisor(this int[] values)

Parameters

values int[]

The values whose common divisor is to be computed.

Returns

int

The greatest common divisor of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

ArgumentOutOfRangeException

At least one element in values is negative.

GreatestCommonDivisor(long, long)

Returns the greatest common divisor of two non-negative long values.

public static long GreatestCommonDivisor(this long value, long other)

Parameters

value long

The first value.

other long

The second value.

Returns

long

The greatest common divisor of value and other.

Exceptions

ArgumentOutOfRangeException

value or other is negative.

GreatestCommonDivisor(long[])

Returns the greatest common divisor of every element in the supplied array of non-negative long values.

public static long GreatestCommonDivisor(this long[] values)

Parameters

values long[]

The values whose common divisor is to be computed.

Returns

long

The greatest common divisor of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

ArgumentOutOfRangeException

At least one element in values is negative.

GreatestCommonDivisor(ushort, ushort)

Returns the greatest common divisor of two ushort values.

public static ushort GreatestCommonDivisor(this ushort value, ushort other)

Parameters

value ushort

The first value.

other ushort

The second value.

Returns

ushort

The greatest common divisor of value and other.

GreatestCommonDivisor(ushort[])

Returns the greatest common divisor of every element in the supplied array of ushort values.

public static ushort GreatestCommonDivisor(this ushort[] values)

Parameters

values ushort[]

The values whose common divisor is to be computed.

Returns

ushort

The greatest common divisor of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

GreatestCommonDivisor(uint, uint)

Returns the greatest common divisor of two uint values.

public static uint GreatestCommonDivisor(this uint value, uint other)

Parameters

value uint

The first value.

other uint

The second value.

Returns

uint

The greatest common divisor of value and other.

GreatestCommonDivisor(uint[])

Returns the greatest common divisor of every element in the supplied array of uint values.

public static uint GreatestCommonDivisor(this uint[] values)

Parameters

values uint[]

The values whose common divisor is to be computed.

Returns

uint

The greatest common divisor of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

GreatestCommonDivisor(ulong, ulong)

Returns the greatest common divisor of two ulong values.

public static ulong GreatestCommonDivisor(this ulong value, ulong other)

Parameters

value ulong

The first value.

other ulong

The second value.

Returns

ulong

The greatest common divisor of value and other.

GreatestCommonDivisor(ulong[])

Returns the greatest common divisor of every element in the supplied array of ulong values.

public static ulong GreatestCommonDivisor(this ulong[] values)

Parameters

values ulong[]

The values whose common divisor is to be computed.

Returns

ulong

The greatest common divisor of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

IsPrime(short)

Returns a value indicating whether the specified short is a prime number.

public static bool IsPrime(this short value)

Parameters

value short

The value to test.

Returns

bool

true when value is greater than 1 and has no positive divisors other than 1 and itself; otherwise false. Returns false for negative values and for 0 and 1.

IsPrime(int)

Returns a value indicating whether the specified int is a prime number.

public static bool IsPrime(this int value)

Parameters

value int

The value to test.

Returns

bool

true when value is greater than 1 and has no positive divisors other than 1 and itself; otherwise false. Returns false for negative values and for 0 and 1.

IsPrime(long)

Returns a value indicating whether the specified long is a prime number.

public static bool IsPrime(this long value)

Parameters

value long

The value to test.

Returns

bool

true when value is greater than 1 and has no positive divisors other than 1 and itself; otherwise false. Returns false for negative values and for 0 and 1.

IsPrime(ushort)

Returns a value indicating whether the specified ushort is a prime number.

public static bool IsPrime(this ushort value)

Parameters

value ushort

The value to test.

Returns

bool

true when value is greater than 1 and has no positive divisors other than 1 and itself; otherwise false.

IsPrime(uint)

Returns a value indicating whether the specified uint is a prime number.

public static bool IsPrime(this uint value)

Parameters

value uint

The value to test.

Returns

bool

true when value is greater than 1 and has no positive divisors other than 1 and itself; otherwise false.

IsPrime(ulong)

Returns a value indicating whether the specified ulong is a prime number.

public static bool IsPrime(this ulong value)

Parameters

value ulong

The value to test.

Returns

bool

true when value is greater than 1 and has no positive divisors other than 1 and itself; otherwise false.

LeastCommonMultiple(short, short)

Returns the least common multiple of two non-negative short values.

public static short LeastCommonMultiple(this short value, short other)

Parameters

value short

The first value.

other short

The second value.

Returns

short

The least common multiple of value and other; 0 when either argument is 0.

Exceptions

ArgumentOutOfRangeException

value or other is negative.

OverflowException

The result does not fit in a short.

LeastCommonMultiple(short[])

Returns the least common multiple of every element in the supplied array of non-negative short values.

public static short LeastCommonMultiple(this short[] values)

Parameters

values short[]

The values whose common multiple is to be computed.

Returns

short

The least common multiple of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

ArgumentOutOfRangeException

At least one element in values is negative.

OverflowException

The accumulating product cannot be represented as a short.

LeastCommonMultiple(int, int)

Returns the least common multiple of two non-negative int values.

public static int LeastCommonMultiple(this int value, int other)

Parameters

value int

The first value.

other int

The second value.

Returns

int

The least common multiple of value and other; 0 when either argument is 0.

Exceptions

ArgumentOutOfRangeException

value or other is negative.

OverflowException

The result does not fit in an int.

LeastCommonMultiple(int[])

Returns the least common multiple of every element in the supplied array of non-negative int values.

public static int LeastCommonMultiple(this int[] values)

Parameters

values int[]

The values whose common multiple is to be computed.

Returns

int

The least common multiple of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

ArgumentOutOfRangeException

At least one element in values is negative.

OverflowException

The accumulating product cannot be represented as an int.

LeastCommonMultiple(long, long)

Returns the least common multiple of two non-negative long values.

public static long LeastCommonMultiple(this long value, long other)

Parameters

value long

The first value.

other long

The second value.

Returns

long

The least common multiple of value and other; 0 when either argument is 0.

Exceptions

ArgumentOutOfRangeException

value or other is negative.

OverflowException

The result does not fit in a long.

LeastCommonMultiple(long[])

Returns the least common multiple of every element in the supplied array of non-negative long values.

public static long LeastCommonMultiple(this long[] values)

Parameters

values long[]

The values whose common multiple is to be computed.

Returns

long

The least common multiple of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

ArgumentOutOfRangeException

At least one element in values is negative.

OverflowException

The accumulating product cannot be represented as a long.

LeastCommonMultiple(ushort, ushort)

Returns the least common multiple of two ushort values.

public static ushort LeastCommonMultiple(this ushort value, ushort other)

Parameters

value ushort

The first value.

other ushort

The second value.

Returns

ushort

The least common multiple of value and other; 0 when either argument is 0.

Exceptions

OverflowException

The result does not fit in a ushort.

LeastCommonMultiple(ushort[])

Returns the least common multiple of every element in the supplied array of ushort values.

public static ushort LeastCommonMultiple(this ushort[] values)

Parameters

values ushort[]

The values whose common multiple is to be computed.

Returns

ushort

The least common multiple of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

OverflowException

The accumulating product cannot be represented as a ushort.

LeastCommonMultiple(uint, uint)

Returns the least common multiple of two uint values.

public static uint LeastCommonMultiple(this uint value, uint other)

Parameters

value uint

The first value.

other uint

The second value.

Returns

uint

The least common multiple of value and other; 0 when either argument is 0.

Exceptions

OverflowException

The result does not fit in a uint.

LeastCommonMultiple(uint[])

Returns the least common multiple of every element in the supplied array of uint values.

public static uint LeastCommonMultiple(this uint[] values)

Parameters

values uint[]

The values whose common multiple is to be computed.

Returns

uint

The least common multiple of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

OverflowException

The accumulating product cannot be represented as a uint.

LeastCommonMultiple(ulong, ulong)

Returns the least common multiple of two ulong values.

public static ulong LeastCommonMultiple(this ulong value, ulong other)

Parameters

value ulong

The first value.

other ulong

The second value.

Returns

ulong

The least common multiple of value and other; 0 when either argument is 0.

Exceptions

OverflowException

The intermediate product exceeds the range of ulong.

LeastCommonMultiple(ulong[])

Returns the least common multiple of every element in the supplied array of ulong values.

public static ulong LeastCommonMultiple(this ulong[] values)

Parameters

values ulong[]

The values whose common multiple is to be computed.

Returns

ulong

The least common multiple of every element in values.

Exceptions

ArgumentNullException

values is null.

ArgumentException

values is empty.

OverflowException

The accumulating product exceeds the range of ulong.

ReverseBits(byte)

Reverses the order of all bits in the specified byte value.

public static byte ReverseBits(this byte value)

Parameters

value byte

The 8-bit unsigned value whose bits are to be reversed.

Returns

byte

A byte whose bits appear in the reverse order of those in value.

ReverseBits(byte, int)

Reverses the order of the least significant bitLength bits in the specified byte value.

public static byte ReverseBits(this byte value, int bitLength)

Parameters

value byte

The 8-bit unsigned value to reflect.

bitLength int

The number of least significant bits to reverse. Must be in the range [0, 8]. Bits above the reflected window are discarded.

Returns

byte

A byte whose least significant bitLength bits contain the bit-reversed window of value, with all higher bits set to zero.

Exceptions

ArgumentOutOfRangeException

bitLength is less than 0 or greater than 8.

ReverseBits(byte[])

Reverses the bits within each byte of the specified byte array and returns a new array.

public static byte[] ReverseBits(this byte[] bytes)

Parameters

bytes byte[]

The byte array to process. Must not be null.

Returns

byte[]

A new byte array in which the bits of each element have been reversed. The original array is not modified.

Remarks

Each byte is reversed independently using ReverseBits(byte). The relative order of bytes within the array is preserved.

Exceptions

ArgumentNullException

bytes is null.

ReverseBits(ushort)

Reverses the order of all bits in the specified ushort value.

public static ushort ReverseBits(this ushort value)

Parameters

value ushort

The 16-bit unsigned integer whose bits are to be reversed.

Returns

ushort

A ushort whose bits appear in the reverse order of those in value.

ReverseBits(ushort, int)

Reverses the order of the least significant bitLength bits in the specified ushort value.

public static ushort ReverseBits(this ushort value, int bitLength)

Parameters

value ushort

The 16-bit unsigned integer to reflect.

bitLength int

The number of least significant bits to reverse. Must be in the range [0, 16]. Bits above the reflected window are discarded.

Returns

ushort

A ushort whose least significant bitLength bits contain the bit-reversed window of value, with all higher bits set to zero.

Exceptions

ArgumentOutOfRangeException

bitLength is less than 0 or greater than 16.

ReverseBits(uint)

Reverses the order of all bits in the specified uint value.

public static uint ReverseBits(this uint value)

Parameters

value uint

The 32-bit unsigned integer whose bits are to be reversed.

Returns

uint

A uint whose bits appear in the reverse order of those in value.

ReverseBits(uint, int)

Reverses the order of the least significant bitLength bits in the specified uint value.

public static uint ReverseBits(this uint value, int bitLength)

Parameters

value uint

The 32-bit unsigned integer to reflect.

bitLength int

The number of least significant bits to reverse. Must be in the range [0, 32]. Bits above the reflected window are discarded.

Returns

uint

A uint whose least significant bitLength bits contain the bit-reversed window of value, with all higher bits set to zero.

Exceptions

ArgumentOutOfRangeException

bitLength is less than 0 or greater than 32.

ReverseBits(ulong)

Reverses the order of all bits in the specified ulong value.

public static ulong ReverseBits(this ulong value)

Parameters

value ulong

The 64-bit unsigned integer whose bits are to be reversed.

Returns

ulong

A ulong whose bits appear in the reverse order of those in value.

ReverseBits(ulong, int)

Reverses the order of the least significant bitLength bits in the specified ulong value.

public static ulong ReverseBits(this ulong value, int bitLength)

Parameters

value ulong

The 64-bit unsigned integer to reflect.

bitLength int

The number of least significant bits to reverse. Must be in the range [0, 64]. Bits above the reflected window are discarded.

Returns

ulong

A ulong whose least significant bitLength bits contain the bit-reversed window of value, with all higher bits set to zero.

Remarks

Commonly used by CRC and other checksum primitives to implement the reflect-in / reflect-out transformations where bitLength matches the hash width.

Exceptions

ArgumentOutOfRangeException

bitLength is less than 0 or greater than 64.

ReverseBytes(ushort)

Reverses the byte order of the specified ushort value.

public static ushort ReverseBytes(this ushort value)

Parameters

value ushort

The 16-bit unsigned integer whose byte order is to be reversed.

Returns

ushort

A ushort whose bytes are in the reverse order of value, converting between big-endian and little-endian representations.

Remarks

Delegates to ReverseEndianness(ushort), allowing the JIT to lower the operation to a single byte-swap instruction on supported platforms.

ReverseBytes(uint)

Reverses the byte order of the specified uint value.

public static uint ReverseBytes(this uint value)

Parameters

value uint

The 32-bit unsigned integer whose byte order is to be reversed.

Returns

uint

A uint whose bytes are in the reverse order of value, converting between big-endian and little-endian representations.

Remarks

Delegates to ReverseEndianness(uint), allowing the JIT to lower the operation to a single byte-swap instruction on supported platforms.

ReverseBytes(ulong)

Reverses the byte order of the specified ulong value.

public static ulong ReverseBytes(this ulong value)

Parameters

value ulong

The 64-bit unsigned integer whose byte order is to be reversed.

Returns

ulong

A ulong whose bytes are in the reverse order of value, converting between big-endian and little-endian representations.

Remarks

Delegates to ReverseEndianness(ulong), allowing the JIT to lower the operation to a single byte-swap instruction on supported platforms.

ReverseDigits(ushort)

Reverses the decimal digits of a ushort value.

public static ushort ReverseDigits(this ushort value)

Parameters

value ushort

The 16-bit unsigned integer whose digits are to be reversed.

Returns

ushort

A ushort whose decimal digits appear in the reverse order of those in value. Leading zeros that emerge in the reversed value are not preserved.

ReverseDigits(uint)

Reverses the decimal digits of a uint value.

public static uint ReverseDigits(this uint value)

Parameters

value uint

The 32-bit unsigned integer whose digits are to be reversed.

Returns

uint

A uint whose decimal digits appear in the reverse order of those in value. Leading zeros that emerge in the reversed value are not preserved.

ReverseDigits(ulong)

Reverses the decimal digits of a ulong value.

public static ulong ReverseDigits(this ulong value)

Parameters

value ulong

The 64-bit unsigned integer whose digits are to be reversed.

Returns

ulong

A ulong whose decimal digits appear in the reverse order of those in value. Leading zeros that emerge in the reversed value are not preserved.

ReverseWords(byte[])

Swaps the bytes within each 16-bit word of the specified byte array and returns a new array.

public static byte[] ReverseWords(this byte[] bytes)

Parameters

bytes byte[]

The byte array to process. Must not be null.

Returns

byte[]

A new byte array in which each pair of adjacent bytes has been exchanged. The original array is not modified.

Remarks

The array is processed in consecutive pairs. Each even-indexed byte is exchanged with the byte immediately following it, leaving all other bytes in place.

Exceptions

ArgumentNullException

bytes is null.

ArgumentException

The length of bytes is not a positive multiple of 2.

ReverseWords(Span<byte>)

Swaps the bytes within each 16-bit word of the specified Span<T>, modifying it in place.

public static void ReverseWords(this Span<byte> bytes)

Parameters

bytes Span<byte>

The byte span to process in place.

Remarks

The span is processed in consecutive pairs. Each even-indexed byte is exchanged with the byte immediately following it.

Exceptions

ArgumentException

The length of bytes is not a positive multiple of 2.

ReverseWords(ushort)

Swaps the two bytes of the specified ushort value.

public static ushort ReverseWords(this ushort value)

Parameters

value ushort

The 16-bit unsigned integer whose bytes are to be swapped.

Returns

ushort

A ushort whose high and low bytes are exchanged relative to value.

Remarks

Because a ushort comprises a single 16-bit word, this operation is equivalent to ReverseBytes(ushort).

ReverseWords(uint)

Swaps the bytes within each 16-bit word of the specified uint value.

public static uint ReverseWords(this uint value)

Parameters

value uint

The 32-bit unsigned integer whose per-word bytes are to be swapped.

Returns

uint

A uint in which the two bytes of each 16-bit half have been exchanged. For example, 0xAABBCCDD becomes 0xBBAADDCC.

Remarks

This method treats the value as two consecutive 16-bit words and reverses the byte order within each word independently. It differs from ReverseBytes(uint), which reverses the byte order of the entire 32-bit value.

ReverseWords(ulong)

Swaps the bytes within each 16-bit word of the specified ulong value.

public static ulong ReverseWords(this ulong value)

Parameters

value ulong

The 64-bit unsigned integer whose per-word bytes are to be swapped.

Returns

ulong

A ulong in which the two bytes of each 16-bit quarter have been exchanged. For example, 0xAABBCCDD11223344 becomes 0xBBAADDCC22114433.

Remarks

This method treats the value as four consecutive 16-bit words and reverses the byte order within each word independently. It differs from ReverseBytes(ulong), which reverses the byte order of the entire 64-bit value.

RotateBitsLeft(byte, int)

Rotates the bits of the specified byte value to the left by the specified number of positions.

public static byte RotateBitsLeft(this byte value, int count)

Parameters

value byte

The 8-bit unsigned value whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 8]. A count of 0 or 8 is a no-op.

Returns

byte

A byte with the bits of value rotated left by count positions.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 8.

RotateBitsLeft(ushort, int)

Rotates the bits of the specified ushort value to the left by the specified number of positions.

public static ushort RotateBitsLeft(this ushort value, int count)

Parameters

value ushort

The 16-bit unsigned integer whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 16]. A count of 0 or 16 is a no-op.

Returns

ushort

A ushort with the bits of value rotated left by count positions.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 16.

RotateBitsLeft(uint, int)

Rotates the bits of the specified uint value to the left by the specified number of positions.

public static uint RotateBitsLeft(this uint value, int count)

Parameters

value uint

The 32-bit unsigned integer whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 32]. A count of 0 or 32 is a no-op.

Returns

uint

A uint with the bits of value rotated left by count positions.

Remarks

Delegates to RotateLeft(uint, int) after validation, allowing the JIT to lower the rotation to a single CPU instruction on supported platforms.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 32.

RotateBitsLeft(ulong, int)

Rotates the bits of the specified ulong value to the left by the specified number of positions.

public static ulong RotateBitsLeft(this ulong value, int count)

Parameters

value ulong

The 64-bit unsigned integer whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 64]. A count of 0 or 64 is a no-op.

Returns

ulong

A ulong with the bits of value rotated left by count positions.

Remarks

Delegates to RotateLeft(ulong, int) after validation, allowing the JIT to lower the rotation to a single CPU instruction on supported platforms.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 64.

RotateBitsRight(byte, int)

Rotates the bits of the specified byte value to the right by the specified number of positions.

public static byte RotateBitsRight(this byte value, int count)

Parameters

value byte

The 8-bit unsigned value whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 8]. A count of 0 or 8 is a no-op.

Returns

byte

A byte with the bits of value rotated right by count positions.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 8.

RotateBitsRight(ushort, int)

Rotates the bits of the specified ushort value to the right by the specified number of positions.

public static ushort RotateBitsRight(this ushort value, int count)

Parameters

value ushort

The 16-bit unsigned integer whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 16]. A count of 0 or 16 is a no-op.

Returns

ushort

A ushort with the bits of value rotated right by count positions.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 16.

RotateBitsRight(uint, int)

Rotates the bits of the specified uint value to the right by the specified number of positions.

public static uint RotateBitsRight(this uint value, int count)

Parameters

value uint

The 32-bit unsigned integer whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 32]. A count of 0 or 32 is a no-op.

Returns

uint

A uint with the bits of value rotated right by count positions.

Remarks

Delegates to RotateRight(uint, int) after validation, allowing the JIT to lower the rotation to a single CPU instruction on supported platforms.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 32.

RotateBitsRight(ulong, int)

Rotates the bits of the specified ulong value to the right by the specified number of positions.

public static ulong RotateBitsRight(this ulong value, int count)

Parameters

value ulong

The 64-bit unsigned integer whose bits are to be rotated.

count int

The number of positions to rotate. Must be in the range [0, 64]. A count of 0 or 64 is a no-op.

Returns

ulong

A ulong with the bits of value rotated right by count positions.

Remarks

Delegates to RotateRight(ulong, int) after validation, allowing the JIT to lower the rotation to a single CPU instruction on supported platforms.

Exceptions

ArgumentOutOfRangeException

count is less than 0 or greater than 64.

RotateDigitsLeft(ushort)

Rotates the decimal digits of a ushort value one place to the left.

public static ushort RotateDigitsLeft(this ushort value)

Parameters

value ushort

The value whose digits are to be rotated.

Returns

ushort

The rotated value.

RotateDigitsLeft(ushort, int)

Rotates the decimal digits of a ushort value the specified number of places to the left.

public static ushort RotateDigitsLeft(this ushort value, int count)

Parameters

value ushort

The value whose digits are to be rotated.

count int

The non-negative number of positions to rotate.

Returns

ushort

The rotated value.

Exceptions

ArgumentOutOfRangeException

count is negative.

RotateDigitsLeft(uint)

Rotates the decimal digits of a uint value one place to the left.

public static uint RotateDigitsLeft(this uint value)

Parameters

value uint

The value whose digits are to be rotated.

Returns

uint

The rotated value.

RotateDigitsLeft(uint, int)

Rotates the decimal digits of a uint value the specified number of places to the left.

public static uint RotateDigitsLeft(this uint value, int count)

Parameters

value uint

The value whose digits are to be rotated.

count int

The non-negative number of positions to rotate.

Returns

uint

The rotated value.

Exceptions

ArgumentOutOfRangeException

count is negative.

RotateDigitsLeft(ulong)

Rotates the decimal digits of a ulong value one place to the left.

public static ulong RotateDigitsLeft(this ulong value)

Parameters

value ulong

The value whose digits are to be rotated.

Returns

ulong

The rotated value.

RotateDigitsLeft(ulong, int)

Rotates the decimal digits of a ulong value the specified number of places to the left.

public static ulong RotateDigitsLeft(this ulong value, int count)

Parameters

value ulong

The value whose digits are to be rotated.

count int

The non-negative number of positions to rotate.

Returns

ulong

The rotated value.

Exceptions

ArgumentOutOfRangeException

count is negative.

RotateDigitsRight(ushort)

Rotates the decimal digits of a ushort value one place to the right.

public static ushort RotateDigitsRight(this ushort value)

Parameters

value ushort

The value whose digits are to be rotated.

Returns

ushort

The rotated value.

RotateDigitsRight(ushort, int)

Rotates the decimal digits of a ushort value the specified number of places to the right.

public static ushort RotateDigitsRight(this ushort value, int count)

Parameters

value ushort

The value whose digits are to be rotated.

count int

The non-negative number of positions to rotate.

Returns

ushort

The rotated value.

Exceptions

ArgumentOutOfRangeException

count is negative.

RotateDigitsRight(uint)

Rotates the decimal digits of a uint value one place to the right.

public static uint RotateDigitsRight(this uint value)

Parameters

value uint

The value whose digits are to be rotated.

Returns

uint

The rotated value.

RotateDigitsRight(uint, int)

Rotates the decimal digits of a uint value the specified number of places to the right.

public static uint RotateDigitsRight(this uint value, int count)

Parameters

value uint

The value whose digits are to be rotated.

count int

The non-negative number of positions to rotate.

Returns

uint

The rotated value.

Exceptions

ArgumentOutOfRangeException

count is negative.

RotateDigitsRight(ulong)

Rotates the decimal digits of a ulong value one place to the right.

public static ulong RotateDigitsRight(this ulong value)

Parameters

value ulong

The value whose digits are to be rotated.

Returns

ulong

The rotated value.

RotateDigitsRight(ulong, int)

Rotates the decimal digits of a ulong value the specified number of places to the right.

public static ulong RotateDigitsRight(this ulong value, int count)

Parameters

value ulong

The value whose digits are to be rotated.

count int

The non-negative number of positions to rotate.

Returns

ulong

The rotated value.

Exceptions

ArgumentOutOfRangeException

count is negative.

RoundToSignificantDigits(decimal, int)

Rounds a decimal to the specified number of significant decimal digits.

public static decimal RoundToSignificantDigits(this decimal value, int digits)

Parameters

value decimal

The value to round.

digits int

The number of significant digits to retain. Must be between 1 and 28, inclusive (the precision limit of decimal).

Returns

decimal

value rounded to digits significant digits. Returns value unchanged when it is zero.

Remarks

The computation is performed entirely in decimal arithmetic so the full 28-digit precision is preserved; no intermediate double conversion occurs.

Exceptions

ArgumentOutOfRangeException

digits is less than 1 or greater than 28.

OverflowException

Rounding away from zero pushes the result beyond MaxValue or MinValue (possible only for values within one rounding step of the range limit).

RoundToSignificantDigits(double, int)

Rounds a double to the specified number of significant decimal digits.

public static double RoundToSignificantDigits(this double value, int digits)

Parameters

value double

The value to round.

digits int

The number of significant digits to retain. Must be between 1 and 15, inclusive (the practical precision limit of double).

Returns

double

value rounded to digits significant digits. Returns value unchanged when it is zero, NaN, or infinite.

Examples

12345.6789.RoundToSignificantDigits(3); // => 12300
0.0012345.RoundToSignificantDigits(2); // => 0.0012

Remarks

Unlike Round(double, int), which rounds to a fixed number of fractional digits, this method scales the rounding magnitude to the order of magnitude of value. Negative values are handled symmetrically.

Exceptions

ArgumentOutOfRangeException

digits is less than 1 or greater than 15.

ToDigitArray(ushort)

Returns the decimal digits of a ushort value in most-significant-first order.

public static byte[] ToDigitArray(this ushort value)

Parameters

value ushort

The value whose digits are to be enumerated.

Returns

byte[]

A new array containing the digits of value; for 0 the result is a single-element array containing 0.

ToDigitArray(uint)

Returns the decimal digits of a uint value in most-significant-first order.

public static byte[] ToDigitArray(this uint value)

Parameters

value uint

The value whose digits are to be enumerated.

Returns

byte[]

A new array containing the digits of value; for 0 the result is a single-element array containing 0.

ToDigitArray(ulong)

Returns the decimal digits of a ulong value in most-significant-first order.

public static byte[] ToDigitArray(this ulong value)

Parameters

value ulong

The value whose digits are to be enumerated.

Returns

byte[]

A new array containing the digits of value; for 0 the result is a single-element array containing 0.

Applies to

ProductVersions
.NET8, 10