? operator warning

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

? operator warning

484 Views
markulhu
Contributor II

signed char X1,X2,X3;

X3 = (X1 > X2)? 127: -128;

When i compile this code, the compiler display warning message said "possible lost of data".

But compile the code "X3 = (X1 > X2)? 127: -127;" is ok.

 

So do that "signed short" with -32768, and "signed long" with -2147483648

 

Why? Is this a compiler's Bug?

Labels (1)
0 Kudos
2 Replies

433 Views
BlackNight
NXP Employee
NXP Employee

the compiler is a little bit over-cautious here: -128 gets promoted to int, which is 0xffff'8000 (as 16bit int), and then truncated back to signed char with the assignment.

So the compiler warns about these uppper 8bits lost (which are not really an issue).

You can get rid of that warning with a cast like this:

X3 = (signed char)((X1 > X2)? 127: -128);

Erich

433 Views
markulhu
Contributor II

Thank you Erich Styger!

Today,i tring to compile code "signed long X1 = -2147483648"(0x8000 0000), the complier warning: Unary minus operator appiled to unsigned type.

just "signed long X1 = 0x80000000"is the right way?

How about your idea?

0 Kudos