diff --git a/Core/Extensions/BitHelper.cs b/Core/Extensions/BitHelper.cs index d32691f4..9b570d3e 100644 --- a/Core/Extensions/BitHelper.cs +++ b/Core/Extensions/BitHelper.cs @@ -4,6 +4,7 @@ using System.Runtime.InteropServices; namespace Vanara.Extensions { /// Static methods to help with bit manipulation. + /// This class is intended to support whole numbers. Without a specific constraint for numbers, the list of constraints helps to limit incorrect types, but is NOT foolproof. public static class BitHelper { /// Gets the bit value at the specified index in a bit vector. @@ -11,7 +12,8 @@ namespace Vanara.Extensions /// The bit vector. /// The zero-based index of the bit to get. /// if the bit is set (1); otherwise. - public static bool GetBit(T bits, byte idx) where T : IConvertible => (idx < (Marshal.SizeOf(typeof(T)) * 8)) ? (bits.ToInt64(null) & 1 << idx) != 0 : throw new ArgumentOutOfRangeException(nameof(idx)); + public static bool GetBit(T bits, byte idx) where T : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable => + (idx < (Marshal.SizeOf(typeof(T)) * 8)) ? (bits.ToInt64(null) & 1 << idx) != 0 : throw new ArgumentOutOfRangeException(nameof(idx)); /// Gets the bit array value from the specified range in a bit vector. /// The type of the bit vector. Must be of type . @@ -19,7 +21,7 @@ namespace Vanara.Extensions /// The zero-based start index of the bit range to get. /// The number of sequential bits to fetch starting at . /// The value of the requested bit range. - public static T GetBits(T bits, byte startIdx, byte count) where T : IConvertible + public static T GetBits(T bits, byte startIdx, byte count) where T : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable { if (startIdx >= (Marshal.SizeOf(typeof(T)) * 8)) throw new ArgumentOutOfRangeException(nameof(startIdx)); if (count + startIdx > (Marshal.SizeOf(typeof(T)) * 8)) throw new ArgumentOutOfRangeException(nameof(count)); @@ -31,7 +33,7 @@ namespace Vanara.Extensions /// The bit vector. /// The index of the bit to set. /// If set to , set the bit (= 1); otherwise, clear the bit (= 0). - public static void SetBit(ref T bits, byte idx, bool value) where T : IConvertible + public static void SetBit(ref T bits, byte idx, bool value) where T : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable { if (idx >= (Marshal.SizeOf(typeof(T)) * 8)) throw new ArgumentOutOfRangeException(nameof(idx)); long bit = 1 << idx; @@ -46,7 +48,7 @@ namespace Vanara.Extensions /// The zero-based start index of the bit range to set. /// The number of sequential bits to set starting at . /// The value to set within the specified range of . - public static void SetBits(ref T bits, byte startIdx, byte count, TValue value) where T : IConvertible where TValue : IConvertible + public static void SetBits(ref T bits, byte startIdx, byte count, TValue value) where T : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable where TValue : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable { if (startIdx >= (Marshal.SizeOf(typeof(T)) * 8)) throw new ArgumentOutOfRangeException(nameof(startIdx)); if (count + startIdx > (Marshal.SizeOf(typeof(T)) * 8)) throw new ArgumentOutOfRangeException(nameof(count));