EnumBitMaskHelper

Class in ChivalrousGames.Common

Description

Static class with helper methods to use enumerations as bit masks (flags).

Usage Example

//An enum prepared for bit mask
public enum MyCustomEnum {
		Flag1 = 1,
		Flag2 = 2,
		Flag3 = 4,
		Flag4 = 8
	}
//Using a bit mask enum
MyCustomEnum myFlags;
EnumBitMaskHelper.Set(ref myFlags, MyCustomEnum.Flag1);
EnumBitMaskHelper.Set(ref myFlags, MyCustomEnum.Flag2);
EnumBitMaskHelper.Unset(ref myFlags, MyCustomEnum.Flag2);
bool myFlag1IsSet = EnumBitMaskHelper.IsSet (myFlags, MyCustomEnum.Flag1);
bool myFlag2IsSet = EnumBitMaskHelper.IsSet (myFlags, MyCustomEnum.Flag1);
//In this example, the enum myFlags has the flag "Flag1" set to true.
//"Flag2" was set to true for a moment and the set to false.
//myFlag1IsSet would have value "true".
//myFlag1IsSet would have value "false".

Static Methods

IsSet
public static bool IsSet(T bits, T bit) where T : struct;
Parameters
bits T (Generic, should be an enumeration) The enum in which the flag could be set
bit T (Generic, should be an enumeration) The flag that must be verified
Return
bool The state of the flag “bit” in flags “bits” (true if flag is set, false otherwise)
Description

Verifies if flag “bit” is set in flags “bits”. This will work on an enum that is prepared for bit masks. See the class usage example.

Set
public static void Set(ref T bits, T bit) where T : struct;
Parameters
bits T (Generic, should be an enumeration) The enum in which the flag will be set. Passed as a ref, which means if will come back modified from the method call.
bit T (Generic, should be an enumeration) The flag that must be set in the enumeration variable.
Description

Sets a flag in an enumeration variable. This will work on an enum that is prepared for bit masks. See the class usage example.

Unset
public static void Unset(ref T bits, T bit) where T : struct;
Parameters
bits T (Generic, should be an enumeration) The enum in which the flag will be unset. Passed as a ref, which means if will come back modified from the method call.
bit T (Generic, should be an enumeration) The flag that must be unset in the enumeration variable.
Description

Unsets a flag in an enumeration variable. This will work on an enum that is prepared for bit masks. See the class usage example.