A bit tag combined with the Flags attribute enumeration in unity using a small note

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.