+ 2
Why cout << (1<<1); print 2?
cout << (1<<1); when run the output is: 2
5 ответов
+ 11
@Nithiwhat:
You've inverted left and right shift: left multiply and right divide ;P ( think to binary: 00000010 left shift is 00000100, right shift is 00000001 )
+ 8
specifically a bitwise operator.
here. this might help.
http://www.cplusplus.com/doc/tutorial/operators/
+ 8
Left shift (<<) operator
Solution:
x << y = x/power(2,y)
1<<1 = 1/power(2,1) = 0
Right shift (>>) operator
Solution:
x >> y = x*power(2,y)
1>>1 = 1*power(2,1) = 2
+ 5
<< is an operator.
+ 3
@Nithiwat
I found that << is left shift and >> is right shift.