c++说:warning:语句对移位位操作符没有作用

g++ says: warning: statement has no effect for shift bits operators

本文关键字:操作符 位操作 作用 warning 语句 c++      更新时间:2023-10-16

我正在实现alkhwarizmi算法。
这是对的,但是我的g++编译器不喜欢移位操作符:>>和<<或者我做错了什么。
当我编译它时,我得到这样的输出:

> g++ -Wall  -std=c++0x -o "Al-khwarizmi algorithm.o" "Al-khwarizmi algorithm.cpp" (in directory: /home/akronix/workspace/Algorithms)
> Al-khwarizmi algorithm.cpp: In function ‘int alkhwarizmi(int, int)’: Al-khwarizmi algorithm.cpp:31:9: warning: statement has no effect
> [-Wunused-value] Al-khwarizmi algorithm.cpp:34:9: warning: statement
> has no effect [-Wunused-value] Compilation finished successfully.
下面是我的代码:
int alkhwarizmi(int x, int y)
{
    int sum = 0;
    while (x>0)
    {
        if (x%2)
            sum+=y;
        //y *= 2;
        y << 1;
        cout << y <<endl;
        //x /= 2;
        x >> 1;
        cout << x <<endl;
    }
    return sum;
}

如果我使用注释语句(简单的乘法和除法),一切正常

 y << 1;
应该

 y <<= 1; //^^equals y = y << 1;

下面的语句也是同样的问题:

x >>1; //^^same issue should be x >>= 1;