Created
October 7, 2013 23:09
-
-
Save kwokhou/6876566 to your computer and use it in GitHub Desktop.
Using bit flags in C#
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
[Flags] | |
public enum ExampleEnum | |
{ | |
A = 1 << 0, | |
B = 1 << 1, | |
C = 1 << 2, | |
D = 1 << 3, | |
E = 1 << 4, | |
} | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var foo = ExampleEnum.A | ExampleEnum.B | ExampleEnum.C; | |
if ((foo & ExampleEnum.B) == ExampleEnum.B) // contains flag B! | |
{ | |
} | |
} | |
} | |
public static bool HasBitFlag(this ExampleEnum e, ExampleEnum other) | |
{ | |
return ((e & other) == other); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment