Lynn Allan said (on or about) 11/11/2006 09:46:
UInt32  baseOffset = 4000000;
UInt32  testOffset = baseOffset + (30000 * 4);         // 3988928
testOffset = baseOffset + ((UInt32)30000 * (UInt32)4); // 4120000
testOffset = baseOffset + (30000L * 4L);               // 4120000

I'm using CW9.3 and seeing unexpected results when adding constants. I would 
expect the 2nd, 3rd, and 4th statements to all result in testOffset being set 
to 4,120,000, but the 2nd statement seems to treat the (30000*4) as Int16 
numbers, and it becomes a negative number.

Is this a bug, or defined/expected behavior? Or am I confused?

This has thrown me off several times. Is there a setting to cause constants to 
be treated as Int32?


The calculation (30000 * 4) is being done by the compiler
at compile time.  I don't recall whether this is defined
by the language standard or left to be defined by the
compiler implementation.  Either way, I would write my code
as if I expected that the constant 30000 would be forced
into the smallest signed type that could hold it and write
the code defensively to make sure that the result was what
I wanted it to be.


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

Reply via email to