Title: Added Bitwise.cs for bitwise operations on enums
x) Created a new file, Bitwise.cs, which includes two static methods for performing bitwise operations on enums. x) The method `ByPointers_DirectInt` directly converts an enum value to its integer representation using unsafe code and pointer manipulation. This is useful when we need the exact binary representation of an enum. x) The method `On` checks if a specific bit (represented by an enum value) is set in a given bitfield. It uses the `ByPointers_DirectInt` method to convert the enum value to its integer equivalent before performing the bitwise operation. x) These changes provide more efficient ways of handling bitwise operations with enums in our codebase.
This commit is contained in:
parent
f9907bb4e0
commit
6f63f550fa
29
bit/Bitwise.cs
Normal file
29
bit/Bitwise.cs
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
|
||||
using System.Numerics;
|
||||
|
||||
static public class bit
|
||||
{
|
||||
|
||||
public static unsafe TInt ByPointers_DirectInt<TInt, TEnum>(TEnum enumValue)
|
||||
where TInt : IBinaryInteger<TInt>
|
||||
where TEnum : unmanaged, System.Enum
|
||||
{
|
||||
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
|
||||
return *(TInt*)(&enumValue);
|
||||
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
|
||||
}
|
||||
|
||||
|
||||
|
||||
static public bool On<T, E>( T bitfield, E e )
|
||||
where T: IBinaryInteger<T>
|
||||
where E: unmanaged, System.Enum
|
||||
{
|
||||
var eInt = ByPointers_DirectInt<T, E>( e );
|
||||
|
||||
return (eInt & bitfield) == eInt;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user