Scroll to turn the page
Computer Architecture · Computer Architecture
how a machine of switches holds a number
Inside a computer there are only switches, each on or off. Everything it stores, from a song to a spreadsheet, is written in those two states.
One switch holds one bit, a 0 or a 1. Eight bits make a byte, and a byte has 2⁸ = 256 possible patterns. The figure holds one: 0100 0001.
What does it mean? The bits do not say. Read as an unsigned integer it is 65. Read as text, ASCII maps 65 to the letter 'A'. Written for people it is 0x41. The meaning comes from the instruction that uses the bits, not from the bits.
This chapter is about the patterns that hold numbers: positive integers, negative ones, and fractions. Each format has a fixed number of bits, and that limit is where the surprises are: sums that wrap round, positives that turn negative, and decimals that cannot be stored exactly.
each place is worth twice the one to its right
Decimal places are worth 1, 10, 100. Binary places are worth 1, 2, 4, 8 and so on: each is 2 times the one to its right.
In an 8-bit number the places are worth 128, 64, 32, 16, 8, 4, 2, 1. The leftmost is the most significant bit, the rightmost the least significant. The value is the sum of the weights where the bit is 1:
1011 0110 = 128 + 32 + 16 + 4 + 2 = 182
The figure reads the bits left to right and adds each weight to the running total.
Decimal to binary. Divide by 2 again and again; the remainders, read from the last to the first, are the bits:
| number | ÷ 2 | remainder |
|---|---|---|
| 182 | 91 | 0 |
| 91 | 45 | 1 |
| 45 | 22 | 1 |
| 22 | 11 | 0 |
| 11 | 5 | 1 |
| 5 | 2 | 1 |
| 2 | 1 | 0 |
| 1 | 0 | 1 |
Read upward: 10110110. With n bits the largest unsigned value is 2n − 1: 255 for 8 bits, 65,535 for 16, 4,294,967,295 for 32.
four bits to a digit
Long rows of bits are hard to read and easy to miscopy. Hexadecimal, base 16, writes them four at a time.
Four bits (a nibble) have 16 patterns, exactly the 16 hex digits: 0–9, then A, B, C, D, E, F for 10 to 15. So each hex digit stands for one nibble, and a byte is always two hex digits.
Split 1011 0110 into 1011 and 0110: 11 is B and 6 is 6, so the byte is 0xB6. Check: 11 × 16 + 6 = 182.
The prefix 0x marks a hex number in C, Java, Python and assembly. You meet hex wherever bits are shown to people: memory addresses (0x7FFC0010), machine instructions, and web colors, where #3D63A8 is three bytes: red 61, green 99, blue 168.
| binary | hex | decimal |
|---|---|---|
1001 | 9 | 9 |
1010 | A | 10 |
1100 | C | 12 |
1111 | F | 15 |
the carries ripple to the left
Binary addition is the column addition you learned in school, with only two digits.
In each column add the two bits and the carry coming in. The four cases:
| sum of the column | write | carry out |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 0 |
| 2 (binary 10) | 0 | 1 |
| 3 (binary 11) | 1 | 1 |
The figure adds 105 and 91, column by column from the right. The carries (red) move left one column each step:
0110 1001 + 0101 1011 = 1100 0100 = 196
That is the whole of addition in hardware. Chapter 2 builds it from logic gates: one small circuit per column, the full adder, with each carry wired into the next column. Because a carry can ripple through every column, a wide adder takes time to settle, which matters for how fast the processor can run.
past 255 the count starts again at 0
A register holds a fixed number of bits. When a sum needs one more, that bit has nowhere to go.
Picture every 8-bit pattern round a wheel, 0 at the top and counting clockwise to 255. Adding moves clockwise. Past 255 the next step is 0 again, like a car’s odometer.
200 + 100 = 300 needs 9 bits (100101100). The register keeps the low 8: 0010 1100 = 44. The lost bit is the carry out.
In general an n-bit sum is the true sum mod 2n: here 300 − 256 = 44. Unsigned overflow has happened exactly when there is a carry out of the top bit.
C and C++ define unsigned arithmetic to wrap exactly this way; hardware reports it in a carry flag on processors that have flags (x86 and Arm do; RISC-V leaves it to a compare). Overflow is not an error to the circuit: it is what fixed width means.
negative numbers on the same wheel
To hold negative numbers, every modern processor uses two’s complement: the same wheel, with its left half read as negative.
Going counterclockwise from 0 counts down: one step back from 0000 0000 is 1111 1111, so that pattern means −1. The patterns from 1000 0000 to 1111 1111 are −128 to −1. An 8-bit signed number runs from −128 to 127; with n bits, from −2n−¹ to 2n−¹ − 1.
Two consequences. The top bit is 1 exactly when the number is negative, so it works as a sign bit. And the most significant place is worth −128 instead of 128: 1101 0110 = −128 + 86 = −42.
To negate, invert every bit and add 1:
| step | bits | value |
|---|---|---|
| 42 | 0010 1010 | 42 |
| invert | 1101 0101 | −43 |
| add 1 | 1101 0110 | −42 |
Why it works: a number plus its inversion is all 1s, which is −1, so the inversion is −x − 1, and adding 1 gives −x. The payoff is that the adder needs no changes: unsigned and signed addition are the same circuit. Only the reading differs.
when two positives add to a negative
On the signed wheel the dangerous edge is not at the top but at the bottom, between 127 and −128.
100 + 50 = 150, beyond 127. The adder produces 1001 0110, the same bits as unsigned 150, but signed that is −106. Two positive numbers have produced a negative one.
The rule: signed overflow happens only when both operands have the same sign and the result’s sign is different. Adding a positive and a negative can never overflow: the result lies between them. Crossing the top of the wheel, from −1 to 0, is fine: −30 + 70 = 40, with a carry out but no overflow.
| sum | carry out | signed overflow |
|---|---|---|
| 100 + 50 | no | yes |
| −30 + 70 | yes | no |
So the carry out signals unsigned overflow and the sign rule signals signed overflow; they are different events. In C, signed overflow is undefined behavior, and real bugs have come from it: a counter that grows past its type’s maximum turns negative.
multiplying and dividing by powers of 2
Moving every bit one place left doubles a number, just as appending a 0 in decimal multiplies by 10.
Shift left by k (<<) moves the bits k places left, brings in 0s on the right and multiplies by 2k: 13 << 2 = 52, which is 13 × 4. Bits pushed off the left end are lost, so this too can overflow.
Shift right comes in two kinds, because of what to bring in on the left:
1101 0110 becomes 0110 1011 = 107.1101 0110 (−42) becomes 1110 1011 = −21.An arithmetic shift right by k divides by 2k and rounds toward −∞: −7 >> 1 is −4, not −3. RISC-V has both: srl and sra.
Compilers turn multiplications by constants into shifts and adds, because a shift is one cheap operation: x × 10 = (x << 3) + (x << 1).
IEEE 754: scientific notation in binary
For fractions and for very large or very small numbers, processors use binary scientific notation, standardized as IEEE 754.
A number is written as ±1.f × 2e, and a 32-bit single-precision float stores three fields:
| field | bits | holds |
|---|---|---|
| sign | 1 | 0 positive, 1 negative |
| exponent | 8 | e + 127 (the bias), so negative exponents need no sign |
| fraction | 23 | the bits after the binary point; the leading 1 is implied |
Encoding −6.25. In binary, 6.25 is 110.01 = 1.1001 × 2². So:
1000 0001;1001 followed by 19 zeros.All 32 bits: 0xC0C80000. Decoding runs backward: (−1)¹ × 1.5625 × 2² = −6.25.
The exponent’s all-0 and all-1 patterns are reserved: all 0s for zero and the tiny subnormal numbers, all 1s for ±infinity and NaN (“not a number”, the result of 0 ÷ 0). The 64-bit double has 11 exponent bits (bias 1023) and 52 fraction bits.
the gaps between floats grow
A float has a fixed number of fraction bits, so between each power of 2 and the next it can hold only so many values, evenly spaced.
The figure shows a toy float with 3 fraction bits: 8 values in each span from 2e to 2e+¹. Between 1 and 2 they are 0.125 apart; between 8 and 16, 1 apart. The gap doubles with each power of 2, so the relative precision stays the same. A value asked for that falls between two is rounded to the nearer one.
A single-precision float has 23 fraction bits, so 24 significant bits. Past 2²⁴ = 16,777,216 its gap is 2, and 16,777,217 is stored as 16,777,216. That is about 7 decimal digits of precision; a double has about 16.
Decimals are not exact. 0.1 in binary repeats forever (0.000110011001…), like 1/3 in decimal. A float stores the nearest value it can: 0.100000001490…. That is why, in doubles, 0.1 + 0.2 gives 0.30000000000000004, and why code should compare floats with a tolerance, never with ==, and why money is counted in whole cents.