The Meaning of Overflow

In digital logic, numbers are stored in registers or slots with a fixed size, such as 4 bits or 8 bits. Because space is limited, there is a limit to how large or small a number can be. Overflow occurs when the result of a math operation is too big or too small to fit into the space you have allocated.

Think of an odometer in an old car that only has five digits. If you drive 99,999 miles and then drive one more mile, the odometer rolls over to 00,000. The car hasn't actually traveled zero miles, but the display "overflowed" because it didn't have enough digits to show 100,000. In our circuits, overflow is a serious problem because it means the computer is giving us a mathematically incorrect answer.

Recall: The Sign Bit

Before we look at how overflow happens, let's remind ourselves of a key rule from our lesson on Two's Complement. In a signed binary system, the leftmost bit is the sign bit.

  • If the leftmost bit is 0, the number is positive.
  • If the leftmost bit is 1, the number is negative.

As you have seen in previous lessons, this bit is special because it tells the circuit how to interpret the rest of the digits. When we build an adder circuit, this sign bit is treated just like any other column during addition, but its final value tells us if our result makes sense.

Identifying Overflow Scenarios

Overflow does not happen every time you add numbers. In fact, it only happens in two specific situations. Let’s look at the rules:

  1. Positive + Positive = Negative: If you add two positive numbers and the result has a 1 in the sign bit, an overflow has occurred.
  2. Negative + Negative = Positive: If you add two negative numbers and the result has a 0 in the sign bit, an overflow has occurred.

If you add a positive number and a negative number, overflow is impossible. This is because the result will always be somewhere between the two original numbers, meaning it will always fit in the same number of bits.

Visualizing the Paradox

To see overflow in action, let’s imagine we are using a 4-bit system. In 4-bit Two’s Complement, the range of numbers we can represent is -8 to +7.

If we try to add 5+45 + 4, we know the answer should be 99. However, 99 is larger than our maximum limit of 77. Let's see what happens in binary:

The Logic of Detection (The XOR Method)

How do we tell a circuit to recognize this error automatically? We use the Carry bits from the very last stage of our adder.

Every Full Adder has a Carry-In (CinC_{in}) and a Carry-Out (CoutC_{out}). To detect overflow, we specifically look at the very last bit (the column).

Summary and Lab Prep

In this lesson, you learned that overflow is a mathematical error that happens when a circuit's bit-limit is exceeded. You discovered that:

  • Overflow only happens when adding numbers with the same sign.
  • You can spot overflow if the result's sign bit is the opposite of the inputs.
  • In hardware, we detect this by comparing the Carry-In and Carry-Out of the last bit using an XOR gate.

Now, you are ready to head into the practice environment. You will be tasked with taking a standard binary adder and adding an Overflow Flag to it. This flag will light up whenever the math doesn't fit, ensuring your circuit "knows" when it has made an error!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal