I have a code which uses comparison of 64-bit integers. It looks similar to the following:
#include <cstdio>
long long getResult()
{
return 123456LL;
}
int main()
{
long long result = getResult();
if (result > 0x000FFFFFFFFFFFFFLL
|| result < 0xFFF0000000000000LL)
{
printf("Something is wrong.\n");
if (result > 0x000FFFFFFFFFFFFFLL
|| result < -4503599627370496LL)
{
printf("Additional check failed too.\n");
}
else
{
printf("Additional check went fine.\n");
}
}
else
{
printf("Everything is fine.\n");
}
return 0;
}
When this code is compiled in g++ (tried different versions on Ubuntu 12.04 x64: 4.6.3, 4.6.4, 4.7.3, 4.8.0) with flags -Wall -pedantic -std=c++0x test.cpp -o test I get -Wsign-compare warning for second line of the first if statement (output from g++-4.8):
test.cpp:13:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
|| result < 0xFFF0000000000000LL)
^
And when the test program is run I get two lines of text:
Something is wrong.
Additional check went fine.
When compiling same code on Windows using MS Visual Studio 11 Express Update 2 with default project options for either x86 or x64 architecture I don't get neither the warning nor this output, instead the output is:
Everything is fine.
Is it a problem in the code? If yes, could you point it? Or is it a problem with the compiler used?
Adding additional type cast for the second constant in the first if statement removes the warning in g++.