Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting uint32_t to int32_t and comparing them afterwards

Tags:

c

stdint

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?

like image 856
MLapaj Avatar asked Nov 12 '19 23:11

MLapaj


People also ask

What is the meaning of uint32_t?

uint32_t is a numeric type that guarantees 32 bits. The value is unsigned, meaning that the range of values goes from 0 to 232 - 1. This. uint32_t* ptr; declares a pointer of type uint32_t* , but the pointer is uninitialized, that is, the pointer does not point to anywhere in particular.

How many bits is uint32_t?

A 32-bit unsigned integer value type.

Is unsigned the same as uint32_t?

unsigned int is an unsigned integer of a system defined size but probably 32 bits. uint32_t is an unsigned integer of 32 bits. Probably the same as unsigned int but not guaranteed to be so. size_t is the type used to specify the size of memory allocations and the underlying type for indexes in the standard library.


1 Answers

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.

like image 165
dbush Avatar answered Sep 28 '22 00:09

dbush