Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
C# enum flag comparison with bitwise operators
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { [Flags] public enum Choices { None = 0, //0, First = 1 << 0, //1 Second = 1 << 1, //2 Third = 1 << 2, //4 Fourth = 1 << 3 //8 } public static void Main(string[] args) { Choices firstandsecond = Choices.First | Choices.Second; int asdf = (int)firstandsecond; Choices casted = (Choices)asdf; bool hasFirst = (casted & Choices.First) != 0; //same as Choices.None bool hasSecond = (casted & Choices.Second) != Choices.None; //same as 0 bool hasThird = (casted & Choices.Third) != Choices.None; Console.WriteLine(asdf); Console.WriteLine(casted); Console.WriteLine(hasFirst); Console.WriteLine(hasSecond); Console.WriteLine(hasThird); Console.WriteLine("-----"); Choices CChoice = Choices.None; CChoice |= Choices.First; CChoice |= Choices.Second; Console.WriteLine(CChoice); CChoice |= Choices.Third; Console.WriteLine(CChoice); CChoice &= ~Choices.First; Console.WriteLine(CChoice); } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
dxsdxsd
Pizzas & List of Tuples
SimpleHelloworldProgram
Combustible SetUp
test
List of Lists example
Game 1.0.2
ANQ Bug with Feb month display
Binary search, IComparer, in lambda expressions style
Enigme
Please log in to post a comment.