Kayavak / Foundations of Computing / CS·06 Number Systems & Data Representation / Deeper
Deeper · Foundations of Computing
Number Systems & Data Representation
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Gottfried Leibniz described binary arithmetic in 1703, but nobody needed it until machines did. Two-state devices (relays, then vacuum tubes, then transistors) are far easier to build reliably than ten-state ones: telling "on" from "off" survives noise that would blur ten voltage levels. Once Shannon showed in 1937 that switching circuits compute Boolean logic, binary won, and every other representation in this chapter is a convention for what a pattern of bits means.
Those conventions were fought over. Early machines represented negative numbers in sign-magnitude or ones' complement, which have two zeros and need extra circuitry; two's complement won because the same adder works for signed and unsigned numbers, and C23 and C++20 finally made it the only allowed representation. Floating point was a mess of incompatible formats until the IEEE 754 standard (1985), led by William Kahan, who received the Turing Award in 1989 largely for it. Text went from dozens of national character sets to ASCII (1963), then to Unicode (1991) for every writing system, and UTF-8, designed by Ken Thompson and Rob Pike in 1992, became the way almost the whole web stores it.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Bits have no meaning of their own. A type is an agreement about how to read them; most bugs in this chapter are two parts of a system reading the same bits under different agreements.
| Question | Answer |
|---|---|
| What is it? | Conventions mapping bit patterns to numbers, characters and structures |
| Problem it solves | Storing and exchanging any kind of information with hardware that only knows 0 and 1 |
| Parts | Width (how many bits), encoding (unsigned, two's complement, IEEE 754, UTF-8), byte order, alignment |
| How it works | Fixed-width patterns represent a finite set of values; arithmetic wraps, traps or rounds at the edges |
| Assumptions | Writer and reader agree on the type, width, encoding and byte order |
| When they change | Mojibake, wrong signs, garbled numbers across machines: the same bytes read under another agreement |
| Where it fails | Any value outside the representable set: overflow, rounding, unmappable characters |
| Related ideas | Registers and ALUs implement it (ch07); compilers choose widths (ch10); networks fix byte order (ch09) |
What if?
What if computers used decimal floating point?
Reason it through, then open
0.1 + 0.2 would equal 0.3, because tenths are exact in base ten. But 1/3 would still be inexact, as it is on paper, so rounding wouldn't disappear, it would just happen at the fractions people expect. IEEE 754 has had decimal formats since its 2008 revision, and some hardware and libraries (Java's BigDecimal, Python's decimal) provide them; binary remains the default because it's faster and simpler in hardware.
What if every machine were big-endian?
Reason it through, then open
Network byte order and memory order would match, so the conversion calls in every networking library would vanish, and hex dumps would read in the natural order. Nothing else fundamental would change: endianness is a pure convention. Little-endian survived on x86 partly because it makes reading a smaller integer from the start of a larger one free, which mattered to early hardware.
Where the boundary is
| Aspect | Binary floating point (double) | Decimal (BigDecimal, decimal) |
|---|---|---|
| Exact for | Integers up to 2⁵³ and fractions with power-of-two denominators | Any number with a finite decimal expansion |
| 0.1 + 0.2 | 0.30000000000000004 | 0.3 |
| Speed | One hardware instruction | Software, many times slower |
| Use for | Measurements, science, graphics, ML | Money and anything with legal decimal rounding rules |
Derive it
Why "invert and add one" negates a two's complement number.
- In n bits, arithmetic wraps around modulo 2ⁿ, so −x is whatever value y makes x + y = 0 modulo 2ⁿ, that is, y = 2ⁿ − x.
- Inverting every bit of x gives ~x, and x + ~x is all ones, which is 2ⁿ − 1 (every column adds 1 + 0, with no carry).
- Rearranging: ~x = 2ⁿ − 1 − x, so ~x + 1 = 2ⁿ − x, which is exactly −x.
- A consequence: the most negative value, −2ⁿ⁻¹, has no positive partner in n bits; negating it gives itself back. In
Java,
Math.abs(Integer.MIN_VALUE)is negative for this reason.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| ~x + 1 = −x in two's complement | Theorem | The derivation above |
| Doubles represent every integer exactly up to 2⁵³ | Theorem | 53 significant bits in IEEE 754 binary64 |
| UTF-8 is backward compatible with ASCII | Theorem | Its design: code points below 128 are single, identical bytes |
| Two's complement is the only signed representation in C and C++ | Convention | Required since C23 and C++20; before that, implementations could choose |
| Binary floating point is unsuitable for money | Model | Correct as a rule; exact rounding rules come from regulation and accounting, not mathematics |
Still open
Number formats are changing again because of machine learning: bfloat16, 8-bit floats (FP8) and even 4-bit formats trade precision for speed and memory, and how little precision training and inference can tolerate is an active research question. Unicode keeps growing every year, and what counts as one "character" for editing and display is still being refined.
What would change your mind? If you met a workload where the extra exactness of decimal mattered but its speed didn't, the default of binary floats would be the wrong choice there. Finance is the standard example.
Recall
Why does "invert and add one" negate a two's complement number?
x + ~x = 2ⁿ − 1, so ~x + 1 = 2ⁿ − x, which is −x modulo 2ⁿ.
Why is Math.abs(Integer.MIN_VALUE) negative in Java?
−2³¹ has no positive counterpart in 32 bits; negating it gives itself.
Would decimal floating point remove all rounding error?
No: tenths become exact, but values like 1/3 still round.
Why did two's complement beat other signed representations?
One zero, and the same adder works for signed and unsigned arithmetic.
What decides what a bit pattern means?
Only the agreed type, width, encoding and byte order; bits have no meaning of their own.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.