Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

Demo 1.4: How Big Is the Box?

The previous demonstration kept its operands carefully fenced in. Every sum on the two’s complement tab was chosen so that the answer would fit, and the steppers refused to go further, and a note said why. This one takes the fence away.

Nothing in a computer is an arbitrary number of bits wide. Values live in fields of a fixed size, that size is decided long before anybody knows what will be put in it, and the field cannot be persuaded to hold one more value than it has patterns for. Ask it for a number outside its range and you do not get an error, a warning, or an approximation. You get a different number, confidently, with nothing about it marked as suspect.

A field of four bits does not count along a line. It counts around a circle of sixteen positions, and adding moves you clockwise, exactly as an odometer does.

Both readings of those bits use the same circle and the same addition. What differs between them is only where the numbering is cut. The unsigned reading cuts it between 15 and 0. The two’s complement reading cuts it between +7 and −8, half a turn away.

Everything in this demonstration falls out of that one picture. Carry is the flag that says you crossed the unsigned cut. Overflow is the flag that says you crossed the signed one. They are two flags because there are two cuts, both are computed on every addition whether you want them or not, and neither of them is an error until you say which reading you meant.

Bit groupings is the vocabulary, with the range each width implies. Pick a name and read the panel: how many patterns it has, what it holds unsigned, what it holds signed, and how many hexadecimal digits it takes to write. The table at the bottom lets you read all of them down the page at once, which is the only way to feel how fast the ranges grow.

Half the names in the reading are jokes, and the demonstration says so plainly rather than leaving you to find out in an interview.

The odometer wraps is the circle. Set a starting position and an amount to add, and two arcs are drawn: the unsigned journey outside the ticks and the signed journey inside them, matching the two rings of labels. When the amount you are adding has its top bit set, watch what the two arcs do.

Carry versus overflow puts one addition on the page and reads it both ways at once, with both flags worked out. All four combinations of the two flags are reachable and there is a preset for each. There is also a panel showing the two standard tests for overflow, checked against each other over every pair of four-bit values.

Spot it yourself generates sums at four, six and eight bits and asks only one question: which reading has gone wrong?

Fixed Widths, Wrapping and Overflow

How big the box is, what happens when you exceed it, and why carry and overflow are two different flags.

Nothing in a computer is an arbitrary number of bits wide. Values live in fields of a fixed size, and that size is chosen long before anybody knows what will be put in it. The names below are the ones the reading lists, and it is worth being told plainly that half of them are jokes. What is not a joke is the second half of the table: every width implies a range, the range is fixed, and asking a field for a number outside it is the subject of the rest of this demonstration.

The names
Bytein real use

The unit everything is measured in. Two hex digits, one character of ASCII, and the smallest thing most processors will fetch from memory.

width8 bits
patterns256
unsigned range0 to 255
signed range-128 to 127
in hexadecimal2 digits
8 bits, grouped in fours
0000
0
0001
1
  • The grouping into fours is not decoration.
  • Four bits is one hexadecimal digit, so a byte is two hex digits, a 16-bit value is four, and a 32-bit value is eight.
  • This is the regrouping from the demonstration on changing base, and it is the reason hexadecimal rather than octal became the way bit patterns are written down: four divides eight and three does not.
Every width, and what it can hold
NameBitsPatternsUnsignedSignedHex
Bit120 to 1-1 to 01
Crumbjoke240 to 3-2 to 11
Nibble4160 to 15-8 to 71
Nicklejoke5320 to 31-16 to 152
Byte82560 to 255-128 to 1272
Decklejoke101,0240 to 1,023-512 to 5113
Playtejoke1665,5360 to 65,535-32,768 to 32,7674
Dynnerjoke324,294,967,2960 to 4,294,967,295-2,147,483,648 to 2,147,483,6478
  • Read the Patterns column downwards.
  • Each row is not a little larger than the one above it, it is twice as large, so the numbers stop being readable almost immediately.
  • One extra bit doubles the range, which is why the difference between a 16-bit counter and a 32-bit one is not a matter of degree.
Why a byte is eight bits
  • It was not always eight. Machines have been built with six-bit, seven-bit and nine-bit bytes.
  • The word originally meant whatever group of bits held one character on the machine you happened to be using.
  • Eight won partly on merit: it is a power of two, so a byte splits evenly into hexadecimal digits and addresses divide neatly.
  • It also won on history: IBM's System/360 used eight, and everything after it followed.
  • The standards body's term for exactly eight bits is the octet, which exists precisely because byte once did not reliably mean eight.
The word that means several things
  • A word is the natural unit a processor works in: 32 bits on a 32-bit machine, 64 bits on a 64-bit one.
  • The trap is that the term also got frozen into instruction sets, where it no longer tracks the machine.
  • On x86, WORD still means 16 bits on a 64-bit processor, because it meant 16 bits in 1978 and changing it would have broken everything.
  • So a Windows programmer writing DWORD is writing 32 bits on a machine whose actual word is 64.
  • When a data sheet says word, find out which one it means.

Open Bit groupings and click along the row.

A bit is the unit. A nibble is four bits, it is genuinely used, and it earns its place because a nibble is exactly one hexadecimal digit. A byte is eight bits and is what everything is measured in. Those three are real.

Crumb, nickle, deckle, playte and dynner are jokes, following nibble and byte into a dinner service. The sizes are real and important, and 16 and 32 bits in particular are everywhere, but they are called halfword and word, or short and int, and nobody has ever said dynner in a design review.

Then click Word, which is the one that causes genuine trouble.

A word is the natural unit a processor works in, so it is 32 bits on a 32-bit machine and 64 on a 64-bit one. The trap is that the term got frozen into instruction sets: on x86, WORD still means 16 bits on a 64-bit processor, because it meant 16 bits in 1978 and changing it would have broken everything ever compiled. A Windows programmer writing DWORD is writing 32 bits on a machine whose actual word is 64. When a data sheet says word, find out which one it means.

Look at the Patterns column in the table at the bottom of that tab.

Each row is not a little larger than the one above it. It is twice as large, and the numbers stop being readable almost immediately: 256, then 65,536, then 4,294,967,296, then eighteen and a half quintillion. One extra bit doubles the range.

That is worth sitting with, because it cuts both ways. It means the difference between a 16-bit counter and a 32-bit one is not a matter of degree. It also means that a field which is too small is not slightly too small: when a 16-bit counter is not enough it is usually catastrophically not enough, and making it 17 bits would not help.

Step 3: Watch the circle, and find the two cuts

Section titled “Step 3: Watch the circle, and find the two cuts”

Open The odometer wraps and press No wrap, which is 3 + 2.

Sixteen positions, numbered twice. The outer ring is the unsigned reading, 0 to 15. The inner ring is two’s complement, 0 to +7 and then −8 to −1. The two marks across the circle are the seams: amber at the top between 15 and 0, violet at the bottom between +7 and −8.

Now press Past the unsigned seam, which is 15 + 1. The arc crosses the amber mark. Read the two panels: unsigned, 15 + 1 gives 0, which is wrong. Signed, the very same bits say −1 + 1 gives 0, which is right.

Then press Past the signed seam, which is 7 + 1, and the situation reverses exactly. Unsigned, 7 + 1 is 8 and correct. Signed, +7 + 1 gives −8 and is wrong.

Two additions, four verdicts, and in each case one reading is right while the other is wrong. The hardware did not choose between them. It set one flag and cleared the other, and which of them matters depends entirely on what you meant the bits to be.

Step 4: Watch the two arcs go opposite ways

Section titled “Step 4: Watch the two arcs go opposite ways”

Press Past both, which starts at position 8 and adds 8.

Read the note that appears. The addend’s top bit is 1, so the two readings disagree about which direction you are travelling: as unsigned you are moving 8 positions clockwise, and as signed you are moving 8 positions anticlockwise. Both arcs finish on the same tick, because 8 and −8 differ by exactly 16 and the circle has 16 positions.

This is the clearest single picture of why there have to be two flags. The destination is shared and the route is not, so each reading meets its own seam, or misses it, quite independently of the other. The outer amber arc crosses the amber seam and the inner violet arc crosses the violet one, and both flags come up set.

Now step Add down to a small positive number and watch the two arcs fall back into step. When the top bit of the addend is 0 both readings agree about the direction, and only then do the two journeys coincide.

Open Carry versus overflow and work through all four presets.

The column layout is the one from the previous demonstration, with the top column shaded so you can watch the two carries that matter: the one going into it and the one coming out. Beneath it are the two flags, and beside them the same addition read as unsigned and as signed.

The important preset is Carry only. The two panels disagree about the same addition, and there is nothing in the wires that distinguishes 15 from −1: they are the same four bits. The difference lives entirely in the intention of whoever wrote the program.

That has a direct consequence in software. Declaring a variable signed or unsigned in C does not change the addition instruction the compiler emits, because there is only one. It changes the branch instruction that follows it. On ARM, BHI and BLS branch on the unsigned comparison while BGT and BLE branch on the signed one, and choosing the wrong family is a real and common bug.

Step 6: Learn the rule you can use on paper

Section titled “Step 6: Learn the rule you can use on paper”

Still on that tab, read the panel with the two detection rules.

The hardware test is that the carry into the top column and the carry out of it differ. That is cheap, because both wires already exist inside the adder and one XOR gate finishes the job.

The test to use on paper is the one from the reading: two operands of the same sign that produce a result of the opposite sign. Adding two positives can only overflow upwards, adding two negatives can only overflow downwards, and adding numbers of opposite signs can never overflow at all, because the answer lies between them and both of them were in range.

That last part halves your work. When checking a sum by hand, glance at the two top bits: if they differ, there is no signed overflow to look for.

The demonstration works both rules out independently for all 256 pairs of four-bit values and reports that they agree on every one. They are two descriptions of the same event.

Step 7: Look at the examples from the reading

Section titled “Step 7: Look at the examples from the reading”

Underneath, the two six-bit sums from the chapter are worked out: 17 + 19 and −17 + −19.

Both are signed overflows and both are caught by V. Now look at their carry flags, because they do not agree. Adding two positives produced no carry at all, while adding two negatives produced one, and in neither case did the carry have anything to say about the error.

If you had been watching the carry flag to decide whether a signed sum was trustworthy, the first of those would have sailed straight past you.

Open Spot it yourself. Before calculating anything, look at the two top bits. If they differ, V is 0 and only the unsigned reading can be in trouble. If they match, look at whether the answer’s top bit changed.

Try it at six and eight bits too. Nothing changes except the size of the circle.

Quiz
Select 0/1

A four-bit field holds the value 1111. What number is that?

Quiz
Select 0/1

Adding two four-bit values produces a carry out of the top column. What does that tell you?

Concept Match

Match each item to what it actually is

Quiz
Select 0/2

Which of these additions overflow when the bits are read as signed? Assume a four-bit field. Select all that apply.

Quiz
Select 0/1

Why does making a field one bit wider so rarely fix an overflow problem properly?

Five things to take away.

  1. Bit, nibble and byte are real terms. Crumb, nickle, deckle, playte and dynner are jokes. Word means whatever the architecture says, and on x86 it has meant 16 bits since 1978 regardless of the actual machine.
  2. Every width implies a range, and ranges grow by doubling. One extra bit doubles what a field can hold, which is why a field that is too small is rarely nearly big enough.
  3. A fixed-width field counts around a circle, not along a line. Adding walks you round it and passing the start is called wrapping.
  4. There are two seams on that circle because there are two readings, and they sit half a turn apart. Carry reports crossing the unsigned seam and overflow reports crossing the signed one.
  5. Both flags are set on every addition and neither is an error by itself. Which one matters is a fact about your program, not about the processor, and the processor genuinely cannot know it.

That completes the arithmetic. Everything from here is about building circuits that do it, starting with what a logic gate actually is, and the carry you have been stepping along a row of columns will turn up again as a wire between two gates when the adders are built.