Application of the program median operation

Source: Internet
Author: User

1. permission settings

Step 1: Create an enumeration to indicate all permission management operations, followed by permission calculation.

Common bitwise operations include (&), (|), and (~), For example:

1 & 0 = 0, 1 | 0 = 1 ,~ 1 = 0

When designing a permission, we can convert the permission management operation to a C # bitwise operation.

Step 1: Create an enumeration to indicate all permission management operations:

[Flags]

Public Enum Permissions

{

Insert = 1,

Delete = 2,

Update = 4,

Query = 8

}

[Flags] indicates that this enumeration can support the C # bitwise operation. For each enumerated value, we use the Npower of 2 to assign a value. In this way, when it is binary, it is exactly 1 = 0001, 2 = 0010, 4 = 0100, 8 = 1000, etc. Each digit indicates a permission. 1 indicates that the permission is granted, and 0 indicates that no.

Next is the permission calculation:

1. Permission addition, which can be implemented using and operations. We know that 0001 | 0100 = 0101 means that permissions with the first and third places are managed at the same time, and enumeration means:

Permissions per = permissions. Insert | permissions. Update

2. Permission Subtraction is implemented by operations + non-operations. To remove the insert permission, perform the following operations:

Permissions per & = ~ Permissions. Insert means 0101 &~ 0001 = 0101 & 1110 = 0100

3. when determining whether a user has the permission for this operation, the user's permission and operation permission should be performed and calculated, if the result is still operation permission management, the user has this permission:

Permissions per = permissions. Insert | permissions. update;

If (per & permissionspermissions. Insert = permissions. insert)

{

// Operation permission

}

When the comparison process is 0101 & 0001 = 0001,000, the 0 bits of 1 are set to 0 with the C # bits, and the 1 bits are compared.

 

2. Month settings

Sometimes you need to save the signs of a month in 12 months in a data table.

At this time, we can use one field to replace 12 fields. We need to use decimal to save the records of the month, and then use bitwise operations to read and judge.

Public Enum monthmask

{

Month1 = 1,

Month1 = 2,

Month3 = 4,

Month4 = 8

......

}

 

A field stores the 12-month mark, which is efficient and simple.

In the same way, binary can be used to save multiple identifiers and can be used in many aspects.

 

Iii. Forum posts

In SQL Server, status fields marked by numbers such as 1, 2, 4, 8, 16... can be accumulated and combined to form various States.

For example, the original number of a record field is 2. If we want to add 4, we can use

Update t_user set iflag = iflag | 4 where userid = 1

(Iflag is the field name)

Example 2: After adding 4, what should we do if we want to remove 4?

Update t_user set iflag = iflag ^ 4 where userid = 1

In this way, 4 is removed from the record.

If we want to select all records for 2, how can we do this?

Select * From t_user where iflag & 2 = 2

Bitwise operations in SQL not only extract various values, but also sort the data.

For example, a field in the News list is marked

1: Stick

2: unstick

4: Recommended

8: Not recommended

The value of this field can be a combination of the four States. What should we do if we want to put all the top states in front of them based on certain conditions?

Select * From t_news order by iflag & 1 DESC

In this way, we will put all the top posts in front. Of course, some where conditions can be added here, and some bit operations can be added in where,

For bit operations, see the corresponding SQL help

Next, let's talk about enumeration bit operations in C #.

Here we define an enumeration

[Flags]
Enum userflag
{
A = 1,
B = 2,
C = 4,
D = 8,
E = 16,
F = 32
}

 

InCodeAdd the following processing

Protected void page_load (Object sender, eventargs E)
{

If (! Ispostback)
{

String strsql = "select * From v_user where iflag & @ iflag = @ iflag ";

// Sqlparameter parm = new sqlparameter ("@ iflag", sqldbtype. Int, 4 );
// Parm. value = userflag. A | userflag. B;
Sqlconnection con = new sqlconnection ("Server =.; database = sinvan_texdb; user id = sa; Pwd = 123 ;");

Sqlcommand comm = new sqlcommand (strsql, con );

Comm. Parameters. Add ("@ iflag", sqldbtype. Int, 4). value = userflag. A | userflag. B;

Sqldataadapter ADP = new sqldataadapter (Comm );
Datatable dtable = new datatable ();
ADP. Fill (dtable );

Userflag = (userflag) enum. parse (typeof (userflag), dtable. Rows [0] [11]. tostring ());

}
}

 

After processing, userflag is a combination of various types in the database.

We can also perform bitwise operations on it.

If you want to add userflag. C, perform the following operations:

Userflag = userflag | userflag. c

To remove userflag. C, perform the following operations:

Userflag = userflag ^ userflag. c

If you want to determine whether the ID contains C, perform the following operations:

(Userflag & userflag. c) = userflag. c

 

 

 

 

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.