Ref:unsigned int 2 ulong type promotion issue in GCC!

Thamilarasu Kandasamy (thamil) thamil@cisco.com
Sun May 24 13:31:19 GMT 2020


Hi

I am observing a unsigned int to unsigned long type promotion issue which I described with a sample program.

gcc version 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)

The output of program is here


Result with signed   int:  0x7ffee7fd3b0c      --> int to unsigned long type promotion happens

Result with unsigned int:  0xe7fd3b0c          --> in the expression involving unsigned int and long, there is no type promotion, hence truncated output

Result with type casting:  0x7ffee7fd3b0c.     --> Type casting unsigned int to unsigned long produces result as expected



In the expression involving int & ulong operands, promotion of int to ulong happen as expected.

But the same doesn’t happen in expression involving uint and ulong.

If there is an explicit cast of unsigned to ulong, the result is  as expected.



As per the type promotion rules, unsigned int to unsigned long promotion should have happened.



Will appreciate your input on whether this behaviour is as expected or a bug ? Or am I missing something here ?



#include  <stdio.h>



int main(int argc, char *argv[])

{

    int var;



    void *result1;

    void *result2;

    void *result3;



    int size = 100;

    unsigned int usize = 100;



    result1 = (void *) (((uintptr_t) &var) & ~(size - 1));

    result2 = (void *) (((uintptr_t) &var) & ~(usize - 1));

    result3 = (void *) (((uintptr_t) &var) & ~((unsigned long  int)usize - 1));



    printf("\nResult with signed   int:  %p", result1);

    printf("\nResult with unsigned int:  %p", result2);

    printf("\nResult with type casting:  %p", result3);



    printf("\n");



    return (0);

}





More information about the Gcc-help mailing list