From 6f63f550fa802bc12cdc98c3b66968aa9bf9a173 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Mon, 22 Jul 2024 18:47:54 -0700 Subject: [PATCH] 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. --- bit/Bitwise.cs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 bit/Bitwise.cs diff --git a/bit/Bitwise.cs b/bit/Bitwise.cs new file mode 100644 index 0000000..ed055f4 --- /dev/null +++ b/bit/Bitwise.cs @@ -0,0 +1,29 @@ + + +using System.Numerics; + +static public class bit +{ + +public static unsafe TInt ByPointers_DirectInt(TEnum enumValue) +where TInt : IBinaryInteger +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 bitfield, E e ) +where T: IBinaryInteger +where E: unmanaged, System.Enum +{ + var eInt = ByPointers_DirectInt( e ); + + return (eInt & bitfield) == eInt; +} + + +}