+ 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);
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
+ 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/
+ 1
thank you