That was one advantage of using 2C for signed int representation.

However, detecting overflow is different for the two representations. We look adding additional logic to the ripple carry adder, which can detect overflow for UB addition and for 2C addition.

Assuming both **x** and **y** are valid 2C bitstrings, then
their sum overflows only if the result is larger than the largest
positive integer. For **k** bits, the maximum value is
**2 ^{k} - 1**.

If a sum is larger than that, it overflows. When a sum is
larger than **2 ^{k} - 1**, it takes

Here's how the circuit looks:

The letter "V" is often used to represent the overflow bit. Presumably "O" is not used because it looks too much like the numeral 0.

Here are some facts about overflow in 2C.

- If
**x**and**y**have opposite signs (one is negative, the other is non-negative), then the sum will never overflow. Just try it out. The result will either be**x**or**y**or somewhere in between. - Thus, overflow can only occur when
**x**and**y**have the same sign. - One way to detect overflow is to check the sign bit of the
sum. If the sign bit of the sum does not match the sign bit of
**x**and**y**, then there's overflow. This only makes sense. - Suppose
**x**and**y**both have sign bits with value 1. That means, both representations represent negative numbers. If the sum has sign bit 0, then the result of adding two negative numbers has resulted in a non-negative result, which is clearly wrong. Overflow has occurred. - Suppose
**x**and**y**both have sign bits with value 0. That means, both representations represent non-negative numbers. If the sum has sign bit 1, then the result of adding two non-negative numbers has resulted in a negative result, which is clearly wrong. Overflow has occurred.

So that would suggest that one way to detect overflow is to look at the sign bits of the two most signicant bits and compare it to the sum.

Let's derive a formula for this. Let's say we want to add
two **k** bit numbers:
**x _{k-1}...x_{0}** and

Thus, one formula for detecting overflow is:

V = xThis formula basically says that overflow has occurred if the sign bit_{k-1}y_{k-1}\s_{k-1}+ \x_{k-1}\y_{k-1}s_{k-1}

We can write the formula as:

V = cThis is effectively XORing the carry-in and the carry-out of the leftmost full adder._{k-1}XOR c_{k-2}

Why does this work? The XOR of the carry-in and carry-out differ if there's either a 1 being carried in, and a 0 being carried out, or if there's a 0 being carried in, and a 1 being carried out.

When does that happen? Let's look at each case:

Notice the XOR gate at the bottom, which is exclusive-ORing the carry-in of the full adder of most significant bits to the carry-out.

Overflow detection is detecting overflow for a specific representation.
Too often people mistake overflow detection for overflow. Thus, students
say "overflow is when the carry out is a 1". Specific detection of
overflow requires knowing the operation *and* the representatin.

Thus, "overflow is when the carry out is a 1" is only correct when the representation is UB and the operation is addition. In this case, I claim that this definition is how to detect overflow for UB addition.

Overflow detection for 2C addition is different. One way to detect it is to XOR the carry-in and the carry-out. We also discussed another Boolean expression for detecting overflow.