Last active
June 28, 2021 16:51
-
-
Save logicseed/7a6fe8c1d1ffd6a3fc6c2384dcd83a47 to your computer and use it in GitHub Desktop.
C# 7.2+ Bit Flag Enumeration Templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright © 2021 Marc King <[email protected]> - All Rights Reserved | |
// | |
// SPDX-License-Identifier: MIT | |
// | |
// This work is licensed under the terms of the MIT license. | |
using System; | |
using System.Runtime.CompilerServices; | |
/* | |
* This is a series of 8, 16, 32, and 64 bit flag enumerations that can be easily copied into | |
* a project. They use numeric literals that only work in C# 7.2+. Also included are extension | |
* methods that allow for easy use of these bit flag enumerations. | |
*/ | |
[Flags] | |
public enum FlagEnum8 : byte | |
{ | |
None = 0b_0000_0000, | |
// Value01 = 0b_0000_0001, | |
// Value02 = 0b_0000_0010, | |
// Value03 = 0b_0000_0100, | |
// Value04 = 0b_0000_1000, | |
// Value05 = 0b_0001_0000, | |
// Value06 = 0b_0010_0000, | |
// Value07 = 0b_0100_0000, | |
// Value08 = 0b_1000_0000, | |
All = 0b_1111_1111, | |
} | |
public static partial class BitFlagEnumExtensions | |
{ | |
/// <summary> | |
/// Sets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to set.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag set.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum8 Set(this FlagEnum8 @this, FlagEnum8 flag) => @this |= flag; | |
/// <summary> | |
/// Unsets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to unset.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag unset.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum8 Unset(this FlagEnum8 @this, FlagEnum8 flag) => @this &= ~flag; | |
/// <summary> | |
/// Toggles the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to toggle.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag toggled.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum8 Toggle(this FlagEnum8 @this, FlagEnum8 flag) => @this ^= flag; | |
/// <summary> | |
/// Checks if the big flag enumeration has the specified bit flag set. | |
/// </summary> | |
/// <param name="flag">A bit flag for which to check.</param> | |
/// <returns>Returns <c>true</c> is the bit flag is set; otherwise <c>false</c>.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static bool IsSet(this FlagEnum8 @this, FlagEnum8 flag) => (@this & flag) == flag; | |
} | |
[Flags] | |
public enum FlagEnum16 : ushort | |
{ | |
None = 0b_0000_0000_0000_0000, | |
// Value01 = 0b_0000_0000_0000_0001, | |
// Value02 = 0b_0000_0000_0000_0010, | |
// Value03 = 0b_0000_0000_0000_0100, | |
// Value04 = 0b_0000_0000_0000_1000, | |
// Value05 = 0b_0000_0000_0001_0000, | |
// Value06 = 0b_0000_0000_0010_0000, | |
// Value07 = 0b_0000_0000_0100_0000, | |
// Value08 = 0b_0000_0000_1000_0000, | |
// Value09 = 0b_0000_0001_0000_0000, | |
// Value10 = 0b_0000_0010_0000_0000, | |
// Value11 = 0b_0000_0100_0000_0000, | |
// Value12 = 0b_0000_1000_0000_0000, | |
// Value13 = 0b_0001_0000_0000_0000, | |
// Value14 = 0b_0010_0000_0000_0000, | |
// Value15 = 0b_0100_0000_0000_0000, | |
// Value16 = 0b_1000_0000_0000_0000, | |
All = 0b_1111_1111_1111_1111, | |
} | |
public static partial class BitFlagEnumExtensions | |
{ | |
/// <summary> | |
/// Sets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to set.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag set.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum16 Set(this FlagEnum16 @this, FlagEnum16 flag) => @this |= flag; | |
/// <summary> | |
/// Unsets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to unset.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag unset.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum16 Unset(this FlagEnum16 @this, FlagEnum16 flag) => @this &= ~flag; | |
/// <summary> | |
/// Toggles the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to toggle.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag toggled.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum16 Toggle(this FlagEnum16 @this, FlagEnum16 flag) => @this ^= flag; | |
/// <summary> | |
/// Checks if the big flag enumeration has the specified bit flag set. | |
/// </summary> | |
/// <param name="flag">A bit flag for which to check.</param> | |
/// <returns>Returns <c>true</c> is the bit flag is set; otherwise <c>false</c>.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static bool IsSet(this FlagEnum16 @this, FlagEnum16 flag) => (@this & flag) == flag; | |
} | |
[Flags] | |
public enum FlagEnum32 : uint | |
{ | |
None = 0b_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value01 = 0b_0000_0000_0000_0000_0000_0000_0000_0001, | |
// Value02 = 0b_0000_0000_0000_0000_0000_0000_0000_0010, | |
// Value03 = 0b_0000_0000_0000_0000_0000_0000_0000_0100, | |
// Value04 = 0b_0000_0000_0000_0000_0000_0000_0000_1000, | |
// Value05 = 0b_0000_0000_0000_0000_0000_0000_0001_0000, | |
// Value06 = 0b_0000_0000_0000_0000_0000_0000_0010_0000, | |
// Value07 = 0b_0000_0000_0000_0000_0000_0000_0100_0000, | |
// Value08 = 0b_0000_0000_0000_0000_0000_0000_1000_0000, | |
// Value09 = 0b_0000_0000_0000_0000_0000_0001_0000_0000, | |
// Value10 = 0b_0000_0000_0000_0000_0000_0010_0000_0000, | |
// Value11 = 0b_0000_0000_0000_0000_0000_0100_0000_0000, | |
// Value12 = 0b_0000_0000_0000_0000_0000_1000_0000_0000, | |
// Value13 = 0b_0000_0000_0000_0000_0001_0000_0000_0000, | |
// Value14 = 0b_0000_0000_0000_0000_0010_0000_0000_0000, | |
// Value15 = 0b_0000_0000_0000_0000_0100_0000_0000_0000, | |
// Value16 = 0b_0000_0000_0000_0000_1000_0000_0000_0000, | |
// Value17 = 0b_0000_0000_0000_0001_0000_0000_0000_0000, | |
// Value18 = 0b_0000_0000_0000_0010_0000_0000_0000_0000, | |
// Value19 = 0b_0000_0000_0000_0100_0000_0000_0000_0000, | |
// Value20 = 0b_0000_0000_0000_1000_0000_0000_0000_0000, | |
// Value21 = 0b_0000_0000_0001_0000_0000_0000_0000_0000, | |
// Value22 = 0b_0000_0000_0010_0000_0000_0000_0000_0000, | |
// Value23 = 0b_0000_0000_0100_0000_0000_0000_0000_0000, | |
// Value24 = 0b_0000_0000_1000_0000_0000_0000_0000_0000, | |
// Value25 = 0b_0000_0001_0000_0000_0000_0000_0000_0000, | |
// Value26 = 0b_0000_0010_0000_0000_0000_0000_0000_0000, | |
// Value27 = 0b_0000_0100_0000_0000_0000_0000_0000_0000, | |
// Value28 = 0b_0000_1000_0000_0000_0000_0000_0000_0000, | |
// Value29 = 0b_0001_0000_0000_0000_0000_0000_0000_0000, | |
// Value30 = 0b_0010_0000_0000_0000_0000_0000_0000_0000, | |
// Value31 = 0b_0100_0000_0000_0000_0000_0000_0000_0000, | |
// Value32 = 0b_1000_0000_0000_0000_0000_0000_0000_0000, | |
All = 0b_1111_1111_1111_1111_1111_1111_1111_1111, | |
} | |
public static partial class BitFlagEnumExtensions | |
{ | |
/// <summary> | |
/// Sets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to set.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag set.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum32 Set(this FlagEnum32 @this, FlagEnum32 flag) => @this |= flag; | |
/// <summary> | |
/// Unsets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to unset.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag unset.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum32 Unset(this FlagEnum32 @this, FlagEnum32 flag) => @this &= ~flag; | |
/// <summary> | |
/// Toggles the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to toggle.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag toggled.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum32 Toggle(this FlagEnum32 @this, FlagEnum32 flag) => @this ^= flag; | |
/// <summary> | |
/// Checks if the big flag enumeration has the specified bit flag set. | |
/// </summary> | |
/// <param name="flag">A bit flag for which to check.</param> | |
/// <returns>Returns <c>true</c> is the bit flag is set; otherwise <c>false</c>.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static bool IsSet(this FlagEnum32 @this, FlagEnum32 flag) => (@this & flag) == flag; | |
} | |
[Flags] | |
public enum FlagEnum64 : ulong | |
{ | |
None = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value01 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001, | |
// Value02 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010, | |
// Value03 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100, | |
// Value04 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000, | |
// Value05 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000, | |
// Value06 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010_0000, | |
// Value07 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000, | |
// Value08 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000, | |
// Value09 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000_0000, | |
// Value10 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000, | |
// Value11 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000, | |
// Value12 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000_0000, | |
// Value13 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000_0000_0000, | |
// Value14 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000, | |
// Value15 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000, | |
// Value16 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000_0000_0000, | |
// Value17 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000, | |
// Value18 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000, | |
// Value19 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000_0000, | |
// Value20 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000_0000_0000_0000, | |
// Value21 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000, | |
// Value22 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000, | |
// Value23 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000_0000_0000, | |
// Value24 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000, | |
// Value25 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000, | |
// Value26 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000_0000, | |
// Value27 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000_0000_0000_0000, | |
// Value28 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000, | |
// Value29 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000, | |
// Value30 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000_0000_0000, | |
// Value31 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000, | |
// Value32 = 0b_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000, | |
// Value33 = 0b_0000_0000_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value34 = 0b_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value35 = 0b_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value36 = 0b_0000_0000_0000_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value37 = 0b_0000_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value38 = 0b_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value39 = 0b_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value40 = 0b_0000_0000_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value41 = 0b_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value42 = 0b_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value43 = 0b_0000_0000_0000_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value44 = 0b_0000_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value45 = 0b_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value46 = 0b_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value47 = 0b_0000_0000_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value48 = 0b_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value49 = 0b_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value50 = 0b_0000_0000_0000_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value51 = 0b_0000_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value52 = 0b_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value53 = 0b_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value54 = 0b_0000_0000_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value55 = 0b_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value56 = 0b_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value57 = 0b_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value58 = 0b_0000_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value59 = 0b_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value60 = 0b_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value61 = 0b_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value62 = 0b_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value63 = 0b_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
// Value64 = 0b_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000, | |
All = 0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, | |
} | |
public static partial class BitFlagEnumExtensions | |
{ | |
/// <summary> | |
/// Sets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to set.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag set.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum64 Set(this FlagEnum64 @this, FlagEnum64 flag) => @this |= flag; | |
/// <summary> | |
/// Unsets the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to unset.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag unset.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum64 Unset(this FlagEnum64 @this, FlagEnum64 flag) => @this &= ~flag; | |
/// <summary> | |
/// Toggles the specified tag on the bit flag enumeration. | |
/// </summary> | |
/// <param name="flag">A bit flag to toggle.</param> | |
/// <returns>A copy of the bit flag enumeration with the specified bit flag toggled.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static FlagEnum64 Toggle(this FlagEnum64 @this, FlagEnum64 flag) => @this ^= flag; | |
/// <summary> | |
/// Checks if the big flag enumeration has the specified bit flag set. | |
/// </summary> | |
/// <param name="flag">A bit flag for which to check.</param> | |
/// <returns>Returns <c>true</c> is the bit flag is set; otherwise <c>false</c>.</returns> | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static bool IsSet(this FlagEnum64 @this, FlagEnum64 flag) => (@this & flag) == flag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment