Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

bit-shift

g++

I'm implementing the alkhwarizmi algorithm.
It's right but my g++ compiler doesn't like the shift operators: >> and << or I'm doing something wrong.
When I compile it, I get this output:

> 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.

Here is my code:

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;
}

if I use the commented statements (straightforward multiplication and division), everything works fine.

like image 324
Akronix Avatar asked Mar 26 '14 18:03

Akronix


1 Answers

 y << 1;

should be

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

Same issue on the statement below:

x >>1; //^^same issue should be x >>= 1;
like image 171
taocp Avatar answered Oct 05 '22 23:10

taocp