What does << do in this piece of code?
[Serializable]
[Flags]
public enum SiteRoles
{
User = 1 << 0,
Admin = 1 << 1,
Helpdesk = 1 << 2
}
It means bitshift left, so:
int i = 1 << 2;
// 0000 0001 (1)
// shifted left twice
// 0000 0100 (4)
A left bitshift is analogous to multiplying by two, and a right bitshift acts as a divide by two.
Bitshifts are useful because they convey semantics better when working with bitmasks and they are (on x86 at least) faster than multiplication
Bitwise shifting.
Bitshifting Just like in C++
Bitwise Shifting
It is a Bitwise shift.
Admin = 1 << 1
means one's binary value move to left one bit.
The result is
Admin = 2
精彩评论