Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - 16 bit MCU multiplies two int variables, and the result is stored in a long variable. Why does it overflow?
16 bit MCU multiplies two int variables, and the result is stored in a long variable. Why does it overflow?
Obviously, your result is overflow. Therefore, the part above 65536 in 4000000 is discarded, and only the lower 16 bits are kept. Therefore, the result is equivalent to: 4000000% 65536 = 2304.

As for why this happens, I guess, if you are sure that your C is a long type, it should be the compiler's problem. I have encountered this problem before when converting from char type to short type.

However, you can do this to be sure:

Long c;

c = a;

c = c * b; That's enough.

In addition, you can cast:

Long c;

C = (length) a* (length) b;