cstdint

Casting uint32_t to int32_t and comparing them afterwards


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?


Solution

  • 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.