Skip to content

Instantly share code, notes, and snippets.

@KaiserWerk
Last active January 10, 2021 00:00
Show Gist options
  • Save KaiserWerk/73e4d4b635bee496869e3fa968fdfd63 to your computer and use it in GitHub Desktop.
Save KaiserWerk/73e4d4b635bee496869e3fa968fdfd63 to your computer and use it in GitHub Desktop.
C# Example Bitmask Implementation
using System;
namespace BitmaskTest
{
[Flags]
public enum Names
{
NoOne = 0,
Tim = 1,
Alfred = 2,
Michelle = 4,
Jon = 8,
}
class Program
{
static void Main(string[] args)
{
MethodWithBitmask(Names.Tim | Names.Jon);
}
static void MethodWithBitmask(Names names)
{
bool timIsThere = (names & Names.Tim) != Names.NoOne;
bool alfredIsThere = (names & Names.Alfred) != Names.NoOne;
bool jonIsThere = (names & Names.Jon) != Names.NoOne;
Console.WriteLine($"Tim is there: {timIsThere}");
Console.WriteLine($"Alfred is there: {alfredIsThere}");
Console.WriteLine($"Jon is there: {jonIsThere}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment