Hello
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,
Thomas
Solved! Go to Solution.
Hello Thomas,
Try the following -
temp = (valor * 254UL);
This should cause the result of the multiplication to be 32-bit, rather than 16-bit.
Regards,
Mac
Hello Thomas,
Try the following -
temp = (valor * 254UL);
This should cause the result of the multiplication to be 32-bit, rather than 16-bit.
Regards,
Mac
Thank you very much !
I had tried with a cast on my own shortly after posting that(ocurred like this :smileysilly:) and it worked as well.
Your way is "cleaner" than a cast on "valor", that's probably what I'll use.
Again, thanks, problem solved
Thomas