2

What does << do in this piece of code?

[Serializable]
[Flags]
public enum SiteRoles
{
    User = 1 << 0,
    Admin = 1 << 1,
    Helpdesk = 1 << 2
}
Lasse Edsvik
  • 9,070
  • 16
  • 73
  • 109

5 Answers5

8

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

7

Bitwise shifting.

unwind
  • 391,730
  • 64
  • 469
  • 606
5

Bitshifting Just like in C++

RvdK
  • 19,580
  • 4
  • 64
  • 107
1

Bitwise Shifting

AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
1

It is a Bitwise shift.

Admin = 1 << 1 means one's binary value move to left one bit.

The result is

Admin = 2

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
carl
  • 307
  • 2
  • 5
  • 13