AnsweredAssumed Answered

long multiplication issues

Question asked by Thomas Jeannes on Jun 25, 2013
Latest reply on Jun 25, 2013 by Thomas Jeannes



I am currently working on a MC9S08SH8 with CodeWarrior IDE 5.9.0( build 5292 ) to design an interface between 2 sub-systems. One collects data, and the other sends them. My program is to read the data received, convert them into SI units (meters, Pascals ... ) and store them in a 32 bytes vector in order to send data through RS-232 serial port.

Problem is, during the conversion phase, some multiplications are done wrong :


This one works perfectly :

     unsigned short valor = 30000;


     unsigned short resultado;

     unsigned long temp;


     temp = (valor*33782);

     auxiliar_1 = temp;     // = 1.013.460.000


     resultado = temp;



But this one does not :

     unsigned short valor = 300;   

     unsigned short resultado;

     unsigned long temp;


     temp = (valor*254);

     auxiliar_2 = temp;     // = 10.664 instead of 76.200 !


     resultado = temp;

I have noticed that 76.200d is 1 00101001 10101000b in binary,

                     and 10.664d is 0 00101001 10101000b.

So it seems that even though temp is a long type(4 bytes), in the second example, it is truncated to short type(2bytes), but not in the first ...

Any idea why this happens ? Syntaxis and call situation are identical. I tried replacing the "254" in the second example by "33782" and it worked. How could it be related to the number ?

Thanks in advance,