g ++说:warning:语句对移位操作符没有影响

时间:2014-03-26 18:08:30

标签: c++ g++ bit-shift

我实施了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;
}

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

1 个答案:

答案 0 :(得分:14)

 y << 1;

应该是

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

以下声明中的相同问题:

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