I've been looking at this piece of code for an hour now... I desperately try to find the error.
#include <iostream>
int main()
{
for (int32_t n = 1; n>= 0; n--)
{
std::cout << "n: " << n << std::endl;
std::cout << std::dec << " 64 - n: " << (64 - n) << std::endl;
std::cout << std::hex << " 0x1ULL << 64: " << (0x1ULL << 64) << std::endl;
std::cout << std::hex << " 0x1ULL << (64 - n): " << (0x1ULL << (64 - n)) << std::endl;
}
}
And that's the output:
n: 1
64 - n: 63
0x1ULL << 64: 0
0x1ULL << (64 - n): 8000000000000000
n: 0
64 - n: 64
0x1ULL << 64: 0
0x1ULL << (64 - n): 1
Why is (0x1ULL << 64) different from (0x1ULL << (64 - n)) for n = 0? The type of n does not seem to matter.
It's the same with each version of GCC. Feel free to try out online: http://www.compileonline.com/compile_cpp11_online.php