Basis Lab

What a Computer Does to a Number

What Really Happens When a Sum Does Not Fit

Last timeA Number in a Fixed Number of Bits

Adding one to the largest value does not raise an error, it lands at the bottom of the range. Here is the carry that does it, the modulus behind it, and where it hides.

The column with no column above it

Add 1 to 127 in eight bits. Both values fit, the true answer is 128, and 128 is

outside the signed range because the largest eight bit signed value is 127. So

something has to happen, and it is worth watching it happen one column at a

time rather than being told the answer.

Written as patterns the sum is 01111111 plus 00000001. Addition proceeds from

the right, each column adding two bits and the carry from the column below, and

producing a sum bit and a carry for the column above.

FIG 1127 plus 1, column by column
stepbit positionthe two bitscarry insum bitcarry outwhat happened
101 + 10011 plus 1 is 2, which is 10 in binary, so the column keeps 0 and hands 1 upwards. This is the only column where both bits are set.
211 + 01011 plus 0 plus the carry is 2 again. The carry survives.
321 + 0101The same again, and it will be the same up to bit 6.
431 + 0101Four columns done and every one of them has emptied itself.
541 + 0101The carry is now travelling through the value rather than being absorbed by it.
651 + 0101Nothing here is unusual: this is how 0111 plus 0001 becomes 1000 at any width.
761 + 0101The last of the seven low bits, all of which are now clear.
870 + 0110The carry arrives at the top column and stops. Both bits here are 0, so the sum bit is the carry itself, and nothing leaves the byte.
8 steps
Read the last row twice. The carry went into the top column and did not come out of it, so no bit left the byte at all: the carry out is 0. What changed is that the top bit is now set, and under the signed rule its weight is minus 128, so the eight bits hold -128 + 0 = -128. The sum did not fall off the end. It fitted perfectly and read as the wrong number.

That is the part most accounts of this skip. Two separate things can go wrong in

the top column and they are independent. A carry coming out of it means the

unsigned reading has run past 255. A carry going in without coming out, or

coming out without having gone in, means the signed reading has run past its own

ends. Processors expose these as two different condition flags for that reason,

and the signed condition is the exclusive or of the carry in and the carry out,

which was checked here against the true sum for all 65,536 ordered pairs of

eight bit signed values with no disagreement.

Both cases are real and they do not coincide. For 127 plus 1 the carry in is 1

and the carry out is 0, so the signed sum has overflowed while the unsigned

reading, 128, is perfectly fine. For 255 plus 1, which is the same bits as minus

1 plus 1, the carry in is 1 and the carry out is also 1, so the signed sum is

fine at minus 1 plus 1 equals 0 while the unsigned reading has run out. One

addition, two rules, two different verdicts.

It is not an error, it is a modulus

The carry out of the top column of a ww bit addition would have had the weight

2w2^w. Throwing it away therefore subtracts exactly 2w2^w from the true answer,

and never anything else. For 255 plus 1 the true answer is 256 and the stored

answer is 256−256=0256 - 256 = 0.

So the unsigned rule is simple to state. The stored result is the true sum

modulo 2w2^w, and the C standard says so directly, by defining an unsigned

result that cannot be represented to be reduced modulo one more than the largest

value the type can hold. There is no error condition to detect because, as far

as the language is concerned, nothing went wrong.

The signed rule is the same residue seen through a window in a different place.

The window runs from −2w−1-2^{w-1} up to 2w−1−12^{w-1} - 1 rather than from 0 up to

2w−12^w - 1, so the residue has to be shifted before the modulus is taken and

shifted back afterwards.

The lesson stops here

8 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Why Eight Bits Hold 256 Values and Minus 128 Has No Positive Twin
  2. 02What Really Happens When a Sum Does Not Fityou are here
  3. 03Reading a Real Number Out of Its Bits, and Writing One Tenth Back Inopening only
  4. 04The Gap to the Next Representable Number, and How to Compute Itopening only
  5. 05One Rounding Is Half a Gap, and a Tenth Plus Two Tenths Is Exactly a Tieopening only
  6. 06Error in a Long Sum Grows Like Root n, Not Like nopening only
  7. 07The Subtraction That Loses Every Correct Digit at Onceopening only
  8. 08Two Zeros, Two Infinities, and a Value Not Equal to Itselfopening only

Read alongside