Bit shift compiler bug or a corner case?

Rohit Garg
Thu Nov 24 07:55:00 GMT 2011


The following code outputs 0,1,32,33. Which is counter intuitive to
say the least. But if I replace the literal 1 with the type annonated
constant "ONE", the loop runs fine and outputs 0 and 1, which is what
I would expect.

This is with gcc 4.6.2 and -std=c++0x.

using namespace std;
int main()
    int64_t bitmask = 3;
    int64_t k;
    const int64_t ONE = 1;
    cout<<"bitmask = "<<bitmask<<endl;

    for(k=0; k<64; k++)
        if(bitmask & (1<<k))

    return 0;

Any help will be greatly appreciated.

Thanks and Regards,
Rohit Garg

Graduate Student
Applied and Engineering Physics
Cornell University

More information about the Gcc-help mailing list