I have a problem doing a simple long multiplication.
lValue = (long)(1000 * 3850); Return in lValue 0xFFFFBF10. Wrong should be 0x003ABF10.
lValue = 3850000; Return correct lValue 0x003ABF10.
Is this a serious compiler error? Or what do I do wrong?
Attached is a little project named Long_Mul and 2 pdf files that show the source code made as Tiny and Minimal Startup code. MCU used is a MC9SO8QD4 with 4K flash and 255 bytes RAM.
Sincerely Yours
Per Højfeldt
Solved! Go to Solution.
Hi Per,
The compiler is doing everything fine and to the standard: keep in mind that on the HCS08 an integer is 16bits.
Your multiplication (1000*3850) is performed with int, so gives 0xBF10. This is signed and gets sign extended with your cast to 32bit, so it gives 0xFFFFBF10.
If you want to do this multiplication on 32bit, you have have to use for example (1000L*3850).
I hope this helps,
Erich
Hi Per,
The compiler is doing everything fine and to the standard: keep in mind that on the HCS08 an integer is 16bits.
Your multiplication (1000*3850) is performed with int, so gives 0xBF10. This is signed and gets sign extended with your cast to 32bit, so it gives 0xFFFFBF10.
If you want to do this multiplication on 32bit, you have have to use for example (1000L*3850).
I hope this helps,
Erich
Thanks Erich.
Forgot that. Going from 32 bit MCU to 8 bit MCU can be tough. Haha.
Thanks for reminding me.
Per.
Forgot to say that on the multiplication the compiler gives a warning. See attached pdf files in folder Long_Mul.zip