asp.net C # bitwise AND, bitwise, or combination

Source: Internet
Author: User
Tags bitwise

ASP tutorials. NET C # bitwise AND, bitwise, or combination
First, each number of permissions is 2 of the N-second square number

such as: k1=2; Add to

k2=4; Delete

k3=8; Modify

...

This defines the number of functional permissions, and when you need to combine permissions, you need to bitwise or for each number of permissions you have.

Such as:

purview = K2|K3; assigning to add and remove permissions

When you need to determine whether you have a certain permission in the permission string, you need to place a bitwise AND.

Such as:

if ((Purview & K1) >0)//Determines whether this permission string has add permissions, the result >0 into the If statement code block

{

....

}

There's got to be some doubt about this, so don't worry, I'll explain it carefully.

First, the 8-bit binary value of 2 is 00000010

The 8-bit binary value of 4 is 00000100

The 8-bit binary value of 8 is 00001000

Second, when the 8 and 4 are bitwise OR manipulated, the result is:

4|8 = 12

00000100 |00001000 = 00001100

Why is that so? 00000001|00000001=00000001 00000001|00000000=1; 00000000|00000000=00000000 in the case of a bitwise OR operation;

In other words, except the 0|0 result is 0, the other results are 1

So 00000100 |00001000 = 00001100

Also say above purview = K2|K3 Binary Value result is purview = 00000100 |00001000 = 00001100.

Third, when the 8 and 4 are bitwise and manipulated, the result is:

4&8=0

00000100 &00001000 = 00000000

And why is that so? 00000001&00000001=00000001 00000001&00000000=00000000; 00000000&00000000=00000000 at the time of bitwise AND operation;

In other words, except the 1&1 result is 1, the other results are 0

So 00000100 &00001000 = 00000000

The result of the if ((Purview & K1) >0) above is 00001100&00000010 =00000000 (0>0) =false

The result of the same if ((Purview & K2) >0) is 00001100&00000100 =00000100=4=k2 (4>0) =true

In this way we understand the combination of permission strings and the principle of judging permissions, the white is to 2n the number of bitwise AND and bitwise OR

Related Article

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.