Lite / Building Machines That Respond / 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?
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.
Predict
A 4-bit unsigned register holds 1111 (15). You add 1. What does it hold now?
- 16
- 10000
- 0000
- 1111
Guess first, then reveal
15 + 1 = 16 = 10000, but there are only four bits, so the leading 1 falls off as a carry out. The register shows 0000: it has wrapped round, like a car's odometer.
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.
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
Unsigned k-bit addition is arithmetic modulo 2ᵏ; carry out signals overflow. Two's complement maps the same bit patterns to −2ᵏ⁻¹ … 2ᵏ⁻¹ − 1; signed overflow is the XOR of the carry into and out of the top bit.
Mathematical
8-bit: range −128 to 127. −1 = 1111 1111 = 255 as unsigned. −100 = 256 − 100 = 156 = 1001 1100. 100 + 100 = 200 overflows signed 8-bit and reads as 200 − 256 = −56.
Applied
Wheel encoders count pulses in fixed-width registers that wrap; robot code computes the difference between two readings in the same width so a wrap does not look like a huge jump.
Derivation: Why 'invert and add 1' gives −n
In k bits, n plus its bitwise inverse ¬n is all 1s: for example 0101 + 1010 = 1111. So n + ¬n = 2ᵏ − 1.
Add 1 to both sides: n + (¬n + 1) = 2ᵏ.
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.
Example, 4 bits: 5 = 0101, ¬5 = 1010, +1 = 1011. Check: 0101 + 1011 = 1 0000, which wraps to 0000. ✓
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Why do you think computers use two's complement rather than a separate minus sign?
Write your answer on paper or in your own notes. Lite never stores what you write.