At 7:43 PM -0400 2000/08/17, DIAMOND JEFF wrote:
>It makes sense that on the Palm, ints are 16-bit, not 32-bit.

Integers are whatever size you tell the compiler they are. This isn't unique to the 
Palm OS...


>But what I didn't catch is that in macros, such as:
>
>#define MY_FLAG        (32768 << 256)
>
>UInt32 flags |= MY_FLAG;
>
>Everything will act like it's working OK -> in fact, all the normally
>"integer math" functions such as (a << b) still work in 32-bits.
>
>But the #defines don't.  The #define above will only contain the low
>word of the value, and since you can't see the value of a define in the
>debugger, all you notice is that values and branches aren't working
>right.

Newcomers to C must ALWAYS remember that #defines are character-by-character 
replacements of the original C code, not compile-time macros like inline functions. 
You'll have the same problem above whether you use #defines or enter the constants 
directly into the source -- integer overflow is (almost) always silent.


>The best way I found around this was just to check the USE 4 BYTE INTS
>option.

The example above is rather odd, in that one can't left shift *any* supported integer 
size 256 times and get a non-zero result. But regardless, a better solution all-around 
is to quantify the size of the naked constant by adding L after the constant to make 
it a long integer (e.g. 32768L) or by explicitly casting it to the desired size (e.g. 
(UInt32)32768).

Regards,

Jim Schram
Palm Incorporated
Partner Engineering


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to