Hasflag function for C # Binary or algorithm implementation enumeration

Source: Internet
Author: User

From:http://www.cnblogs.com/icyJ/archive/2013/02/20/HasFlag.html

In the management of permissions, there is often a permission to include the phenomenon. For example, there are three basic permissions: Staff A, staff B, Staff C. On this basis, there is the manager authority, which includes a and B permissions, as well as the boss rights, including a/b/c three kinds of permissions.

In code, we can use enumerations to manage these permissions.

[Flags]
PublicenumEnumhasflag
{
A =1<<0,
B =1<<1,
C =1<<2,
Manager = A | B
Boss = A | B | C
}

The feature of this code is that the definition enumeration uses a property to limit [Flags], and each value is assigned a value in binary increments. The advantage of this is that the Hasflag function of the enumeration can be used to determine whether a permission contains another permission.

StaticvoidMain (string[] args)
{
varRighta = Enumhasflag.boss;
varRightB = Enumhasflag.manager;
if(Righta.hasflag (ENUMHASFLAG.C)) Console.WriteLine ("Righta can do this");
if(Rightb.hasflag (ENUMHASFLAG.C)) Console.WriteLine ("RightB can do this");
Console.readkey ();
}

The final code outputs: Righta can do. In this way, the inclusion relationship of the enumeration value can be judged by hasflag, so that the corresponding permission is specified and managed.

This effect can also be achieved by using a binary or operation. The base statement is source | target = = Source. A value A, with another value B or after the result of the operation or a, you can determine that a contains B.

Static void Main (string[] args) {    
var 1 0 1 1 ;    
if (A | (10)) = = A)
Console.WriteLine ("A has 1<<0");    
if (A | (12)) ! = A)
Console.WriteLine ("A doesn ' t has 1<<2");}

The output of the code is:

A has 1<<0

A doesn ' t has 1<<2

On the premise of understanding logic, we can do the following switches:

StaticvoidMain (string[] args)
{
ControlCenter (1<<0|1<<3);
}

Static voidControlCenter (intInput
{
if(Input | (1<<0)) = = input) Console.WriteLine ("Do 0");
if(Input | (1<<1)) = = input) Console.WriteLine ("Do 1");
if(Input | (1<<2)) = = input) Console.WriteLine ("Do 2");
}

The final output can be tested on your own.

This article uses two methods to achieve the inclusion relationship management of numerical values. After a careful understanding of the logic of implementation, it can be used in many places. For example, we can combine the values of multiple settings into a single field. Form ' 10111101 ', with minimal code to manage these settings. In cases where there are few options and the object's active domain is small, you can consider using binary or arithmetic implementations. The advantage of this implementation is that you can not build the enumeration separately, the code is much less, the disadvantage is that the code is poor readability, the call flexibility is not as hasflag as the enumeration, the scalability is not strong.

Supplement . Below the park friend proposed the source & target = = target judgment algorithm, to determine whether the source contains target. I think the conditions are very good, and the whole idea is clearer than or calculated. Later, looking at other information, found that the enumeration of 1,2,4,8 understanding, a lot of it is from this formula.

Static voidMain (string[] args)
{
varxx = Testenum.manager;
if(xx & TESTENUM.A) = = TESTENUM.A) Console.WriteLine ("Has A");
Console.readkey ();
}

enumTestenum
{
A =1<<0,
B =1<<1,
C =1<<2,
Manager = A | B
Boss = A | B | C
}

Hasflag function for C # Binary or algorithm implementation enumeration

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.