When does 0xFF != 0xFF - strange behaviour in CW7.2

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

When does 0xFF != 0xFF - strange behaviour in CW7.2

577 Views
FridgeFreezer
Senior Contributor I

Device is MCF52259 144LQFP, CW is 7.2, code is C.

 

I have just had a strange problem in a routine, something like this:

 

#define MATCH 0xFF

 

myfunc(char ch)
{

       if(ch == MATCH)

       {

        do_something();

        }

}

 

Yet when I pass 0xFF to it, it doesn't evalute TRUE unless I do this:

 

#define MATCH 0xFF

 

myfunc(char ch)
{

       if(ch == (char)MATCH)

       {

        do_something();

        }

}

 

This code previously worked absolutely fine in our old project in CW5.9 / MMC2114

 

Anyone got any clues? Have I got to change the compiler config or something?

Labels (1)
0 Kudos
1 Reply

205 Views
CompilerGuru
NXP Employee
NXP Employee

Depending on the compiler, char can be either signed or unsigned. When char is signed and 8 bit, then condition ch == 0xff cannot be true as signed 8 bit chars (with 2 complement signed representation) can only represent values from -128 to 127.

 

The most portable solution to this is not to use char, but always signed char or unsigned char instead.

Another way might be to use "#define MATCH ((char)0xFF)".

 

It is probably possible to change the signedness of char in the CF compiler (I never looked into this), but I would suggest to write code which does not rely on the sign of char instead.

 

Daniel

0 Kudos