We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I encountered some unexpected results when porting some older code involving the way armcc appears to handle types. Note that uint16_t is an "unsigned short".
In compilers I'm familiar with, if you do the following:
uint16_t x = 0x0001
uint16_t y = 0x0002
then the following IF statement will be evaluated as true:
if((x-y) > 0x8000)
This is because the subtraction of 2 uint16_t variables returns a uint16_t result of 0xFFFF.
When compiling and running the test code below with armcc:
uint16_t x; uint16_t y; uint16_t z1[2]; bool result[8]; x = 0x0001; y = 0x0002; z1[0] = x - y; z1[1] = (uint16_t)(x - y); if((x-y) > 0x8000) result[0] = 1; if((uint16_t)(x-y) > 0x8000) result[1] = 1; if((x-y) > (uint16_t)(0x8000)) result[2] = 1; if((uint16_t)(x-y) > (uint16_t)(0x8000)) result[3] = 1; if(z1[0] > 0x8000) result[4] = 1; if((uint16_t)(z1[0]) > 0x8000) result[5] = 1; if(z1[0] > (uint16_t)(0x8000)) result[6] = 1; if((uint16_t)(z1[0]) > (uint16_t)(0x8000)) result[7] = 1;
I get the following results:
z1[0] is 0xFFFF
result[0] is 0
result[1] is 1
result[2] is 0
result[3] is 1
result[4] is 1
result[5] is 1
result[6] is 1
result[7] is 1
Why do I have to typecast the result of subtracting two uint16_t variables as uint16_t for the if statement to evaluate as true? Is there some place in the armcc documentation that explains how the compiler handles these evaluations (promotions, automatic typecasting, etc)?