Math Error using WinAVR

WinAVR-20090313-install.exe

GNU Compiler Collection (GCC) 4.3.2

avr-libc 1.6.6

AVR AtMega328P

Mixing unsigned char and unsigned long

count1 and count2 should be equal.

But for some input values they are not.

The equation that uses unsigned long vars is always right.

The equation that uses unsigned char vars is sometimes right and sometimes wrong.

--------- Source -----------------------

unsigned char Byte0, Byte1, Byte2;

unsigned long count1, count2, b0, b1, b2;

Byte0 = 121;

Byte1 = 192;

Byte2 = 33;

b0 = (unsigned long)Byte0;

b1 = (unsigned long)Byte1;

b2 = (unsigned long)Byte2;

count1 = (65536 * Byte2) + (256 * Byte1) + Byte0;

count2 = (65536 * b2) + (256 * b1) + b0;

--------- Results -----------------------

Byte0=121 Byte1=192 Byte2=33 Count1=2146425 Count2=2211961 <-- Different

Byte0=231 Byte1=85 Byte2=34 Count1=2250215 Count2=2250215 <-- Same

--------------------

The fact that the value of count1 and count2 are

often the same, but ocasionally different indicates a bug in the library.

Can anyone confirm this?