In my codes, I got a strange result following. I compilerd by C51 9.03, and C51 9.60 is as the same result. That doesn't fit my logic. In the comment after the code is the value obtained when debugging
unsigned char aa,bb,cc,dd;aa = 0xab;bb = 0xcd;cc = (aa+bb)%255; //cc=0x79dd = (unsigned char)((aa+bb)%255); //dd=0x78
The following is the disassembly code. The right is the option of C51.
In fact, cc and dd should be 0x79. I use parentheses to clearly indicate the operation priority order: first calculate the sum, calculate the modulus, and then cast to unsigned char.
I tried in VC2010, TI CCS3.3, both can get the correct result 0x79, that is to say, cc=0x79 and dd=0x79.
Thank you for your attention.
Just for your information: This issue is now solved with C51 compiler version 9.60.2. This version (or newer) will be part of PK51 version 9.61 (or later), but the date of this next PK51 release is not scheduled yet. If the support period of your license is not expired and you want to receive C51 version 9.60.2, please raise a support case and refer to this forum post.