This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C Code / CCS

Other Parts Discussed in Thread: TMS320C5515

When running this simple program in CCS4.2:

#define BASELINE_MEAN 2000
uint16 sample = 1997;
int32 res;
res = sample - BASELINE_MEAN);
In result I receive res 0x8FFFFFFD

While running in other complier I receive C standard result: res -3

Can you please explain why we don't receive res -3?

Thanks,

Yaron

 

 

 

 

  • What device are you building the code for?

  • Are you absolutely sure you get 0x8ffffffd?  The correct answer (for C5500) is 0xfffd (65533), because "int" is 16 bits.

    The constant 2000 fits in a signed int, so the compiler treats it as a signed int.  The expression "sample - BASELINE_MEAN" is then "uint16 - int".  It doesn't matter whether "uint16" is "unsigned short" or "unsigned int", because they are both the same size.  The C standard requires that when evaluating an expression "unsigned int - int", since the types don't match, the "int" value gets converted to "unsigned int", and the subtraction proceeds as an unsigned subtraction and you get the unsigned number 0xfffd.  This value fits in "int32", so res gets the signed value 65533.

    On architectures where "int" is 32 bits, the expression is "uint16 - int", and the C standard requires that the "uint16" be converted to "int" before evaluation.  Thus, you get "int - int", and the result is the signed value -3.  This value fits in "int32" so res gets the signed value -3.

    To avoid getting different results on different architectures, use this expression:

    res = (int32)sample - BASELINE_MEAN;