+ 2

Why cout << (1<<1); print 2?

cout << (1<<1); when run the output is: 2

27th Apr 2017, 3:52 AM
Yusuf Malikul Mulki
Yusuf Malikul Mulki - avatar
5 Antworten
+ 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 )
27th Apr 2017, 4:38 AM
visph
visph - avatar
+ 8
specifically a bitwise operator. here. this might help. http://www.cplusplus.com/doc/tutorial/operators/
27th Apr 2017, 4:08 AM
jay
jay - avatar
+ 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
27th Apr 2017, 4:18 AM
Nithiwat
Nithiwat - avatar
+ 5
<< is an operator.
27th Apr 2017, 3:55 AM
Nithiwat
Nithiwat - avatar
+ 3
@Nithiwat I found that << is left shift and >> is right shift.
27th Apr 2017, 4:12 AM
Yusuf Malikul Mulki
Yusuf Malikul Mulki - avatar