Skip to content

Instantly share code, notes, and snippets.

@kwokhou
Created October 7, 2013 23:09
Show Gist options
  • Save kwokhou/6876566 to your computer and use it in GitHub Desktop.
Save kwokhou/6876566 to your computer and use it in GitHub Desktop.
Using bit flags in C#
[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