From 9e193e7142587e7838a4548017865b75e8af6369 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 1 Apr 2021 13:34:30 +0100 Subject: [PATCH] Add ByteExtensions and SByteExtensions --- X10D/src/ByteExtensions/ByteExtensions.cs | 146 +++++++++++++++++++++ X10D/src/ByteExtensions/SByteExtensions.cs | 116 ++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 X10D/src/ByteExtensions/ByteExtensions.cs create mode 100644 X10D/src/ByteExtensions/SByteExtensions.cs diff --git a/X10D/src/ByteExtensions/ByteExtensions.cs b/X10D/src/ByteExtensions/ByteExtensions.cs new file mode 100644 index 0000000..54cc154 --- /dev/null +++ b/X10D/src/ByteExtensions/ByteExtensions.cs @@ -0,0 +1,146 @@ +namespace X10D.ByteExtensions +{ + /// + /// Extension methods for . + /// + public static class ByteExtensions + { + /// + /// Returns the current 8-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 1. + public static byte[] GetBytes(this byte value) + { + return new[] { value }; + } + + /// + /// 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. + /// + public static bool IsEven(this byte value) + { + return value % 2 == 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. + /// + public static bool IsOdd(this byte value) + { + return !value.IsEven(); + } + + /// + /// Returns a value indicating whether the current value is a prime number. + /// + /// The value whose primality to check. + /// + /// if is prime, or otherwise. + /// + public static bool IsPrime(this byte value) + { + return value switch + { + 2 => true, + 3 => true, + 5 => true, + 7 => true, + 11 => true, + 13 => true, + 17 => true, + 19 => true, + 23 => true, + 29 => true, + 31 => true, + 37 => true, + 41 => true, + 43 => true, + 47 => true, + 53 => true, + 59 => true, + 61 => true, + 67 => true, + 71 => true, + 73 => true, + 79 => true, + 83 => true, + 89 => true, + 97 => true, + 101 => true, + 103 => true, + 107 => true, + 109 => true, + 113 => true, + 127 => true, + 131 => true, + 137 => true, + 139 => true, + 149 => true, + 151 => true, + 157 => true, + 163 => true, + 167 => true, + 173 => true, + 179 => true, + 181 => true, + 191 => true, + 193 => true, + 197 => true, + 199 => true, + 211 => true, + 223 => true, + 227 => true, + 229 => true, + 233 => true, + 239 => true, + 241 => true, + 251 => true, + _ => false + }; + } + + /// + /// Converts the value of the current 8-bit unsigned integer to an equivalent Boolean value. + /// + /// The 8-bit unsigned integer to convert. + /// + /// if is not zero, or otherwise. + /// + /// + /// + /// The following example converts an array of values to values. + /// + /// + /// byte[] bytes = { byte.MinValue, 100, 200, byte.MaxValue }; + /// bool result; + /// + /// foreach (byte value in bytes) + /// { + /// result = value.ToBoolean(); + /// Console.WriteLine("{0, -5} --> {1}", value, result); + /// } + /// + /// // The example displays the following output: + /// // 0 --> False + /// // 100 --> True + /// // 200 --> True + /// // 255 --> True + /// + /// + public static bool ToBoolean(this byte value) + { + return value != 0; + } + } +} diff --git a/X10D/src/ByteExtensions/SByteExtensions.cs b/X10D/src/ByteExtensions/SByteExtensions.cs new file mode 100644 index 0000000..23924f5 --- /dev/null +++ b/X10D/src/ByteExtensions/SByteExtensions.cs @@ -0,0 +1,116 @@ +using System; + +namespace X10D.ByteExtensions +{ + /// + /// Extension methods for . + /// + [CLSCompliant(false)] + public static class SByteExtensions + { + /// + /// 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. + /// + public static bool IsEven(this sbyte value) + { + return value % 2 == 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. + /// + public static bool IsOdd(this sbyte value) + { + return !value.IsEven(); + } + + /// + /// Returns a value indicating whether the current value is a prime number. + /// + /// The value whose primality to check. + /// + /// if is prime, or otherwise. + /// + public static bool IsPrime(this sbyte value) + { + return value switch + { + 2 => true, + 3 => true, + 5 => true, + 7 => true, + 11 => true, + 13 => true, + 17 => true, + 19 => true, + 23 => true, + 29 => true, + 31 => true, + 37 => true, + 41 => true, + 43 => true, + 47 => true, + 53 => true, + 59 => true, + 61 => true, + 67 => true, + 71 => true, + 73 => true, + 79 => true, + 83 => true, + 89 => true, + 97 => true, + 101 => true, + 103 => true, + 107 => true, + 109 => true, + 113 => true, + 127 => true, + _ => false + }; + } + + /// + /// Converts the value of the current 8-bit signed integer to an equivalent Boolean value. + /// + /// The 8-bit signed integer to convert. + /// + /// if is not zero, or otherwise. + /// + /// + /// + /// The following example converts an array of values to values. + /// + /// + /// byte[] bytes = { byte.MinValue, 100, 200, byte.MaxValue }; + /// bool result; + /// + /// foreach (byte value in bytes) + /// { + /// result = value.ToBoolean(); + /// Console.WriteLine("{0, -5} --> {1}", value, result); + /// } + /// + /// // The example displays the following output: + /// // 0 --> False + /// // 100 --> True + /// // 200 --> True + /// // 255 --> True + /// + /// + public static bool ToBoolean(this sbyte value) + { + return value != 0; + } + } +}