### Introduction

In our discussion of ripple-carry adders, we said that the same adder that adds two k-bit UB bitstrings also adds two k-bit 2C bitstrings.

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.

When adding two k-bit bitstrings using UB, you are adding two non-negative numbers. Let's call these numbers x and y. Since they are non-negative, then x + y >= x and x + y >= y. That is, adding two non-negative numbers either keeps the value the same, or increases it.

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 2k - 1.

If a sum is larger than that, it overflows. When a sum is larger than 2k - 1, it takes k + 1 bits to represent the result. So, you can tell overflow just by checking if the carry out of the leftmost adder (which adds the most significant bits and the carry in) is 1.

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.

The condition for overflow is different if the bitstring representation is 2C. In particular, x + y >= x and x + y >= y are no longer necessarily true.

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: xk-1...x0 and yk-1...y0. The sum is sk-1...s0.

Thus, one formula for detecting overflow is:

```  V = xk-1yk-1\sk-1 + \xk-1\yk-1sk-1
```
This formula basically says that overflow has occurred if the sign bit x and y are 1, and the sign bit of the sum is 0, or if the sign bit of x and y are 0, and the sign bit of the sum is 1.

### A Simpler Formula for Overflow

However, there is an easier formula, though one that is more obscure. Let the carry out of the full adder adding the least significant bit be called c0. Then, the carry out of the full adder adding the next least significant bit is c1. Thus, the carry out of the full adder adding the most significant bits is ck - 1. This assumes that we are adding two k bit numbers.

We can write the formula as:

```  V = ck-1 XOR ck-2
```
This is effectively XORing the carry-in and the carry-out of the leftmost full adder.

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:

#### Case 1: 0 carried in, and 1 carried out

If a 0 is carried, then the only way that 1 can be carried out is if xk-1 = 1 and yk-1 = 1. That way, the sum is 0, and the carry out is 1. This is the case when you add two negative numbers, but the result is non-negative.

#### Case 1: 1 carried in, and 0 carried out

The only way 0 can be carried out if there's a 1 carried in is if xk-1 = 0 and yk-1 = 0. In that case, 0 is carried out, and the sum is 1. This is the case when you add two non-negative numbers and get a negative result.

#### The Circuit

Here's the circuit that detects overflow when adding two k-bit numbers represented in 2C.

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.