Rohit Garg <rpg.314@xxxxxxxxx> writes: > 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. > > #include<iostream> > #include<cstdint> > 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)) > { > cout<<"k="<<k<<endl; > } > } > > return 0; > } The number 1 has type int, which on your system is probably 32 bits. In C, using a shift which is larger than the size of the integer is undefined behaviour. When a program uses undefined behaviour, the results are often surprising and counter intuitive. When you use ONE, with type int64_t, the value has 64 bits and all the shifts are well defined. Ian