gpt4 book ai didi

c++ - g++ 表示 : warning: statement has no effect for shift bits operators

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:48 25 4
gpt4 key购买 nike

我正在实现 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;

关于c++ - g++ 表示 : warning: statement has no effect for shift bits operators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22669533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com