It seems you are confusing 2 different issues: The encoding of something and the meaning of something. Here is an example: You see a number 97. This is a decimal encoding. But the meaning of this number is something completely different. It can denote the ASCII 'a' character, a very hot temperature, a geometrical angle in a triangle, etc. You cannot deduce meaning from encoding. Someone must supply a context to you (like the ASCII map, temperature etc).
Back to your question: 0x80000000
is encoding. While INT_MIN
is meaning. There are not interchangeable and not comparable. On a specific hardware in some contexts they might be equal just like 97 and 'a' are equal in the ASCII context.
Compiler warns you about ambiguity in the meaning, not in the encoding. One way to give meaning to a specific encoding is the casting operator. Like (unsigned short)-17
or (student*)ptr;
On a 32 bits system or 64bits with back compatibility int
and unsigned int
have encoding of 32bits like in 0x80000000
but on 64 bits MIN_INT
would not be equal to this number.
Anyway - the answer to your question: in order to remove the warning you must give identical context to both left and right expressions of the comparison.
You can do it in many ways. For example:
(unsigned int)a == (unsigned int)0x80000000
or (__int64)a == (__int64)0x80000000
or even a crazy (char *)a == (char *)0x80000000
or any other way as long as you maintain the following rules:
- You don't demote the encoding (do not reduce the amount of bits it requires). Like
(char)a == (char)0x80000000
is incorrect because you demote 32 bits into 8 bits
- You must give both the left side and the right side of the == operator the same context. Like
(char *)a == (unsigned short)0x80000000
is incorrect an will yield an error/warning.
I want to give you another example of how crucial is the difference between encoding and meaning. Look at the code
char a = -7;
bool b = (a==-7) ? true : false;
What is the result of 'b'
? The answer will shock you: it is undefined.
Some compilers (typically Microsoft visual studio) will compile a program that b will get true
while on Android NDK compilers b will get false
.
The reason is that Android NDK treats 'char
' type as 'unsigned char
', while Visual studio treats 'char
' as 'signed char
'. So on Android phones the encoding of -7 actually has a meaning of 249 and is not equal to the meaning of (int)-7.
The correct way to fix this problem is to specifically define 'a' as signed char:
signed char a = -7;
bool b = (a==-7) ? true : false;