+ 2
How does it work
cout<<(1<<1) ; //output is 2
4 Respuestas
+ 5
it's just shif-left(<<) operator which shifts the each bit to the left side.
so in this example
1<<1 means shift the bits of 1 ( which is on the left hand side of operator<<)to left side only 1(which is on the right hand side of the operator<<)time so
1<<1
binary of 1 : 0001= decimal(1)
now shifting to left only 1 time : 0010 = decimal (2)
like wise if there is
1 << 2
then
binary of 1 : 0001
shift 2 times left
so 0010 = decimal(2)
0100 = decimal(4)
so ans will be decimal(4)
hope it helps....
+ 5
Omar , you may like to read below link :
https://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work
+ 5
so basically when you do shift left once it multiply the number with 2
and likewise when you right shift a number once it divides the number by 2
+ 2
<< is a left shift operator
left side indicates the number
right side indicates no. of bits to be shifted
1<<1 results in 1 bit shifting 1 position to the left, so it would be 10 which is binary for 2
left shift and right shift works on a binary level.
So 2<<1 would be
10 << 1 = 100 = 4