Hi,
I made a small program to make the problem clear:
==================
void problem(void) { unsigned char ii; unsigned char jj; unsigned char flag; for (ii=0;ii<0xff;ii++) { for (jj=0;jj<0xff;jj++) { if (ii * jj < 0xff) { if (ii * (unsigned int)jj < 0xff) flag = 1; // ok else flag = 2; // not ok } } } }
=================== I have a double for loop, both loop counters are of type unsigned char. When I do a compare: if (ii * jj < 0xff), the outcome sometimes is wrong (flag = 2). That can be checked with the same expression using an explicit (unsigned int) cast.
The first three combinations which yield an incorrect result are: ii=0x82, jj=0xfd ii=0x82, jj=0xfe ii=0x83, jj=0xfb
I am using the following compiler: C51 COMPILER V9.51 - SN: K1NGC-DAEVIE COPYRIGHT Copyright (C) 2012 ARM Ltd and ARM Germany GmbH. All rights reserved.
on a C8051F587 processor.
Questions: o Do you agree that this is a bug in the compiler? o Is this a known bug?
If I recall it correctly:
The result of "ii * jj" is signed and therefore can be lower than 0xff
Write instead: if ((unsigned int)(ii * jj) < 0xffu)
So it is not a bug. This signed/unsigned rules in C have some known pitfalls.
Thanks a lot for your answer. Actually I should have known that. Probably not used to 2-byte integers anymore. (Luckily I did not make the code; just trying to solve some bugs).
View all questions in Keil forum