开发者

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

开发者 https://www.devze.com 2023-04-04 01:50 出处:网络
unsigned int command = 4; cout << command; command = (command << 1); cout << command; command 开发者_开发百科= (command << 1);
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

精彩评论

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