NumericExtensions Class
Definition
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
valueTThe numeric value to convert.
asBigEndianbooltrue 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
valuein the specified byte order.
Type Parameters
TThe 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
Tis 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
Returns
- short
The greatest common divisor of
valueandother.
Exceptions
- ArgumentOutOfRangeException
valueorotheris 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
valuesshort[]The values whose common divisor is to be computed.
Returns
- short
The greatest common divisor of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.- ArgumentOutOfRangeException
At least one element in
valuesis 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
Returns
- int
The greatest common divisor of
valueandother.
Exceptions
- ArgumentOutOfRangeException
valueorotheris 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
valuesint[]The values whose common divisor is to be computed.
Returns
- int
The greatest common divisor of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.- ArgumentOutOfRangeException
At least one element in
valuesis 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
Returns
- long
The greatest common divisor of
valueandother.
Exceptions
- ArgumentOutOfRangeException
valueorotheris 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
valueslong[]The values whose common divisor is to be computed.
Returns
- long
The greatest common divisor of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.- ArgumentOutOfRangeException
At least one element in
valuesis negative.
GreatestCommonDivisor(ushort, ushort)
Returns the greatest common divisor of two ushort values.
public static ushort GreatestCommonDivisor(this ushort value, ushort other)
Parameters
Returns
- ushort
The greatest common divisor of
valueandother.
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
valuesushort[]The values whose common divisor is to be computed.
Returns
- ushort
The greatest common divisor of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.
GreatestCommonDivisor(uint, uint)
Returns the greatest common divisor of two uint values.
public static uint GreatestCommonDivisor(this uint value, uint other)
Parameters
Returns
- uint
The greatest common divisor of
valueandother.
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
valuesuint[]The values whose common divisor is to be computed.
Returns
- uint
The greatest common divisor of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.
GreatestCommonDivisor(ulong, ulong)
Returns the greatest common divisor of two ulong values.
public static ulong GreatestCommonDivisor(this ulong value, ulong other)
Parameters
Returns
- ulong
The greatest common divisor of
valueandother.
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
valuesulong[]The values whose common divisor is to be computed.
Returns
- ulong
The greatest common divisor of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.
IsPrime(short)
Returns a value indicating whether the specified short is a prime number.
public static bool IsPrime(this short value)
Parameters
valueshortThe value to test.
Returns
IsPrime(int)
Returns a value indicating whether the specified int is a prime number.
public static bool IsPrime(this int value)
Parameters
valueintThe value to test.
Returns
IsPrime(long)
Returns a value indicating whether the specified long is a prime number.
public static bool IsPrime(this long value)
Parameters
valuelongThe value to test.
Returns
IsPrime(ushort)
Returns a value indicating whether the specified ushort is a prime number.
public static bool IsPrime(this ushort value)
Parameters
valueushortThe value to test.
Returns
IsPrime(uint)
Returns a value indicating whether the specified uint is a prime number.
public static bool IsPrime(this uint value)
Parameters
valueuintThe value to test.
Returns
IsPrime(ulong)
Returns a value indicating whether the specified ulong is a prime number.
public static bool IsPrime(this ulong value)
Parameters
valueulongThe value to test.
Returns
LeastCommonMultiple(short, short)
Returns the least common multiple of two non-negative short values.
public static short LeastCommonMultiple(this short value, short other)
Parameters
Returns
- short
The least common multiple of
valueandother;0when either argument is0.
Exceptions
- ArgumentOutOfRangeException
valueorotheris 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
valuesshort[]The values whose common multiple is to be computed.
Returns
- short
The least common multiple of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.- ArgumentOutOfRangeException
At least one element in
valuesis 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
Returns
- int
The least common multiple of
valueandother;0when either argument is0.
Exceptions
- ArgumentOutOfRangeException
valueorotheris 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
valuesint[]The values whose common multiple is to be computed.
Returns
- int
The least common multiple of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.- ArgumentOutOfRangeException
At least one element in
valuesis 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
Returns
- long
The least common multiple of
valueandother;0when either argument is0.
Exceptions
- ArgumentOutOfRangeException
valueorotheris 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
valueslong[]The values whose common multiple is to be computed.
Returns
- long
The least common multiple of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis empty.- ArgumentOutOfRangeException
At least one element in
valuesis 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
Returns
- ushort
The least common multiple of
valueandother;0when either argument is0.
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
valuesushort[]The values whose common multiple is to be computed.
Returns
- ushort
The least common multiple of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis 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
Returns
- uint
The least common multiple of
valueandother;0when either argument is0.
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
valuesuint[]The values whose common multiple is to be computed.
Returns
- uint
The least common multiple of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis 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
Returns
- ulong
The least common multiple of
valueandother;0when either argument is0.
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
valuesulong[]The values whose common multiple is to be computed.
Returns
- ulong
The least common multiple of every element in
values.
Exceptions
- ArgumentNullException
valuesis null.- ArgumentException
valuesis 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
valuebyteThe 8-bit unsigned value whose bits are to be reversed.
Returns
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
valuebyteThe 8-bit unsigned value to reflect.
bitLengthintThe 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
bitLengthbits contain the bit-reversed window ofvalue, with all higher bits set to zero.
Exceptions
- ArgumentOutOfRangeException
bitLengthis 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
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
bytesis null.
ReverseBits(ushort)
Reverses the order of all bits in the specified ushort value.
public static ushort ReverseBits(this ushort value)
Parameters
valueushortThe 16-bit unsigned integer whose bits are to be reversed.
Returns
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
valueushortThe 16-bit unsigned integer to reflect.
bitLengthintThe 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
bitLengthbits contain the bit-reversed window ofvalue, with all higher bits set to zero.
Exceptions
- ArgumentOutOfRangeException
bitLengthis 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
valueuintThe 32-bit unsigned integer whose bits are to be reversed.
Returns
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
valueuintThe 32-bit unsigned integer to reflect.
bitLengthintThe 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
bitLengthbits contain the bit-reversed window ofvalue, with all higher bits set to zero.
Exceptions
- ArgumentOutOfRangeException
bitLengthis 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
valueulongThe 64-bit unsigned integer whose bits are to be reversed.
Returns
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
valueulongThe 64-bit unsigned integer to reflect.
bitLengthintThe 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
bitLengthbits contain the bit-reversed window ofvalue, 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
bitLengthis 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
valueushortThe 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
valueuintThe 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
valueulongThe 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
valueushortThe 16-bit unsigned integer whose digits are to be reversed.
Returns
ReverseDigits(uint)
Reverses the decimal digits of a uint value.
public static uint ReverseDigits(this uint value)
Parameters
valueuintThe 32-bit unsigned integer whose digits are to be reversed.
Returns
ReverseDigits(ulong)
Reverses the decimal digits of a ulong value.
public static ulong ReverseDigits(this ulong value)
Parameters
valueulongThe 64-bit unsigned integer whose digits are to be reversed.
Returns
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
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
bytesis null.- ArgumentException
The length of
bytesis 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
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
bytesis 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
valueushortThe 16-bit unsigned integer whose bytes are to be swapped.
Returns
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
valueuintThe 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,
0xAABBCCDDbecomes0xBBAADDCC.
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
valueulongThe 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,
0xAABBCCDD11223344becomes0xBBAADDCC22114433.
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
valuebyteThe 8-bit unsigned value whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 8]. A count of 0 or 8 is a no-op.
Returns
Exceptions
- ArgumentOutOfRangeException
countis 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
valueushortThe 16-bit unsigned integer whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 16]. A count of 0 or 16 is a no-op.
Returns
Exceptions
- ArgumentOutOfRangeException
countis 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
valueuintThe 32-bit unsigned integer whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 32]. A count of 0 or 32 is a no-op.
Returns
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
countis 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
valueulongThe 64-bit unsigned integer whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 64]. A count of 0 or 64 is a no-op.
Returns
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
countis 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
valuebyteThe 8-bit unsigned value whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 8]. A count of 0 or 8 is a no-op.
Returns
Exceptions
- ArgumentOutOfRangeException
countis 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
valueushortThe 16-bit unsigned integer whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 16]. A count of 0 or 16 is a no-op.
Returns
Exceptions
- ArgumentOutOfRangeException
countis 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
valueuintThe 32-bit unsigned integer whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 32]. A count of 0 or 32 is a no-op.
Returns
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
countis 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
valueulongThe 64-bit unsigned integer whose bits are to be rotated.
countintThe number of positions to rotate. Must be in the range [0, 64]. A count of 0 or 64 is a no-op.
Returns
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
countis 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
valueushortThe 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
valueushortThe value whose digits are to be rotated.
countintThe non-negative number of positions to rotate.
Returns
- ushort
The rotated value.
Exceptions
- ArgumentOutOfRangeException
countis negative.
RotateDigitsLeft(uint)
Rotates the decimal digits of a uint value one place to the left.
public static uint RotateDigitsLeft(this uint value)
Parameters
valueuintThe 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
valueuintThe value whose digits are to be rotated.
countintThe non-negative number of positions to rotate.
Returns
- uint
The rotated value.
Exceptions
- ArgumentOutOfRangeException
countis negative.
RotateDigitsLeft(ulong)
Rotates the decimal digits of a ulong value one place to the left.
public static ulong RotateDigitsLeft(this ulong value)
Parameters
valueulongThe 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
valueulongThe value whose digits are to be rotated.
countintThe non-negative number of positions to rotate.
Returns
- ulong
The rotated value.
Exceptions
- ArgumentOutOfRangeException
countis negative.
RotateDigitsRight(ushort)
Rotates the decimal digits of a ushort value one place to the right.
public static ushort RotateDigitsRight(this ushort value)
Parameters
valueushortThe 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
valueushortThe value whose digits are to be rotated.
countintThe non-negative number of positions to rotate.
Returns
- ushort
The rotated value.
Exceptions
- ArgumentOutOfRangeException
countis negative.
RotateDigitsRight(uint)
Rotates the decimal digits of a uint value one place to the right.
public static uint RotateDigitsRight(this uint value)
Parameters
valueuintThe 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
valueuintThe value whose digits are to be rotated.
countintThe non-negative number of positions to rotate.
Returns
- uint
The rotated value.
Exceptions
- ArgumentOutOfRangeException
countis negative.
RotateDigitsRight(ulong)
Rotates the decimal digits of a ulong value one place to the right.
public static ulong RotateDigitsRight(this ulong value)
Parameters
valueulongThe 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
valueulongThe value whose digits are to be rotated.
countintThe non-negative number of positions to rotate.
Returns
- ulong
The rotated value.
Exceptions
- ArgumentOutOfRangeException
countis 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
valuedecimalThe value to round.
digitsintThe number of significant digits to retain. Must be between
1and28, inclusive (the precision limit of decimal).
Returns
- decimal
valuerounded todigitssignificant digits. Returnsvalueunchanged 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
digitsis less than1or greater than28.- 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
valuedoubleThe value to round.
digitsintThe number of significant digits to retain. Must be between
1and15, inclusive (the practical precision limit of double).
Returns
- double
valuerounded todigitssignificant digits. Returnsvalueunchanged 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
digitsis less than1or greater than15.
ToDigitArray(ushort)
Returns the decimal digits of a ushort value in most-significant-first order.
public static byte[] ToDigitArray(this ushort value)
Parameters
valueushortThe value whose digits are to be enumerated.
Returns
- byte[]
A new array containing the digits of
value; for0the result is a single-element array containing0.
ToDigitArray(uint)
Returns the decimal digits of a uint value in most-significant-first order.
public static byte[] ToDigitArray(this uint value)
Parameters
valueuintThe value whose digits are to be enumerated.
Returns
- byte[]
A new array containing the digits of
value; for0the result is a single-element array containing0.
ToDigitArray(ulong)
Returns the decimal digits of a ulong value in most-significant-first order.
public static byte[] ToDigitArray(this ulong value)
Parameters
valueulongThe value whose digits are to be enumerated.
Returns
- byte[]
A new array containing the digits of
value; for0the result is a single-element array containing0.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |