开发者

What does "<<" stand for in C#? [duplicate]

This question already has answers here: What does the operator "<<" mean in C#? (9 answ开发者_JAVA技巧ers) Closed 7 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜