Overflow Detection for Adders

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.

Overflow in UB 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.

Overflow in 2C addition

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.

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.

Misconceptions about Overflow

Overflow occurs when you do some operation to two valid representations, and the result can not be represented in the representation because the value is too large or too smal.

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.