Read it if you have the patience (the algorithm is still important)
User initial weight: Enable = 0;
If a user has the first power Select, then the user weight: Enable = Enable + 1;
If If a user has the second power Insert, then the user weight is: Enable = Enable + 2;
If a user has the third power Update, then the user weight is: Enable = Enable + 4;
If a user If there is a fourth power, Delete, then the user weight is: Enable = Enable + 8;
Why +1, +2, +4, +8 in order, instead of +1, +2, +3, +4?
If it is +1, +2, +3, +4:
If a user has the first right Select, then the user weight is: Enable = Enable + 1;
If a user has the second If a user has the third right, Insert, then the user’s weight is: Enable = Enable + 2;
If a user has the third right, Update, then the user’s weight is: Enable = Enable + 3;
If a user has the fourth right, Update Delete, then the user weight is: Enable = Enable + 4;
Then when the user weight is: Enable = 3, you cannot judge whether the user has both the
first power Select and the second power Insert. Two rights, or only the third power, Update,
The former weight algorithm can avoid this situation.
But when using the former weight algorithm, how to quickly know which rights the user has
from the user's weight Enable? If the user rights value is Enable = 5, the user has the first power Select and the third power Update.
Note that the user does not have the second power Insert.
I will list the user rights below:
Power Possess Power User's weight Enable
SelectItem: (1, 3, 5, 7, 9, 11, 13, 15)
InsertItem: (2 , 3, 6, 7, 10, 11, 14, 15 )
UpdateItem: ( 4, 5, 6, 7, 12, 13, 14, 15 )
DeleteItem: (8, 9, 10, 11, 12, 13, 14, 15)
It can be seen that users with large Enable weights do not have "more" rights.
If: User Rights Value Enable = 3, the user has the first power (Select) and the second power (Insert), a total of two rights,
User power value Enable = 4, the user has the third power (Update), only one rights.
Look carefully at the "User Power List":
SelectItem: (1, 3, 5, 7, 9, 11, 13, 15): 1 = 0 + 2 raised to the 0th power; 3 = 2 raised to the 1st power Power + 2 to the 0th power; 5 = 2 to the 2nd power + 2 to the 0th power;
InsertItem: (2, 3, 6, 7, 10, 11, 14, 15): 2 = 0 + 2 1 power; 3 = 2 power 0 + 2 power 1; 6 = 2 power 2 + 2 power 1;
UpdateItem: (4, 5, 6, 7, 12, 13 , 14, 15): 4 = 0 + 2 to the power of 2; 5 = 2 to the power of 0 + 2 to the power of 2; 6 = 2 to the power of 2 + 2 to the power of 2;
DeleteItem: ( 8, 9, 10, 11, 12, 13, 14, 15): 8 = 0 + 2 to the 3rd power; 9 = 2 to the 0th power + 2 to the 3rd power 10 = 2 to the 1st power + 2 3rd power;
The rules are summarized as follows:
The weight of the Select user with the first power: Enable = ? + 2 to the 0th power; (where ? must also be divided into 2 and the Nth power added together Situation)
Have the second power Insert user's weight: Enable = ? + 1 power of 2; (where ? must also be split into the sum of N powers of 2)
Have the third power Power Update user’s weight: Enable = ? + 2 to the power of 2; (where ? must also be split into the sum of N powers of 2)
The fourth power of Delete user’s weight: Enable = ? + 2 to the 3rd power; (where ? must also be split into the sum of 2 to the Nth power)
It is already very clear at this point:
As long as the user's weight Enable is split into 2 to N In the case of summation of powers,
if there is N=0 in it, it has the first power Select,
if there is N=1 in it, it has the second power Insert,
if there is N in it =2, then you have the third power Update,
If N=3, you have the fourth power Delete,
Then how to quickly split the user weight Enable into the sum of N powers of 2 What's the situation?
Haha! Just convert Enable into binary and take the base from right to left. If the base is the bit, it has the corresponding rights.
For example:
(11)10=(1011)2, that is, it has the first, second, and The four powers are consistent with the "User Power List";
(12)10=(1100)2, that is, having the third and fourth powers, consistent with the "User Power List";
(15)10= (1111)2, that is, possessing the first, second, third, and fourth powers, consistent with the "user power list";
When the power level is extremely complex, the algorithm can quickly know the user's power:
For example:
A total of 8 levels, user weight Enable=67; (67)10=(1000011)2, that is, it has the first, sixth, and seventh powers,
User weight Enable=67; (159)10 =(10011111)2, that is, it has the first, fourth, fifth, sixth, seventh and eighth powers.
That’s it. I hope I didn’t waste your time and inspired you.
http://www.bkjia.com/PHPjc/532006.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532006.htmlTechArticleRead it if you have the patience (the algorithm is still important) User initial weight: Enable = 0; If a user has the If a power is Select, the user weight is: Enable = Enable + 1; If a user has the...