Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is conversion from unsigned to signed undefined?

Tags:

c

void fun(){
    signed int a=-5;
    unsigned int b=-5;
    printf("the value of b is %u\n",b);
    if(a==b)
         printf("same\n");
    else
         printf("diff");
}

It is printing :

4294967291

same

In the 2nd line signed value is converted to unsigned value. So b has the value UINTMAX + 1 - 5 = 4294967291.

My question is what is happening in the comparison operation .

1) Is a again converted to unsigned and compared with b ?

2) Will b(ie unsigned ) be ever casted to signed value and compared automatically?

3) Is conversion from unsigned to signed undefined due to int overflow ?

I have read other posts on the topic. I just want clarification on questions 2 and 3 .

like image 516
hackrock Avatar asked Feb 29 '12 11:02

hackrock


People also ask

How do you convert an unsigned number to a signed number?

To convert a signed integer to an unsigned integer, or to convert an unsigned integer to a signed integer you need only use a cast. For example: int a = 6; unsigned int b; int c; b = (unsigned int)a; c = (int)b; Actually in many cases you can dispense with the cast.

Is unsigned overflow undefined?

-fsanitize=unsigned-integer-overflow : Unsigned integer overflow, where the result of an unsigned integer computation cannot be represented in its type. Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional.

What happens when you cast a signed int to an unsigned int?

If you mix signed and unsigned int, the signed int will be converted to unsigned (which means a large positive number if the signed int has a negative value).

Is it safe to cast signed to unsigned?

When one unsigned and one signed variable are added (or any binary operation) both are implicitly converted to unsigned, which would in this case result in a huge result. So it is safe in the sense of that the result might be huge and wrong, but it will never crash.


2 Answers

1) Is a again converted to unsigned and compared with b ?

Yes. In the expression (a==b), the implicit type conversion called "balancing" takes place (the formal name is "the usual arithmetic conversions"). Balancing rules specify that if a signed and a unsigned operand of the same size and type are compared, the signed operand is converted to a unsigned.

2) Will b(ie unsigned ) be ever casted to signed value and compared automatically?

No, it will never be converted to signed in your example.

3) Is conversion from unsigned to signed undefined due to int overflow ?

This is what the standard says: (C11)

6.3.1.3 Signed and unsigned integers

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 other words, if the compiler can manage to do the conversion in 2) above, then the behavior is well-defined. If it cannot, then the result depends on the compiler implementation.

It is not undefined behavior.

like image 79
Lundin Avatar answered Oct 02 '22 15:10

Lundin


Answers:

  1. a is converted to unsigned int.
  2. If a had a wider range than the signed counterpart of b (I can imagine long long a would do), b would be converted to a signed type.
  3. If an unsigned value can't be correctly represented after conversion to a signed type, you'll have implementation-defined behavior. If it can, no problem.
like image 33
Alexey Frunze Avatar answered Oct 02 '22 16:10

Alexey Frunze