The use of bit marks in conjunction with the Flags attribute enumeration
Enum is often used in applications to mark some of the effects that we need to function like switches, and the default initialization value of Enum is assigned according to the following form. Such as:
Figure-1
And when you mark a bit with an enum, you tend to use hexadecimal instead of decimal, because the correlation of decimal is much stronger than hexadecimal. As early as c#4.0, to determine whether a particular bit is set, is to use the bit and operator for the glyph with that bit flag, such as:
BOOL usered = (OPS & colors.red) = = colors.red;
C#4.0 introduced the Hasflag method to detect multiple marker bits, specifically, the method should be checked with MSDN.
Characteristics:
There are several conveniences for using features, such as:
First, it informs the compiler, the Object Browser, and other tools that observe this code that the members of the enumeration are to be combined into bit marks instead of being used only as separate values;
Second, the ToString method of an enumeration is allowed to provide a more appropriate format for the value of the bit tag.
Here is an example:
1 [Flags]2 enumCarddecksettings:UINT3 {4Singledeck =0x01,//bit 05Largepictures =0x02,//bit 16Fancynumbers =0x04,//bit 27Animation =0x08 //bit 38 }9 Ten classMyClass One { A BOOLUsesingledeck =false, -Usebigpics =false, -Usefancynumbers =false, theUseanimation =false, -Useanimationandfancynumbers =false; - - Public voidsetoptions (carddecksettings Ops) + { -Usesingledeck =OPS. Hasflag (Carddecksettings.singledeck); +Usebigpics =OPS. Hasflag (carddecksettings.largepictures); AUsefancynumbers =OPS. Hasflag (carddecksettings.fancynumbers); atUseanimation =OPS. Hasflag (carddecksettings.animation); - -Carddecksettings testflags = -carddecksettings.animation |carddecksettings.fancynumbers; -Useanimationandfancynumbers =OPS. Hasflag (testflags); - } in - Public voidprintoptions () to { +Console.WriteLine ("Option Settings:" ); -Console.WriteLine ("Use single Deck-{0}", Usesingledeck); theConsole.WriteLine ("Use Large Pictures-{0}", usebigpics); *Console.WriteLine ("Use Fancy Numbers-{0}", usefancynumbers); $Console.WriteLine ("Show Animation-{0}", useanimation);Panax NotoginsengConsole.WriteLine ("Show Animation and Fancynumbers-{0}", - useanimationandfancynumbers); the } + } A the class Program + { - Static voidMain (string[] args) $ { $MyClass MC =NewMyClass (); -Carddecksettings Ops =Carddecksettings.singledeck -|carddecksettings.fancynumbers the|carddecksettings.animation; - MC. SetOptions (OPS);Wuyi MC. PrintOptions (); the } -}
A bit tag combined with the Flags attribute enumeration in unity using a small note