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
  • 8,638
  • 16
  • 70
  • 107

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
  • 378,987
  • 63
  • 458
  • 590
5

Bitshifting Just like in C++

RvdK
  • 19,128
  • 3
  • 59
  • 105
1

Bitwise Shifting

AutomatedTester
  • 21,840
  • 7
  • 47
  • 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
  • 19,661
  • 10
  • 58
  • 112
carl
  • 297
  • 2
  • 5
  • 13