The example will explain 1. Permission representation 2. Permission 3. Add permissions 4. Remove Permissions
public class Test {
/**
* @param args
*/
public static void Main (string[] args) {
/**
* Four kinds of permissions, currently defined as int, the following binary means only take the next four bits as a description
*/
Add to
int c = 1;//... 0001
Inquire
int r = 2;//... 0010
Modify
int u = 4;//... 0100
Delete
int d = 8;//... 1000
/**
*
* We can observe the rules of the binary representation of four kinds of permissions, all of which are 2 of the n-th square,
* It means that the last one to add permission is 0, the second to the penultimate is 1, the other is 0, the third to the bottom is 1, the other is 0, the fourth is 1, the other is 0.
*
*/
/**
*1111----This indicates which permissions are available | (bitwise OR) operation
*
*/
User A has Add and modify permissions
int UserA = C | R | U
User B has Add and remove permissions
int UserB = C | D
/**
* 2222----Determine if the user has a certain permission to use the user rights and permissions to judge & (bitwise AND) operation, the result is to determine the value of the permission to indicate that the user has this permission, or do not have this permission
*/
if ((UserA & u) = = u) {
System.out.println ("User A has update permission");
} else {
System.out.println ("User A does not have update permissions");
}
/**
* 3333----Add permissions to users with user rights and permissions to add | (bitwise OR) action to overwrite previous permission values
*/
if ((UserB & u) = = u) {
System.out.println ("User B has update permissions");
} else {
System.out.println ("User B does not have update permissions");
}
Add update permissions to User B
UserB = UserB | U
if ((UserB & u) = = u) {
System.out.println ("User B has update permissions");
} else {
System.out.println ("User B does not have update permissions");
}
/**
* 4444----To cancel a user's permission, with the user rights and the rights to be canceled bitwise reversed after the bitwise operation, and then overwrite the previous permission value
*/
if ((UserA & r) = = r) {
System.out.println ("User A has query permission");
} else {
System.out.println ("User A does not have query permission");
}
Cancel User A's query permissions
UserA = UserA & (~r);
if ((UserA & r) = = r) {
System.out.println ("User A has query permission");
} else {
System.out.println ("User A does not have query permission");
}
}
}
This article is from the "Trinidad Search" blog, please be sure to keep this source http://5890483.blog.51cto.com/5880483/1690749
Permission Management with Binary