Skip to content
BetterDL

Overflow

Also called: arithmetic overflow, integer overflow

When the true result of an arithmetic operation doesn't fit in the available bits, so the stored answer is wrong. Unsigned and signed overflow are tested differently.

Hardware works with a fixed number of bits. Overflow happens when the correct answer is outside the range those bits can show, so the result you get is the wrong number.

The same addition can overflow under one reading and not the other, so there are two tests:

  • Unsigned overflow: the addition produced a carry out of the MSB. The true sum needed one more bit.
  • Signed overflow (twos complement): two operands with the same sign gave a result with the opposite sign. Adding numbers of different signs never overflows. In hardware: the carry into the MSB differs from the carry out of it.

The two tests are independent. All four combinations happen (see the example), so never judge one from the other.

Why it happens: fixed-width arithmetic wraps around. In 4 bits, 15 + 1 rolls over to 0 (unsigned), and +7 + 1 rolls over to −8 (signed).

An ALU records both verdicts as status flags: C for the carry-out and V for signed overflow. The program checks whichever matches its data type.

C4C3V

Worked example

Example

Four 4-bit additions, both verdicts

Unsigned range 0 to 15, signed range −8 to 7. Each sum is checked both ways.

  1. 1.

    0001 + 0100 = 0101. No carry-out. Unsigned 1 + 4 = 5 and signed 1 + 4 = 5 both fit. No overflow.

  2. 2.

    1111 + 0010 = 1 0001. Carry-out, so unsigned overflow (15 + 2 = 17). Signed: −1 + 2 = 1, correct.

  3. 3.

    0110 + 0011 = 1001. No carry-out, so unsigned is fine (6 + 3 = 9). Signed: 6 + 3 reads as −7, so signed overflow.

  4. 4.

    1000 + 1100 = 1 0100. Carry-out, so unsigned overflow (8 + 12 = 20). Signed: −8 + (−4) = −12 is below −8, and 0100 reads +4. Signed overflow too.

Common mistakes

  • Treating a carry-out as signed overflow. Adding two negative numbers always carries out, even when the answer fits.

  • Expecting signed overflow to need a carry-out. 0111 + 0001 = 1000 overflows with no carry-out.

  • Calling any negative result overflow. Overflow means same-sign inputs gave the opposite sign.

Practice Overflow

Interactive questions with instant feedback and a worked solution for every wrong answer.

Basic CPU / Computer Architecture lesson full course

Learn it step by step

Overflow is taught in Number Systems and Adders and ALUs.