"&" Bitwise AND operator "|" permission control algorithm for BITS or operators

Source: Internet
Author: User
Tags bitwise

"&" bits and operators:

The bitwise AND operator "&" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.

Such as:

00001001& 00000101 = 00000001

The "|" bit or operator:

Bitwise OR operator ' | ' is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. The two numbers participating in the operation are in complement.

00001001| 00000101 = 00001101 When you get user permissions, use the "|" operator to get user permission values, and use the "&" operator when comparing user permissions to compare whether a user has permission to do so. For example: User A has permissions:
Permission ID Permission value Permission Name
1 1 Inquire
2 2 New
3 4 Delete
4 8 Update
Permission value =2^ (permission ID-1) User A's permission value =1|2|4|8=15 0001 |0010 |0100 |1000=1111 Determine if the user has delete permission 15&4!=0 operation result 1111&0100=0100 0100 = 4 is not equal to 0, indicating permission. If the user rights value = User A's permission value =1|2|8=11 0001 |0010 |1000=1011 Determine whether the user has delete permissions 11&4!=0 operation results 1011&0100=0000 0000=0 equals 0, indicating no permissions. Query permissions directly determine user A's permission value ==0 this way, as long as the user has any permissions can query the document.

"&" Bitwise AND operator "|" permission control algorithm for BITS or operators

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.