Last active
January 10, 2021 00:00
-
-
Save KaiserWerk/73e4d4b635bee496869e3fa968fdfd63 to your computer and use it in GitHub Desktop.
C# Example Bitmask Implementation
This file contains hidden or 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
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