I'm implementing a calculator in a microcontroller which is controlled over the serial port. For example, I'd send 1234*5678=
and it would reply with 7006652\r\n
. I've written the code for this using the C18 compiler.
When I send 123456*789123=
, I get the reply 2932688576
, which is incorrect. The correct answer would have been 97421969088
, but that overflows an unsigned long
.
I do not want to increase the overflow limit, but would like to have a way to check if there has been an overflow. In ASM, that would be possible by checking the relevant bit in the STATUS register, however, with C, this bit would be cleared before I could read it, wouldn't it?
The best would be a general solution to see if an overflow has occured.