Re: [cc65] why this doesn't work?

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2010-06-06 13:40:01
On Sun, Jun 06, 2010 at 11:16:37AM +0100, S M wrote:
> > if ( moviles [i].tipo > -1 )
>
> Member tipo is of type char, the current values for tipo are 0,1,2 and 255.
> But this condition never evaluates to true even when assessing 0,1 or 2. I
> made it work with
>
> if ( moviles [i].tipo >= 0 )
>
> but why doesn't it work as it is above, it would produce better code I
> presume.

What you're doing is doubtful, but the compiler is in fact wrong here.

Characters are unsigned, and the compiler tries to do the comparison using
unsigned chars and finds out that an unsigned char can never be greater than
-1 casted to unsigned char (which is 0xFF). The standard says that default
integer promotions have to be applied, so the comparison must use ints, which
means that your code is valid and should work. But - and this is the doubtful
part - it will always be true, since something stored in an unsigned char is
never negative. So your code is actually a do-nothing operation.

As a general rule, don't use negative numbers if you don't have to, because
the 6502 needs additional code in many case to handle them. I do assume that
your assumption about better code when using negative numbers is not backed by
any facts. At least I can hardly imagine how this could be the case.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sun Jun 6 13:40:10 2010

This archive was generated by hypermail 2.1.8 : 2010-06-06 13:40:13 CEST