Assuming you're using a horrifically old enough compiler for where int
is only 16 bits. Then yes, your analysis is correct.*
400 * 400 = 160000
// Integer overflow wrap-around.
160000 % 2^16 = 28928
// Integer Division
28928 / 400 = 72 (rounded down)
Of course, for larger datatypes, this overflow won't happen so you'll get back 400
.
*This wrap-around behavior is guaranteed only for unsigned integer types. For signed integers, it is technically undefined behavior in C and C++.
In many cases, signed integers will still exhibit the same wrap-around behavior. But you just can't count on it. (So your example with a signed 16-bit integer isn't guaranteed to hold.)
Although rare, here are some examples of where signed integer overflow does not wrap around as expected:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…