diff --git a/Core/Extensions/BitHelper.cs b/Core/Extensions/BitHelper.cs index cfd66ce9..ad5f2688 100644 --- a/Core/Extensions/BitHelper.cs +++ b/Core/Extensions/BitHelper.cs @@ -12,7 +12,7 @@ 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(this T bits, byte idx) where T : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable => + 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. @@ -21,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(this T bits, byte startIdx, byte count) where T : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable + 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)); diff --git a/PInvoke/Kernel32/PsApi.cs b/PInvoke/Kernel32/PsApi.cs index 92d83b11..53775369 100644 --- a/PInvoke/Kernel32/PsApi.cs +++ b/PInvoke/Kernel32/PsApi.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; -using Vanara.Extensions; using Vanara.InteropServices; +using static Vanara.Extensions.BitHelper; namespace Vanara.PInvoke { @@ -1617,10 +1617,10 @@ namespace Vanara.PInvoke public UIntPtr Flags; /// If , the page is sharable; otherwise, the page is not sharable. - public bool Shared => Flags.ToUInt32().GetBit(9); + public bool Shared => GetBit(Flags.ToUInt32(), 9); /// The number of processes that share this page. The maximum value of this member is 7. - public uint ShareCount => Flags.ToUInt32().GetBits(5, 3); + public uint ShareCount => GetBits(Flags.ToUInt32(), 5, 3); /// /// The protection attributes of the page. This member can be one of the following values. @@ -1759,7 +1759,7 @@ namespace Vanara.PInvoke /// /// /// - public uint Protection => Flags.ToUInt32().GetBits(0, 5); + public uint Protection => GetBits(Flags.ToUInt32(), 0, 5); public IntPtr VirtualPage => new IntPtr((long)Flags.ToUInt64() & ~0xFFFL); } @@ -1775,19 +1775,19 @@ namespace Vanara.PInvoke public UIntPtr Flags; /// If , the page is valid; otherwise, the page is not valid. - public bool Valid => Flags.ToUInt32().GetBit(0); + public bool Valid => GetBit(Flags.ToUInt32(), 0); - public bool Locked => Valid ? Flags.ToUInt32().GetBit(22) : false; + public bool Locked => Valid ? GetBit(Flags.ToUInt32(), 22) : false; - public bool LargePage => Valid ? Flags.ToUInt32().GetBit(23) : false; + public bool LargePage => Valid ? GetBit(Flags.ToUInt32(), 23) : false; - public bool Bad => Flags.ToUInt32().GetBit(31); + public bool Bad => GetBit(Flags.ToUInt32(), 31); /// If , the page is sharable; otherwise, the page is not sharable. - public bool Shared => Flags.ToUInt32().GetBit(15); + public bool Shared => GetBit(Flags.ToUInt32(), 15); /// The number of processes that share this page. The maximum value of this member is 7. - public uint ShareCount => Valid ? Flags.ToUInt32().GetBits(1, 3) : 0U; + public uint ShareCount => Valid ? GetBits(Flags.ToUInt32(), 1, 3) : 0U; /// /// The protection attributes of the page. This member can be one of the following values. @@ -1926,9 +1926,9 @@ namespace Vanara.PInvoke /// /// /// - public uint Protection => Valid ? Flags.ToUInt32().GetBits(4, 11) : 0U; + public uint Protection => Valid ? GetBits(Flags.ToUInt32(), 4, 11) : 0U; - public uint Node => Valid ? Flags.ToUInt32().GetBits(0, 5) : 0U; + public uint Node => Valid ? GetBits(Flags.ToUInt32(), 0, 5) : 0U; } /// Contains working set information for a process. diff --git a/PInvoke/Kernel32/WinNT.cs b/PInvoke/Kernel32/WinNT.cs index 61db07c3..3135fd54 100644 --- a/PInvoke/Kernel32/WinNT.cs +++ b/PInvoke/Kernel32/WinNT.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.InteropServices; using Vanara.Extensions; +using static Vanara.Extensions.BitHelper; namespace Vanara.PInvoke { @@ -732,36 +733,36 @@ namespace Vanara.PInvoke /// /// The type of segment. This member can be one of the following values: /// - public byte Type { get => (byte)Flags.GetBits(0, 5); set => BitHelper.SetBits(ref Flags, 0, 5, value); } + public byte Type { get => GetBits((byte)Flags, 0, 5); set => SetBits(ref Flags, 0, 5, value); } /// /// /// The privilege level of the descriptor. This member is an integer value in the range 0 (most privileged) through 3 (least privileged). /// /// - public byte Dpl { get => (byte)Flags.GetBits(5, 2); set => BitHelper.SetBits(ref Flags, 5, 2, value); } + public byte Dpl { get => GetBits((byte)Flags, 5, 2); set => SetBits(ref Flags, 5, 2, value); } /// /// The present flag. This member is 1 if the segment is present in physical memory or 0 if it is not. /// - public bool Pres { get => Flags.GetBit(7); set => BitHelper.SetBit(ref Flags, 7, value); } + public bool Pres { get => GetBit(Flags, 7); set => SetBit(ref Flags, 7, value); } /// /// The high bits (16–19) of the address of the last byte in the segment. /// - public byte LimitHi { get => (byte)Flags.GetBits(8, 4); set => BitHelper.SetBits(ref Flags, 8, 4, value); } + public byte LimitHi { get => GetBits((byte)Flags, 8, 4); set => SetBits(ref Flags, 8, 4, value); } /// /// /// The space that is available to system programmers. This member might be used for marking segments in some system-specific way. /// /// - public bool Sys { get => Flags.GetBit(12); set => BitHelper.SetBit(ref Flags, 12, value); } + public bool Sys { get => GetBit(Flags, 12); set => SetBit(ref Flags, 12, value); } /// /// Reserved. /// - public bool Reserved_0 { get => Flags.GetBit(13); set => BitHelper.SetBit(ref Flags, 13, value); } + public bool Reserved_0 { get => GetBit(Flags, 13); set => SetBit(ref Flags, 13, value); } /// /// @@ -772,12 +773,12 @@ namespace Vanara.PInvoke /// If the segment is a code segment, this member contains 1. The segment runs with the default (native mode) instruction set. /// /// - public bool Default_Big { get => Flags.GetBit(14); set => BitHelper.SetBit(ref Flags, 14, value); } + public bool Default_Big { get => GetBit(Flags, 14); set => SetBit(ref Flags, 14, value); } /// /// The granularity. This member contains 0 if the segment is byte granular, 1 if the segment is page granular. /// - public bool Granularity { get => Flags.GetBit(15); set => BitHelper.SetBit(ref Flags, 15, value); } + public bool Granularity { get => GetBit(Flags, 15); set => SetBit(ref Flags, 15, value); } } /// Used by thread context functions.