The size of the basic integer types varies on different platforms. You can avoid this problem by using uint32_t
and similar types.
Signed integer overflow is undefined behavior, although overflow is well-defined for unsigned integral types (you do the arithmetic modulo 2^N
). Even still, you have to watch out for it, because modular arithmetic is usually not what you were trying to do.
I believe the standard once upon a time left open exactly how division is rounded (although I'm not sure if "positive / positive" was ever left open). But I think this is standardized now, and even if not, rounding towards 0 is nearly universal.
But you can use numeric_limits
to check. If this documentation is to be believed, the standard does guarantee rounding towards zero.