| Overflow: 2C | |||||||||||||
| If x and y have opposite signs, then the result can't overflow: | |||||||||||||
| magnitude of the result will be less than the magnitude of the larger number | |||||||||||||
| | x + y | <= max (|x|, |y|) | |||||||||||||
| Overflow can only occur when the numbers both have the same sign. | |||||||||||||
| If the sign of the result is different, then overflow must have occurred. | |||||||||||||
| For example, if x and y both have sign bit 0 (positive), and the result has | |||||||||||||
| sign bit 1 (negative), then overflow must have occurred. | |||||||||||||
| Add 2 k-bit numbers: | |||||||||||||
| xk-1...x0 | |||||||||||||
| + yk-1...y0 | |||||||||||||
| sk-1...s0 | |||||||||||||
| One way to express whether overflow occurs: | |||||||||||||
| V = xk-1yk-1\sk-1 + \xk-1\yk-1sk-1 | |||||||||||||
| Either both sign bits of x and y are 1 and the sign bit of s is 0, | |||||||||||||
| or the sign bits are both 0 and the sign bit of s is 1 | |||||||||||||
| Simpler formula: | |||||||||||||
| V = ck-1 XOR ck-2 | |||||||||||||
| The overflow bit is equal to the XOR of the carry-in to the leftmost bit | |||||||||||||
| with the carry-out from the leftmost bit. | |||||||||||||