为什么将此位evalue移位到51

Kev*_*oen 6 c++ bit-shift bitwise-operators

我正在学习C++考试.实践考试中的一个问题是:

这个陈述的结果是什么?

cout <<(11>>1)<<1<<endl;
Run Code Online (Sandbox Code Playgroud)

照我看来.11保持二进制当量

1011.
Run Code Online (Sandbox Code Playgroud)

将此二进制数向右移1位使其成为:

0101
Run Code Online (Sandbox Code Playgroud)

然后将第一个向左移动就可以了

1010 
Run Code Online (Sandbox Code Playgroud)

评估为10.

但是,通过在我的编译器中运行相同的语句,它会将数字评估为51.有人可以向我解释这个吗?

Tar*_*ama 13

这是由于运营商超载.

cout <<(11>>1)<<1<<endl;
//   ^ output operator
//        ^ right shift
//            ^ output operator
Run Code Online (Sandbox Code Playgroud)

如果您要将代码更改为此,那么您的答案将是正确的:

cout << ((11>>1) << 1) <<endl;
// brackets force left shift operator instead of output
Run Code Online (Sandbox Code Playgroud)


Nei*_*irk 6

cout << (11>>1) << 1 << endl;

cout << 5 << 1 <<endl;

流式传输意义<<优先于移位意义.因此它打印5后跟1.