The application of user rights in practical applications what I'm talking about here is a common approach to the rights management approach, which is to use the "bit run" action,& bit and operator, bit, or run character.
If the participating operation is a 10 number, it will be converted to the 2 binary number to participate in the operation, and then the result will be converted to the output of 10.
Its permission value is such a 2^0=1, the corresponding 2 number is "0001″" (here ^ I am expressed as "second party", that is: 2 of 0 times, same below) 2^1=2, the corresponding 2 into "0010″2^2=4, corresponding 2 into" 0100″2^3=8, corresponding 2 in the number of "1000″" To determine that a number can be used in some range of the & operator (value from the table above) such as: 7=421 (You can also simply understand as 7=4+2+1) with & to operate, you can know 7&4, 7&2, 7&1 are true, and if 7
&8 is a fake &, unfamiliar to look up the manual, to see how the use of the following example://give permission value--> Delete: 8, Upload: 4, write: 2, read only: 1 define ("Mdelete", 8);
Define ("Mupload", 4);
Define ("Mwrite", 2);
Define ("Mread", 1); VVVVVVVVVVVVV Use description VVVVVVVVVVVVV//Department manager's permissions for (assuming that it has all the authority of this department), is a bit or run character, unfamiliar to check the information echo mdeletemuploadmwritemread, ""; The equivalent is to add up the above permission value: 8+4+2+1=15//Set I only upload and Read permission, then echo Muploadmread, "";//equivalent to the upload, read-only permission values are added: 4+1=5/* Give it multiple permissions on the respective access values
Added, another example is that an employee has a permission other than the deletion, what is its permission value?
* Should be: 4+2+1=7 * Understand how to assign a value to the right? * *//^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ the ^^ ^^ the ^^ ^^ the ^^ the ^^ of a person's permission is available, set the 13;//13=8+4+1 value in the $key/* * * * to judge the permission with the & Bit and the character, * * ^^ = if (^^ & M Delete) echo "has deletion permission";
8 if ($key & Mupload) echo has upload permission“; 4 $a = $key & mwrite; echo "has Write permission". $a; No this permission if ($key & Mread) echo "Read permission"; 1?> OK, the privilege value of this one of these algorithms is such, can be said to be simple and efficient. I do not know if you understand that there is no, do not understand it does not matter, remember the example on the line. The premise is to do a good job of the distribution of the values, that is, 1, 2, 4, 8, 16 ... (there is also an order problem, the more advanced permissions will be higher permission values, such as the above example to demonstrate the deletion of permissions).
With the Permission distribution table can determine what permissions to a person, you simply understand what permissions to add the corresponding permissions value it. This method is very easy to use, the disadvantage is that if the right distribution is thin, then the permission value will be more and more, you think about it, 2 of times, if all the permissions are added.
However, this is sufficient for general permissions. Here are some simple application examples (1) to determine whether an int variable A is odd or even a&1 = 0 even a&1 = 1 odd (2) take the K bit (k=0,1,2......sizeof (int)) of the int variable A, i.e. a>>k& 1 (3) The K-position of the int variable A is 0, namely a=a&~ (1< <> (4), to the K location 1 of the int variable A, that is, the A=a (1< <> (5) int type variable is shifted to the left K times, that is, a=a<>1 6-k (set sizeof (int) =16) (6) INT-type variable a loops right to K-A=a>>ka<<16-k (set sizeof (int) =16) (7) The mean of integers for two integers x,y, if (x+y)/2 Averages, which can overflow because the x+y may be larger than Int_max, but we know that their average value is definitely not going to overflow, we use the following algorithm: int average (int x, int y)//return x,y mean (x&y) + (
(x^y) >>1); (8) to determine whether an integer is a power of 2, for a number x >= 0, to determine whether he is 2 power boolean power2 (int x) {return ((x& (x-1) ==0) && (x!=0);} (9)
Do not exchange two integers with tempvoid swap (int x, int y) {x ^= y;
Y ^= x;
x ^= y;
(10) Calculate the absolute value int abs (int x) {int y;
y = x >> 31; Return (x^y)-y; Or: (X+y) ^y} (11) The modulo operation converts the bitwise operation (without overflow) a% (2^n) equivalent to A & (2^n–1) (12) multiplication operation converts the bitwise operation (without overflow) A * (2^n) equivalent to a<< N (13) Division operations Convert to bitwise operations (in the case of no overflow) A/(2^n) equivalent to a>> N example: 12/8 = = 12>>3 (%) A% 2 is equivalent to A & 1 (1
5 if (x = = a) x= b;
else x= A;
Equivalent to x= a ^ b ^ x; (x) The opposite number is expressed as (~x+1) do not move more than 32 bits to the right on 32-bit systems and do not move left when the result may be more than 32 bits