Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator << in c# [duplicate]

Tags:

operators

c#

i couldn't understand this code in c#

int i=4 
int[] s =new int [1<<i]; 
Console.WriteLine(s.length); 

the ouput is 16 i don't know why the output like that?

like image 332
khtaby Avatar asked Dec 07 '22 05:12

khtaby


2 Answers

bit shift operator

like image 160
Dustin Getz Avatar answered Dec 10 '22 03:12

Dustin Getz


From documentation

If first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of second operand.

If first operand is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of second operand.

Note that i<<1 and i<<33 give the same result, because 1 and 33 have the same low-order five bits.

This will be the same as 2^( the actual value of the lower 5 bits ).

So in your case it would be 2^4=16.

like image 42
Adriaan Stander Avatar answered Dec 10 '22 02:12

Adriaan Stander