xuatla wrote:
#include <iostream>
using namespace std;
int main()
{
ULONG a = 1;
LONG b = -1;
cout << (a>b) << "\t" << (a==b) << "\t" << (a<b) << endl;
cout << (b<a) << "\t" << (b==a) << "\t" << (b>a) << endl;
cout << (ULONG)b << endl;
return 1;
}
The output is:
0 0 1
0 0 1
4294967295
That means when I compared LONG with ULONG (no matter which one first,
and for other unsigned data type), both numbers were converted to ULONG
and then compare. This is not what I want. One way I can do is to use
"(LONG)a > b", but is there any other better way handling this king of
problem?
There is no easy way to handle this problem. It is undefined behaviour
to convert a unsigned value to a signed value when the unsigned value is
bigger than the maximum signed value for that type.
You can write code like "(LONG)a > b" and it might do one thing on your
compiler, but you have no guarantee that it will do the same thing on
another compiler.
This seems very strange (and quite wrong) to me but that's just the way
C++ is.
So there is no answer to your problem, just try and avoid it.
John