+ 1

i cant understand the line x=x<<2 in this code. Can someone help me out ?

int x=12; x=x<<2; Console.Write(x);

1st Sep 2020, 12:51 AM
Thắng Phạm
Thắng Phạm - avatar
3 Answers
+ 2
You can simply learn like this: x << n means the same as x * 2**n So, x<<1 => x*2 x<<2 => x*4 x<<3 => x*8 12<<3 => 12*8 = 96 On the other hand there is a right shift operator too! x >> n means the same as (int) x / 2**n x >> 1 => (int) x / 2 12 >> 3 => (int) 12/8 => 1 3 >> 2 => (int) 3/4 => 0 Refer to here: https://www.sololearn.com/learn/4087/?ref=app
1st Sep 2020, 3:42 AM
Namit Jain
Namit Jain - avatar
+ 3
Here "<<" is bitwise left shift operator. This operator works with binary representation of "x" and shift the bits of "x" to the left. Methematically this have Similar effect as of multiplying the number with some power of two. For more info about bitwise operators visit here 👇 https://www.geeksforgeeks.org/bitwise-operators-in-java/
1st Sep 2020, 1:03 AM
Arsenic
Arsenic - avatar
+ 1
thank you
1st Sep 2020, 1:12 AM
Thắng Phạm
Thắng Phạm - avatar