On Thu, 2002-10-10 at 07:43, Sebastian from Berlin/Germany wrote:
> 
> Hey,
> 
> if i run the following code:
> 
> Int16 count[6];
> char counterlabel[40];
> Int16 p, all,allcards;
> 
> count[1]=2000;
> p=(100*count[1]);
> StrPrintF (counterlabel,"Box 1 - %d cards (%d %%) - %d", 
> count[1],p,count[1]);
> 
> i get a counterlabel = "Box 1 - 2000 cards (3392 %) - 2000"
> 
> Why the hell 3392??? I thought about 200000 ...

Short answer: 

You've overflowed Int16s. 

Make the count integers Int32 rather than Int16.

Long answer:
This is wrapping of an integer.  Int16's can have a maximum value of
32767 before they wrap.

An Int16 has 16 bits:
10000000000000000

The first 1 is the sign bit.  The zeros are the actual number.2^15 is
32768, so 15 bits can hold a maximum of 32767.

Decimal 200000 is 0x30d40 in hex.

However, the 3 is in the 17th and 18th bits, so they get chopped off. 
What's left is 0x0d40, which is indeed 3392.

-Ken


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

Reply via email to