位移数字输出错误的输出`std :: cout`

Ros*_*s W 0 c++ bit-shift

unsigned int command = 4;
cout << command;

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

command = (command << 1);
cout << command;
Run Code Online (Sandbox Code Playgroud)

输出:

4
8
10
Run Code Online (Sandbox Code Playgroud)

为什么是最后一行的输出10,而不是16

MSN*_*MSN 19

cout << hex在此代码运行之前可能存在某个地方.或者您不小心设置cout为以十六进制格式化数字.如果你添加:

command = (command<<1);
cout << command;
Run Code Online (Sandbox Code Playgroud)

它应该20以十六进制模式打印出来.

  • 是的,就是这样 - 该死的很明显.星期五5点以后永远不应该工作 (3认同)