With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Ah hello.

I have been reading about comparing signed and unsigned integers on https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules. This page gives a simple example:

The output is 0, not 1 as might be initially believed, as the int s1 is converted to an unsigned int with value UINT_MAX.

This is something to do with conversion rules and integer ranking; I roughly understand the conversion rules ("Usual Arithmetic Conversions" section on the link) - and believe that rule #3 is being used:

*If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type.*

...but I don't understand the ranking: clearly for the conversion above to take place the unsigned value has been given a greater rank than the signed value but where is it specified that this will happen??

Can someone please clarify ?

(Sorry if I have missed it from the above link but I have spent a long time looking at this which might be to blame:))

I have been reading about comparing signed and unsigned integers on https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules. This page gives a simple example:

```
int si = -1;
unsigned int ui = 1;
printf("%d\n", si < ui);
```

The output is 0, not 1 as might be initially believed, as the int s1 is converted to an unsigned int with value UINT_MAX.

This is something to do with conversion rules and integer ranking; I roughly understand the conversion rules ("Usual Arithmetic Conversions" section on the link) - and believe that rule #3 is being used:

...but I don't understand the ranking: clearly for the conversion above to take place the unsigned value has been given a greater rank than the signed value but where is it specified that this will happen??

Can someone please clarify ?

(Sorry if I have missed it from the above link but I have spent a long time looking at this which might be to blame:))

actually signed and unsigned integers (of same size) were not really "converted" for compare operations but "reinterpreted", what means that the highest (most significant) bit either was interpreted as sign bit or not.

because of that you could do like

```
unsigned int ui = -1; // ui is MAX_UINT
int i = -1;
assert(ui == i); // expression is true
```

though the same code would be much better readable if you do:

```
unsigned int ui = (unsigned int)(-1); // ui is MAX_UINT
int i = -1;
assert((int)ui == i); // expression is true
```

generally in such cases the direction of the cast should follow the "nature" of the value. so in case of a negative number you should cast the "unsigned" to "signed" and not vice versa.

Sara

Woah...time out there Sara!

I am not sure why your first example works: if ui is MAX_UINT then how on earth is that equal to -1; is it because the -1 is being converted to the unsigned int equivalent which is also MAX_UINT?? And if this is true, is it because of the second rule that jkr quoted above: "...the operand with signed integer type is converted to the type of the operand with unsigned integer type"

>> what means that the highest (most significant) bit either was interpreted as sign bit or not

That is really interesting; could you possible elaboate with an example?

>> ...so in case of a negative number you should cast the "unsigned" to "signed" and not vice versa

How come you are casting the uint ui to an int then - or have I misinterpreted your comment??

jkr/Sara:

From the quote jkr made:

"The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any"

Does "corresponding signed integer type" mean "the same numeric value", so "(unsigned int)-1" has the corresponding integer type as "(int)-1". but not the same corresponding integer type as "(int)-2"? In this case, what rule determines which of (unsigned int)-1 and (int)-2 has the highest rank?

Thanks very much in advance both!

all negative numbers of a signed integer have the highest bit (bit 31 for 'int' and 'unsigned int') set to 1. the highest bit of 0 ... 31 is also called the most significant bit as it is responsible for the highest range of numbers from 2^31 to 2^32-1. the 2^31-1 (hex: 0x7FFFFFFF, decimal: 2147483647) is the highest number of a signed integer. if you add 1 to that number you would get -2^31 (decimal -2147483648) for 'int' variable and 2^31 (decimal ) for unsigned int. for both the hex value is 0x80000000 or binary 10000000000000000000000000

summary: for 'signed' and 'unsigned' integers with the same bit count we have the same bit combinations which were "interpreted" differently when the highest bit was set.

```
"signed"
|---------------|+++++++++++++|
-2^31 ..... -1 0 ..... 2^31-1 ..... 2^32-1
|++++++++++++|+++++++++++++|
"unsigned"
```

for a signed integer the second part of the unsigned numbers would be interpreted as the negative numbers without any conversion (all bits keep their value).

>> ...so in case of a negative number you should cast the "unsigned" to "signed" and not vice versa

How come you are casting the uint ui to an int then - or have I misinterpreted your comment??

my statement was: if you compare an signed integer with an unsigned integer you could either do a cast on the signed integer or you could cast the unsigned integer to signed. both

cases are equivalent and would make the comparison better readable as now both operands have same type. but if the signed integer could have a negative number - for example because it was a difference - you better do the cast from unsigned to signed and not vice versa such that the "nature" of the value was not changed. in the debugger both sides would show for example -4 rather than 4294967292 or 0xFFFFFFFB then.

Sara

All Courses

From novice to tech pro — start learning today.

There you'll find the statement:

which directly correlates to

and therefore makes that rule apply.