开发者

Bit shifting number gives wrong output `std::cout`

unsigned int command = 4;
cout << command;

command = (command << 1);
cout << command;

command 开发者_开发百科= (command << 1);
cout << command;

Output:

4
8
10

Why is is the output of the last line 10, and not 16?


There's probably a cout << hex somewhere before this code runs. Either that or you accidentally set cout to format numbers in hexadecimal. If you add:

command = (command<<1);
cout << command;

It should print out 20 in hex mode.


10 is hexadecimal for 16.

0x10 == 16

Sorry if I am stating the obvious.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜