How do you detect 2's complement multiplication overflow?
问题 In one of the books that I am reading, following function is used to determine 2's complement integer multiplication overflow. int tmult_ok(int x, int y) { int p = x*y; return !x || p/x == y; } While this works, how do I prove its correctness in all the cases? How do I ensure that p != x*y when there is an overflow? Here is what I understand: When you multiply 2 integers of size "w- bits", the result can be 2w bits. The computation truncates higher order w bits. So we are left with lower