I’m currently reading an article , but I do not really understand how this work with the logical operator. Can anyone explain this to me?
eg. If I want to have 4 level securities with customer, employee, supervisor and Admin.
[Serializable]
[Flags]
public enum WebRoles
{
customer= 1 << 0,
employee= 1 << 1,
supervisor = 1 << 2,
Admin = 2 << 3
}
and then how I should implement the following logic.
if (Roles != 0 && ((Roles & role) != role))
return false;
Can anyone provide me some knowledge of this implementation?
Thank you very much.
Daoming
This example uses the bitwise shift operator: “<<“. This operator takes the bits and shifts them. For example, “1 << 3” results in the number 8. So, in binary,
Now, you can assign people multiple roles using the bitwise-or operator. This would be a single vertical-bar “|”. The bitwise or combines the two numbers bit-by-bit, setting each bit that is set in either of the two operands.
The if-statement you have is intended to tell whether someone has a particular role. It uses bitwise-and: “&”. Bitwise-and combines the two numbers, setting a bit only if the bit is set in both the operands.