问题
I'm having trouble understanding how does comparing two ints, where one is unsigned int32 and the other one signed int32 work. Let's consider this simple program:
#include <stdint.h>
int main()
{
uint32_t a1 = UINT32_MAX;
int32_t b1 = (int32_t)a1;
if (a1 == b1)
printf("Equal");
else
printf("Not equal");
return 0;
}
In this case, a1
exceeds a signed 32-bit integer range, so as I have confirmed while debugging, after it's been casted, b1
equals -1
.
However it still prints "Equal", while those values obviously aren't the same. What is it caused by?
回答1:
An out-of-range conversion to a signed integer type, as you are doing, is implementation defined.
On most implementations you're likely to come across, converting the max value for a uint32_t
to a int32_t
means retaining the bit pattern and treating it as a signed value. This means that b1
gets assigned the value -1.
When you then compare a1
and b1
, the usual arithmetic conversions apply. These are spelled out in section 6.3.1.8 of the C standard:
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned
integer type corresponding to the type of the operand with signed integer type
The highlighted portion is what applies in this case, since uint32_t
and int32_t
have the same rank, so the value of b1
is converted to type uint32_t
.
When converting an out-of-range value for an unsigned type, this is accomplished by numerically adding or subtracting one more that the max value of the unsigned type repeatedly until the value is in range. This effectively means that any excess bytes of the source value are truncated and what is left is treated as an unsigned value.
This conversion is spelled out in section 6.3.1.3 of the C standard:
1 When a value with integer type is converted to another integer type other than
_Bool
, if the value can be represented by the new type, it is unchanged.2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised
In this case, paragraph 3 applies when you first assign a1
to b1
, and paragraph 2 then applies when you do the comparison and b1
is converted. So that means that the value -1 gets converted to the value UINT32_MAX
, which is why the comparison evaluates to true.
回答2:
In general, unsigned-to-signed conversions are implementation defined (6.3.1.3) as of now (this may change in future versions of the C standard).
In practice, the integers will be two's complement and conversions in either direction will be a no-op—the same data will just be interpreted differently in accordance with how two's complement arithmetic works.
The equality in your case is caused by the signed b1
getting semantically converted to an a1
's unsigned type in the comparison due to usual arithmetic conversions (6.3.1.8).
回答3:
The builtin ==
can only compare values of the same type. If the types of the operands are different, they are converted to a same type beforehand. (See cppreference for how this type is chosen.)
In this case, b1
is converted to uint32_t
before the comparsion is performed.
来源:https://stackoverflow.com/questions/58828063/casting-uint32-t-to-int32-t-and-comparing-them-afterwards