Jeff,

> SO my big question - if "int" is 16-bits, does that mean all constant
> math is 16-bits?

This is not a Palm programming issue. You may want to get a C programming book
(my Bible is "C: A Reference Manual" by Harbison & Steele), or post your
question to a C programming forum (or read its FAQ)

But to answer your question, operands in an expression are generally converted
to a common type sufficient to handle each operand. Thus, in (262143L + 1), the
"1" is converted to a long to be compatible with the "262143L", and the two are
added together as longs. In this conversion process, NO consideration is given
to the possible type needed to hold the result. The type is the same as the type
of the converted operands.

-- Keith Rollin
-- Palm OS Emulator engineer






DIAMOND JEFF <[EMAIL PROTECTED]> on 08/18/2000 02:30:44 PM

Please respond to "Palm Developer Forum" <[EMAIL PROTECTED]>

Sent by:  DIAMOND JEFF <[EMAIL PROTECTED]>


To:   "Palm Developer Forum" <[EMAIL PROTECTED]>
cc:    (Keith Rollin/US/PALM)
Subject:  Re: Watch out for 16-bit constants!



Thanks, Jim...
Yeah - my example as a typo - I meant "<< 8", not "<< 256"...  I used
the wierd shift example because the shift operator was documented as
using "ints", but when I use it in a direct assignment, it seems to
correctly use 32-bits.

SO my big question - if "int" is 16-bits, does that mean all constant
math is 16-bits?

For example:

#define LONG1 262144L // people say this works... (no overflow)
#define LONG2 (262143L + 1L) // will this work?  Or will the "+" convert
to 16-bits?

Thanks!
- Jeff

Jim Schram wrote:
>
> 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/

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





-- 
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