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.
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.
Overflow does not happen every time you add numbers. In fact, it only happens in two specific situations. Let’s look at the rules:
- Positive + Positive = Negative: If you add two positive numbers and the result has a
1in thesign bit, anoverflowhas occurred. - Negative + Negative = Positive: If you add two negative numbers and the result has a
0in thesign bit, anoverflowhas 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.
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 , we know the answer should be . However, is larger than our maximum limit of . Let's see what happens in binary:
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 () and a Carry-Out (). To detect overflow, we specifically look at the very last bit (the column).
In this lesson, you learned that overflow is a mathematical error that happens when a circuit's bit-limit is exceeded. You discovered that:
Overflowonly happens when adding numbers with the same sign.- You can spot
overflowif the result'ssign bitis the opposite of the inputs. - In hardware, we detect this by comparing the
Carry-InandCarry-Outof the last bit using anXOR 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!
