Two 2's complement numbers having sign bits x and y are added and sign bit of result is z.Then occurence of overflow is indicated by which boolean function? I believed that overflow occurs when the XOR of the sum and carry is 1 ,there is overflow,else no overflow.Keeping this in mind,I came up with the following truth table:
X Y Z Overflow
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 1
Is this table correct?
1 Answer 1
When can an overflow happen?
- Adding a positive and a negative number yields a number in between them (lower border inclusive), thus no overflow is possible.
- Adding two positive numbers will always result in a positive number.
- Adding two negative numbers will always result in a negative number.
-> An overflow is only possible when both summands share the same sign.
Can an overflow wrap around more than once?
In a n-bit 2's complement number,
- the maximum number is \2ドル^{n-1}-1\$. Adding two of them yields: $2ドル\cdot(2^{n-1}-1)=2^n-2=-2$$
- the minimum number is \$-2^{n-1}\$. Again adding two of them: $2ドル\cdot(-2^{n-1})=-2^n=0$$
-> In case of an overflow the result will be always negative for positive summands and positive for negative summands.
Conclusion
An overflow has happened if, and only if, both summands have the same sign and the result has the other sign.
-
\$\begingroup\$ So certain cases here will not hold good in the truth table? \$\endgroup\$user1369975– user13699752016年02月19日 04:04:16 +00:00Commented Feb 19, 2016 at 4:04
-
\$\begingroup\$ @user1369975 Yes. Only the cases
001
and110
are overflows. \$\endgroup\$Rainer P.– Rainer P.2016年02月19日 08:27:02 +00:00Commented Feb 19, 2016 at 8:27 -
\$\begingroup\$ Here z indicates sum or carry? \$\endgroup\$user1369975– user13699752016年02月20日 14:28:49 +00:00Commented Feb 20, 2016 at 14:28