Nic
Nic

Reputation: 21

Two's complement detect overflow with carries

If I add two signed binary numbers with Two's Complement method, why does it automatically mean that overflow has occoured if the carry into the MSB (the sign) and the carry out are not the same?

Upvotes: 2

Views: 1583

Answers (1)

GabrielOshiro
GabrielOshiro

Reputation: 8322

Let's take one fact out of the way.

When we add a negative and a positive operand, the result will always be in the range of representation. Overflows occur when we add two numbers with the same sign (both positive or both negative) and the result has the opposite sign.

When we add numbers in two's complement, we add the sign bit of the first operand with the sign bit of second operand.


When we add positive + positive operands, the sum of the sign bits is 0.

  0XXX (positive)
+ 0XXX (positive)
------
  0XXX (positive)

That means that it doesn't matter what happens, there will NEVER be a carry-out when adding positive + positive operands.

So, if there is a carry-on into the sign bits

  1
  0XXX (positive)
+ 0XXX (positive)
------
  1XXX (negative)

That carry-on bit 1 will become the sign of the result. That means that we added two positive operands and we got a negative number as a result.

Carry-out = 0
Carry-on sign bit = 1
OVERFLOW!


When we add negative + negative operands, the sum of the sign bits is 0 with a carry-out.

  1XXX (negative)
+ 1XXX (negative)
------
 10XXX (positive)

That means that it doesn't matter what happens, there will ALWAYS be a carry-out when adding negative + negative operands. Note that by "default" the result would be a positive number. It "needs" that carry-on to adjust the result to the same sign of the operands. If there is a carry-on into the sign bit, we will have two negative operands with a negative result.

So, if there is no carry-on into the sign bits

  0
  1XXX (negative)
+ 1XXX (negative)
------
 10XXX (positive)

That carry-on bit 0 will become the sign of the result. That means that we added two negative operands and we got a positive number as a result.

Carry-out = 1
Carry-on sign bit = 0
OVERFLOW!

Upvotes: 1

Related Questions