From 7bf8a89f822fc6f6ea3a2779693134b0d785b4e6 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 25 Apr 2022 00:06:14 +0100 Subject: [PATCH] Add missing IsEven/IsOdd/Sign methods --- X10D/src/Int16Extensions/Int16Extensions.cs | 5 +-- X10D/src/Int32Extensions/Int32Extensions.cs | 38 +-------------------- X10D/src/Math/Int32Extensions.cs | 34 ++++++++++++++++++ X10D/src/Math/UInt16Extensions.cs | 30 ++++++++++++++++ X10D/src/Math/UInt32Extensions.cs | 30 ++++++++++++++++ X10D/src/Math/UInt64Extensions.cs | 30 ++++++++++++++++ 6 files changed, 126 insertions(+), 41 deletions(-) diff --git a/X10D/src/Int16Extensions/Int16Extensions.cs b/X10D/src/Int16Extensions/Int16Extensions.cs index 16e4dab..371c883 100644 --- a/X10D/src/Int16Extensions/Int16Extensions.cs +++ b/X10D/src/Int16Extensions/Int16Extensions.cs @@ -1,7 +1,4 @@ -using System.Diagnostics.Contracts; -using System.Net; -using System.Runtime.CompilerServices; -using X10D.Math; +using System.Net; namespace X10D; diff --git a/X10D/src/Int32Extensions/Int32Extensions.cs b/X10D/src/Int32Extensions/Int32Extensions.cs index 36ef219..41013b2 100644 --- a/X10D/src/Int32Extensions/Int32Extensions.cs +++ b/X10D/src/Int32Extensions/Int32Extensions.cs @@ -1,6 +1,4 @@ -using System.Diagnostics.Contracts; -using System.Net; -using System.Runtime.CompilerServices; +using System.Net; using X10D.Math; namespace X10D; @@ -82,40 +80,6 @@ public static class Int32Extensions return BitConverter.GetBytes(value); } - /// - /// Returns an integer that indicates the sign of this 32-bit signed integer. - /// - /// A signed number. - /// - /// A number that indicates the sign of , as shown in the following table. - /// - /// - /// - /// Return value - /// Meaning - /// - /// - /// - /// -1 - /// is less than zero. - /// - /// - /// 0 - /// is equal to zero. - /// - /// - /// 1 - /// is greater than zero. - /// - /// - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static int Sign(this int value) - { - return System.Math.Sign(value); - } - /// /// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value. /// diff --git a/X10D/src/Math/Int32Extensions.cs b/X10D/src/Math/Int32Extensions.cs index 2e830e7..2a8c8fa 100644 --- a/X10D/src/Math/Int32Extensions.cs +++ b/X10D/src/Math/Int32Extensions.cs @@ -215,4 +215,38 @@ public static class Int32Extensions int r = dividend % divisor; return r < 0 ? r + divisor : r; } + + /// + /// Returns an integer that indicates the sign of this 32-bit signed integer. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this int value) + { + return System.Math.Sign(value); + } } diff --git a/X10D/src/Math/UInt16Extensions.cs b/X10D/src/Math/UInt16Extensions.cs index 1b64593..05e8d54 100644 --- a/X10D/src/Math/UInt16Extensions.cs +++ b/X10D/src/Math/UInt16Extensions.cs @@ -46,4 +46,34 @@ public static class UInt16Extensions return result; } + + /// + /// Returns a value indicating whether the current value is evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsEven(this ushort value) + { + return (value & 1) == 0; + } + + /// + /// Returns a value indicating whether the current value is not evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is not evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsOdd(this ushort value) + { + return !value.IsEven(); + } } diff --git a/X10D/src/Math/UInt32Extensions.cs b/X10D/src/Math/UInt32Extensions.cs index f041474..288cdad 100644 --- a/X10D/src/Math/UInt32Extensions.cs +++ b/X10D/src/Math/UInt32Extensions.cs @@ -46,4 +46,34 @@ public static class UInt32Extensions return result; } + + /// + /// Returns a value indicating whether the current value is evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsEven(this uint value) + { + return (value & 1) == 0; + } + + /// + /// Returns a value indicating whether the current value is not evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is not evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsOdd(this uint value) + { + return !value.IsEven(); + } } diff --git a/X10D/src/Math/UInt64Extensions.cs b/X10D/src/Math/UInt64Extensions.cs index 3f898fa..2753a02 100644 --- a/X10D/src/Math/UInt64Extensions.cs +++ b/X10D/src/Math/UInt64Extensions.cs @@ -46,4 +46,34 @@ public static class UInt64Extensions return result; } + + /// + /// Returns a value indicating whether the current value is evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsEven(this ulong value) + { + return (value & 1) == 0; + } + + /// + /// Returns a value indicating whether the current value is not evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is not evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsOdd(this ulong value) + { + return !value.IsEven(); + } }