What does "<<" mean in C #?

What does <<do in this piece of code?

 [Serializable] [Flags] public enum SiteRoles { User = 1 << 0, Admin = 1 << 1, Helpdesk = 1 << 2 } 
+2
source share
5 answers
+7
source

This means the bitshift is on the left, like this:

 int i = 1 << 2; // 0000 0001 (1) // shifted left twice // 0000 0100 (4) 

The left bit shift is similar to multiplying by two, and the right bit shift acts as division by two.

Bitshifts are useful because they convey better semantics when working with bitmasks, and they (at least x86) are faster than multiplication

+7
source

Bitshifting Like in C ++

+5
source

This is a bit shift.

Admin = 1 << 1 means that one binary value moves left one bit.

Result

Admin = 2

-2
source

Source: https://habr.com/ru/post/917814/


All Articles