Skip to content
Tactiverge

School/△ Builder/Week 17

Overflow and negative numbers

In a 4-bit computer, 7 + 1 = −8. That is not a bug in the adder. So what is going on?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Registers have a fixed number of bits, so every computer arithmetic result can overflow. Engineers who forget this have lost rockets; engineers who remember it design counters, timers and sensors that never surprise them.

  • ◇ Identify unsigned overflow from the carry out of a fixed-width adder.
  • ◇ Represent negative numbers in two's complement and convert them both ways.
  • ◇ Detect signed overflow and explain why it matters in real machines.

2.Predict

A 4-bit unsigned register holds 1111 (15). You add 1. What does it hold now?

Guess first. Being wrong here is how the surprise works.

3.Discover

Set 4 bits. Add 1111 + 0001 and read the unsigned result and carry out. Then add 0111 + 0001 and read the signed view: why is 7 + 1 shown as −8? Find a pair of negative numbers whose sum looks positive. Switch to 8 bits and repeat one of your examples.

Loading the interactive…

Can't use the interactive? Read a description instead

The adder stepper shows each number's unsigned and signed values. In 4 bits, 1111 + 0001 gives 0000 with a carry out: unsigned overflow. 0111 + 0001 gives 1000, which is 8 unsigned but −8 signed: signed overflow, because two positive numbers gave a negative result. In 8 bits the signed range is −128 to 127.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

In a 4-bit unsigned register, what is 1011 + 0110? Give the value the register holds, in decimal.

Practice 2 · a fresh version every time

What is −2 in 4-bit two's complement? Give the bit pattern's unsigned value in decimal.

5.Understand

Unsigned overflow

k bits hold 0 to 2ᵏ − 1. If a sum is bigger, the extra bit leaves as carry out and the register keeps the sum minus 2ᵏ. 4-bit 1011 + 0110 = 11 + 6 = 17 → keeps 1 (0001), carry out 1. The carry out flag is how hardware reports unsigned overflow.

Two's complement

To store negatives, let the top bit be worth −2ᵏ⁻¹. In 4 bits the range becomes −8 to 7: 0111 = 7, 1000 = −8, 1111 = −1. To negate, invert every bit and add 1. The same adder circuit then adds signed numbers correctly, so subtraction is just adding a negative.

Signed overflow

If two numbers of the same sign give a result of the opposite sign, the true answer did not fit. 0111 + 0001 = 1000: 7 + 1 'became' −8. Adding a positive and a negative can never overflow.

Real consequences

In 1996 the first Ariane 5 rocket was lost less than a minute after launch when its guidance software converted a 64-bit floating-point value into a 16-bit signed integer and the value did not fit. Embedded engineers choose widths with headroom and test the edges.

Explain it another way

A 4-bit register is a clock face with 16 positions. Go past the top and you land back near the bottom. Two's complement just relabels the top half of the clock as negative numbers.

Derivation: Why 'invert and add 1' gives −n
  1. In k bits, n plus its bitwise inverse ¬n is all 1s: for example 0101 + 1010 = 1111. So n + ¬n = 2ᵏ − 1.

  2. Add 1 to both sides: n + (¬n + 1) = 2ᵏ.

  3. A k-bit register cannot hold 2ᵏ: it wraps to 0. So in k-bit arithmetic n + (¬n + 1) = 0, which means ¬n + 1 behaves exactly like −n.

  4. Example, 4 bits: 5 = 0101, ¬5 = 1010, +1 = 1011. Check: 0101 + 1011 = 1 0000, which wraps to 0000. ✓

  5. Equivalently, the top bit is worth −2ᵏ⁻¹ instead of +2ᵏ⁻¹: 1011 = −8 + 2 + 1 = −5.

Worked example 1

Write −3 in 4-bit two's complement and check by adding 3.

3 = 0011 → invert 1100 → add 1: 1101. Check: 0011 + 1101 = 1 0000 → 0000 in 4 bits. ✓

Common mix-ups

  • A negative number just has a minus-sign bit in front of the normal value.
    ✓ That is sign-magnitude, which needs special adder logic. Two's complement lets the ordinary adder work for negatives too.
  • Overflow means the computer crashes.
    ✓ Usually it silently wraps, which is worse: the program carries on with a wrong number.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3 · a fresh version every time

What is −5 in 4-bit two's complement? Give the bit pattern's unsigned value in decimal.

Practice 4 · a fresh version every time

An unsigned 4-bit register adds 1011 + 1100. What value does the register hold afterwards, in decimal? (Any carry out of the top bit is lost.)

Practice 5

Which 4-bit two's complement addition overflows?

Practice 6

What is −5 in 4-bit two's complement? Give the bit pattern's unsigned value in decimal.

7.Apply it

A robot stores its wheel speed in a signed 8-bit variable and adds 30 every control step. Starting from 10, after how many steps does it overflow, and what value does it show? Check with the adder stepper in 8-bit mode, then propose a fix.

8.Reflect

Concepts: Fixed-width integers, bits and registers · Binary arithmetic and adders