C18 compiler shortcomings.
The following was posted by Pommie in another thread.
I have today converted a project from BoostC to C18 and had a rather nasty bug. I finally tracked it down to one bit of code that wasn't working correctly. Here is that code in isolation,
Code:
#include <p18f1320.h>
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO2
void main(){
char count;
unsigned int temp;
OSCCON=0x60; //Osc=4MHz
ADCON1=0x7f; //All digital
count=0;
temp=0b0000100000000000;
while((temp&(1<<count))==0){
count++;
}
while(1);
}
What this code should do is find the first non zero bit in temp and so count should contain 11 after it executes. Well, it doesn't, it contains 7. C18 works with bytes when it does 1<<count. What is even more peculiar is that temp=1<<11 will place zero in temp not 0x0800 as you would expect.
You can fix the above by doing
while((temp&((int)1<<count))==0) or adding -Oi command line option.
So, why am I telling you this. Cause it took me a long time to find it and I just had to get it of my chest. Plus, it may help others.
BTW, the above worked fine in BoostC and it is the C18 compiler that doesn't comply with ISO.
Edit, One good point about C18 is that it's free.
Mike.
and
Sure, add it to that thread. It's not really a bug but can cause some nasty bugs.
Who would have thought that writing,
would not be the same as,
I always assumed that constants would be treated as 32 (or even 64) bits on the PC and cast to the appropriate size during assignment.
Mike.