Computing Foundations › Inside the Machine › Day 4
Day 4: Binary and Data Representation: Bits, Bytes, and Numbers
After this lesson you will be able to convert numbers between decimal, binary, and hexadecimal by hand, add and negate binary numbers the way the hardware does, and explain overflow, floating point, and quantization well enough to size an AI model from its bit format.
Hands-on lab for this lesson
Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-004-binary-and-data-representation-bits-bytes
- Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git cd ai-roadmap-365.github.io - Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
cd labs/sections/computing-foundations/day-004-binary-and-data-representation-bits-bytes - Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
- Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
bash tests/run_tests.sh # or the test command named in the lab README
You can also open the lab as a local page (works offline, shows the file tree and expected output).
Learning objectives
By the end of this lesson you will be able to:
- Explain place value in bases 2, 10, and 16 and read any byte by summing the place values under its 1-bits
- Convert numbers between decimal, binary, and hexadecimal by hand using the subtraction, division, and bit-grouping methods
- Add two 8-bit binary numbers column by column, tracking carries, and verify the result in decimal
- Negate a number in two's complement (flip the bits, add 1) and explain why one adder circuit then handles both addition and subtraction
- Describe integer overflow, including the Ariane 5 flight 501 conversion failure and the Year 2038 problem, and state the range limits of 8-, 16-, and 32-bit integers
- Explain in plain language why 0.1 + 0.2 ≠ 0.3 in floating point and what the sign, exponent, and mantissa fields of a float32 each control
- Compute the memory footprint of a model from its parameter count and numeric format (float32, float16, bfloat16, int8), and distinguish KiB/MiB/GiB from kB/MB/GB
Prerequisites
- Day 1 (How a Computer Works) — transistors, registers, and the adder circuit are assumed
- Comfort opening a terminal, as practiced in the Day 1 lab
- Primary-school column addition — the carry rule is reused unchanged in base 2
Why this matters
Everything in a computer is a number, and every number is a choice about how many bits to spend.
-
That choice is a lever with a price. Model size is parameters times bytes per parameter — nothing more complicated.
-
Get it wrong and nothing raises. A dtype mismatch produces different numbers, not an error.
-
The Ariane 5 rocket was destroyed by this, 37 seconds after ignition.
The idea in plain language
Bits carry no meaning. The type decides what they say.
-
The same 32 bits are an integer, a decimal, or four letters, depending only on how you agree to read them.
-
Nothing in the bits records which. That agreement lives in your program, a file header, or a column type.
-
Break the agreement and you get a different answer, silently.
Historical background
| When | What arrived | Why it mattered |
|---|---|---|
| 1703 | Leibniz publishes binary arithmetic | Two symbols suffice for any number |
| 1937 | Shannon links Boolean algebra to circuits | Binary becomes buildable |
| 1956 | Buchholz coins “byte” | A named unit for a group of bits |
| 1963 | ASCII standardised | 7 bits, 128 characters, English only |
| 1985 | IEEE 754 | One floating-point standard everyone follows |
| 1991 | Unicode 1.0 | One code point per character, for every script |
| 2018– | bfloat16 spreads through ML hardware | Range kept, precision traded, on purpose |
-
IEEE 754 is why
0.1 + 0.2 != 0.3on every machine you will ever use — it is the standard behaving correctly, not a bug. -
bfloat16 is the newest entry and the most instructive: a format invented because training tolerates coarse values far better than overflow.
What it is — and what it is not
It is a set of agreements about what patterns of bits mean.
It is not:
-
Not a property of the hardware. The same memory holds an integer today and a float tomorrow.
-
Not lossless for decimals. One tenth has no finite binary expansion, exactly as one third has none in decimal.
-
Not about size alone. Range and precision are separate axes, and formats trade them against each other deliberately.
Why it was created and what problems it solves
-
Two states are easy to build reliably. A switch is on or off, with a wide margin between them — ten voltage levels would be far more fragile.
-
Place value scales. Eight bits give 256 values, sixteen give 65,536. Each added bit doubles the range at a cost of one wire.
-
Hexadecimal exists for humans, not machines. Each hex digit maps to exactly four bits, so long binary strings become short and mechanically translatable.
How it works
Place values
-
Each position is worth twice the one to its right: 128, 64, 32, 16, 8, 4, 2, 1.
-
10110101is 128 + 32 + 16 + 4 + 1 = 181. -
Eight bits hold 0–255; sixteen hold 0–65,535.
Negative numbers: two’s complement
- Flip every bit and add one. That is the whole rule.
- The top bit becomes a sign without needing a separate sign field.
- Addition works unchanged on negatives, which is why hardware uses it.
Decimals: sign, exponent, mantissa
A float splits its bits three ways:
| Part | What it controls | float32 | float16 | bfloat16 |
|---|---|---|---|---|
| Sign | Positive or negative | 1 | 1 | 1 |
| Exponent | Range — how large or small | 8 | 5 | 8 |
| Mantissa | Precision — significant digits | 23 | 10 | 7 |
-
bfloat16 keeps float32’s exponent and gives up mantissa bits. Same range, coarser values.
-
That is exactly the right trade for training, which tolerates imprecision far better than overflow to infinity.
-
float16’s 5-bit exponent caps values near 65,504, which gradients routinely exceed — hence loss scaling.
Why 0.1 + 0.2 is not 0.3
-
One tenth has no finite binary expansion, just as one third has none in decimal.
-
The machine stores the two nearest representable values and adds them.
-
The rounded sum differs from 0.3 microscopically — and comparing floats with
==is what turns that into a bug.
An everyday analogy
A number written on a card, with no note saying what kind of number it is.
| The card says | It could mean |
|---|---|
030405 | The 3rd of April 2005 |
030405 | Three pounds four and fivepence |
030405 | A phone extension |
030405 | A product code |
-
The digits are identical in every case. The convention decides.
-
Two people using different conventions on the same card get different answers and neither notices.
-
That is a dtype mismatch, and it is why training and serving must agree.
Examples in practice
The Ariane 5, flight 501, 4 June 1996.
-
A 64-bit float was converted to a 16-bit signed integer, which holds at most 32,767.
-
On Ariane 4’s gentler trajectory the value never got that large, so the check had been deliberately removed to save processor time.
-
Ariane 5 climbed faster. At 37 seconds, the value exceeded 16 bits, the conversion raised an unhandled exception, and the unit shut down.
-
The backup, running identical software, had failed the same way moments before.
-
The rocket destroyed itself. Several hundred million dollars, because a number did not fit the type it was converted into.
Why your 1 TB drive shows 931 GB. The maker counts in powers of 1,000; the OS counts in powers of 1,024. 10^12 ÷ 1024^3 ≈ 931. Nothing is missing.
Implications: security, privacy, performance, scalability, and cost
-
Cost. Model size is parameters times bytes per parameter. A 70B model is 280 GB at float32, 140 GB at bfloat16, 35 GB at 4 bits.
-
Performance. Fewer bytes per weight means fewer bytes moved per token, which raises the bandwidth ceiling from Day 3.
-
Correctness. Integer overflow wraps silently in many languages; Python’s integers do not, which is a genuine safety property.
-
Security. Overflow and truncation are a classic exploit class — a length field that wraps becomes a buffer that is too small.
-
Precision. Accumulating millions of float additions drifts. Summation order changes the answer, which is why reproducibility needs fixed ordering.
Alternatives: free, open source, and commercial
As with Day 1, “alternatives” here means other excellent routes into the same material, at different depths and formats.
| Resource | Type | What it offers | Cost |
|---|---|---|---|
| The Day 4 lab in this course | Free | Conversion drills by hand plus a shell toolkit you complete yourself | Free |
| Crash Course Computer Science, episodes 4 and 5 (PBS Digital Studios) | Free video | Binary, hexadecimal, and how the ALU adds — visual and fast | Free |
| Wikipedia: “Binary number”, “Two’s complement”, “IEEE 754” | Free reference | Rigorous, well-cited treatments of exactly today’s three pillars | Free |
| ”Code” by Charles Petzold (2nd ed.), middle chapters | Book (commercial) | The gentlest deep telling of binary, bytes, and codes in print | Book purchase |
| From Nand to Tetris (Schocken & Nisan) | Open course | Build the adder and ALU that execute this lesson’s arithmetic | Free materials |
A programmer’s calculator (macOS Calculator’s Programmer view, or bc) | Built-in software | Instant dec/hex/bin conversion for checking your hand work | Free |
If you read only one thing beyond this lesson, make it Petzold’s chapters on binary codes — and check every hand conversion you ever do against a programmer’s calculator until the two of you always agree.
Comparison with related concepts
| Often confused | The actual difference |
|---|---|
| Bit vs byte | One binary digit vs a group of eight |
| KB vs KiB | 1,000 bytes vs 1,024 — the source of the “missing” disk space |
| Integer vs float | Exact within a range vs approximate across a huge range |
| float16 vs bfloat16 | More precision, less range vs the reverse |
| Encoding vs encryption | Making data readable by machines vs unreadable by people |
That last row matters. Base64 is an encoding, not a security measure: it is reversible by anyone, with no key.
When to use it — and when not to
Think about representation when choosing a dtype, a model will not fit, values look subtly wrong, or you are comparing decimals.
Ignore it when writing ordinary logic. Languages provide sensible defaults, and hand-choosing an integer width in a data-cleaning script is a way to introduce a bug rather than avoid one.
The tell: a number that is close but not equal, a value that wrapped to negative, or a file that is exactly twice or half the size you expected.
The AI connection
-
Precision is a memory lever with a measurable price. Same model, quarter the bytes, four times the bandwidth headroom.
-
bfloat16 exists because training tolerates coarse values far better than overflow — the exponent bits matter more than the mantissa bits.
-
A dtype mismatch between training and serving degrades a model quietly. It does not raise, it just answers slightly differently.
-
The Ariane 5 failure is the same bug class at a different scale: a value that did not fit the type it was converted into, with nothing checking.
Knowledge check
Try these from memory before looking back:
- Convert 77 to binary using either the subtraction or division method, showing each step, then convert your answer to hexadecimal by grouping bits.
- Add the bytes
00110101and01001011by hand, tracking every carry, and check your work in decimal. - The byte
11101100is a two’s-complement signed integer. What decimal number is it? Explain the role the leftmost bit played in your answer. - In one paragraph, retell the Ariane 5 failure: what representation was converted to what, why the value went out of range on Ariane 5 but never on Ariane 4, and why the backup computer did not save the flight.
- A colleague stores prices as floats and cannot reproduce a customer’s total. Explain, using 0.1’s binary expansion, what is happening and what representation to use instead.
- A “2 TB” external drive shows up as roughly 1.8 TiB. Show the arithmetic proving the drive is not defective.
Hands-on exercise
Convert numbers by hand, then watch the machine disagree with you about decimals. Worked through fully in the Day 4 lab directory.
| What you are checking | Command |
|---|---|
| Binary of a number | python3 -c 'print(bin(181))' |
| Hex of a number | python3 -c 'print(hex(181))' |
| What 0.1 really is | printf '%.20f\n' 0.1 |
| Whether the sum matches | python3 -c 'print(0.1 + 0.2 == 0.3)' |
| Disk size, both conventions | df -h / and df -H / |
Expected output
0b10110101
0xb5
0.10000000000000000555
False
-
0b10110101is 181 — 128 + 32 + 16 + 4 + 1. -
0xb5is the same value in hex: b is 11, so 11x16 + 5 = 181. -
0.1 is stored as 0.10000000000000000555. The error appears at the 17th decimal place, and accumulates.
-
Falseis correct behaviour, not a bug. It is IEEE 754 working.
Validate your work
- You converted 77 to binary by hand and the machine agrees
- You can state which decimal digit of 0.1 first goes wrong
- You can explain why
0.1 + 0.2 == 0.3is False in one sentence - You computed a 70B model’s footprint at four precisions
-
bash tests/run_tests.shreports 0 failures
Troubleshooting
-
printfshows only six decimals. Use%.20f, not%f— the default precision hides exactly the digits you are looking for. -
bin()output starts with0b. That prefix is Python telling you the base; it is not part of the number. -
df -handdf -Hdisagree. They should:-huses 1,024 and-Huses 1,000. That difference is the whole “missing disk space” question. -
Your two’s complement result looks too large. Check you are working in a fixed width. Eight bits wrap at 256.
Common mistakes
-
Comparing floats with
==. Use a tolerance —math.isclose— and reserve exact equality for integers. -
Confusing KB with KiB. 1,000 against 1,024. The gap compounds: at terabyte scale it is about 7%.
-
Assuming int8 quantisation loses 4x the accuracy. It loses precision, not proportional accuracy, and the effect is task-dependent.
-
Reading a dtype mismatch as a crash. It is not. It silently returns different numbers, which is worse.
Practice assignment
-
Convert by hand. Turn 77 into binary using either subtraction or repeated division, showing each step, then check with
python3 -c 'print(bin(77))'. -
Find the drift. Run
printf '%.20f\n' 0.1and the same for 0.2 and 0.3. Identify the first decimal digit that goes wrong. -
Do the size arithmetic. For a 70-billion-parameter model, compute the footprint in GiB at float32, bfloat16, int8 and 4 bits. Which fit in the RAM you measured on Day 1?
Extension challenge
Explain the bfloat16 trade in terms of place value alone.
-
Compute the ranges. A 5-bit exponent versus an 8-bit exponent — what is the largest representable value in each?
-
Compute the cost. Three exponent bits were bought with three mantissa bits. How many significant decimal digits does that lose?
-
Write the argument. Two paragraphs on why a noisy statistical process can afford that trade, and why a bank’s ledger cannot.
Quiz
Q1. What decimal number does the binary pattern 101010 represent?
- 52, because the digits sum with place values 16, 8, 4
- 20, because there are two 1-bits and a trailing zero
- 42, because the 1-bits sit on the place values 32, 8, and 2
- 84, because each 1-bit doubles the previous total
Show answer
Answer: C. 42, because the 1-bits sit on the place values 32, 8, and 2
Binary place values run 1, 2, 4, 8, 16, 32 from the right; 101010 has 1-bits under 32, 8, and 2, and 32 + 8 + 2 = 42.
Q2. Why do engineers write bit patterns in hexadecimal?
- Hexadecimal avoids the rounding errors that binary introduces
- Processors compute faster on hexadecimal numbers than on binary ones
- Hexadecimal can represent negative numbers while binary cannot
- Each hex digit maps to exactly four bits, shortening long binary strings
Show answer
Answer: D. Each hex digit maps to exactly four bits, shortening long binary strings
Hex is purely a human convenience: because 16 = 2⁴, each hex digit stands for one four-bit group, so conversion is table lookup with no arithmetic. The processor itself only ever sees bits.
Q3. In 8-bit two's complement, how do you form the representation of −44 from 44 (00101100)?
- Flip every bit and add 1, giving 11010100
- Reverse the order of the bits, giving 00110100
- Subtract the pattern from 11111111, giving 11010011
- Set the leftmost bit to 1, giving 10101100
Show answer
Answer: A. Flip every bit and add 1, giving 11010100
Two's complement negation is invert-then-increment: 00101100 flips to 11010011, and adding 1 gives 11010100. Adding that to 00101100 produces a ninth bit that falls off the register, leaving exactly zero.
Q4. What actually went wrong in the software aboard Ariane 5 flight 501 in 1996?
- A floating-point division by zero crashed the main flight computer
- A 64-bit float was converted to a 16-bit integer it could not fit
- Cosmic radiation flipped a bit in the guidance program's memory
- The engines were commanded in imperial units while guidance computed in metric
Show answer
Answer: B. A 64-bit float was converted to a 16-bit integer it could not fit
Alignment code reused from Ariane 4 converted the horizontal-bias value to a 16-bit signed integer. Ariane 5's faster, flatter ascent pushed the value past 32,767 about 37 seconds after ignition; the operand-error exception halted both identical inertial units, and the vehicle broke up and self-destructed.
Q5. Why does 0.1 + 0.2 print as 0.30000000000000004 in most programming languages?
- The addition circuit in the ALU has a known rounding defect
- The decimal point character is being misread as a thousands separator
- One tenth has no finite binary form, so the sum is microscopically off
- Floating-point numbers can only store values greater than 1 exactly
Show answer
Answer: C. One tenth has no finite binary form, so the sum is microscopically off
0.1 in binary is 0.000110011001100… repeating forever, just as 1/3 repeats in decimal. Floats store the nearest representable values, and the sum of those approximations rounds to a number a hair above 0.3. The hardware adds flawlessly; the inputs were never exactly 0.1 and 0.2.
Q6. A 7-billion-parameter model is quantized from float32 to int8. What happens to its memory footprint for weights?
- It drops to about 875 MB, because int8 uses one bit per weight
- It halves from about 28 GB to about 14 GB, because int8 stores two weights per byte
- It stays the same, because quantization only changes computation speed, not storage
- It drops from about 28 GB to about 7 GB, because each weight shrinks from 4 bytes to 1 byte
Show answer
Answer: D. It drops from about 28 GB to about 7 GB, because each weight shrinks from 4 bytes to 1 byte
Quantization to int8 stores each weight in a single two's-complement byte (with a scale factor for reconstruction) instead of four bytes: 7 billion × 4 bytes ≈ 28 GB becomes 7 billion × 1 byte ≈ 7 GB.
Q7. Both float16 and bfloat16 use 16 bits. What is the key difference, and why does machine learning generally prefer bfloat16?
- bfloat16 keeps float32's exponent range, trading mantissa precision
- float16 is an integer format while bfloat16 is a floating-point format
- bfloat16 stores two numbers per 16 bits, doubling effective memory capacity
- bfloat16 rounds decimal fractions like 0.1 exactly, avoiding accumulation error
Show answer
Answer: A. bfloat16 keeps float32's exponent range, trading mantissa precision
float16 spends its bits on precision (10-bit mantissa, 5-bit exponent); bfloat16 spends them on range (7-bit mantissa, 8-bit exponent, matching float32). Gradients and activations routinely exceed float16's ~65,504 ceiling, so keeping the exponent field is the safer trade for learning.
Q8. A drive sold as "1 TB" shows about 931 GiB in the operating system. Why?
- The filesystem permanently reserves 7% of every drive for error correction
- The maker counts in powers of 1,000 and the OS in powers of 1,024
- The operating system is misreading the drive's firmware and the true size is 1,024 GiB
- The drive shipped partly full of preinstalled recovery software
Show answer
Answer: B. The maker counts in powers of 1,000 and the OS in powers of 1,024
Both numbers describe the same bytes. Marketing prefixes are decimal (kilo = 1,000), while KiB/MiB/GiB are binary (kibi = 1,024); the gap compounds to about 7% at the tera scale. No storage is missing.
Glossary
- bit
- A single binary digit, 0 or 1 — the smallest unit of information, physically realized as one two-state switch.
- byte
- A group of 8 bits, able to hold 256 distinct patterns; the smallest unit of memory most machines address, standardized at 8 bits by IBM's System/360 in 1964.
- nibble
- Half a byte — 4 bits — which is exactly the amount of information one hexadecimal digit represents.
- word
- The natural chunk of bits a processor handles in one operation, typically 32 or 64 bits on modern machines.
- place value
- The worth of a digit position in a positional numeral system: powers of 10 in decimal, powers of 2 in binary, powers of 16 in hexadecimal.
- hexadecimal
- Base-16 notation using digits 0–9 and A–F, written with the 0x prefix; each digit encodes exactly four bits, making it the standard human-readable shorthand for raw binary.
- two's complement
- The standard representation of signed integers, in which −n is the bit pattern of 2ᵏ − n (flip the bits, add 1); it gives one unique zero and lets a single adder circuit perform both addition and subtraction.
- sign bit
- The leftmost bit of a signed integer; in two's complement it carries a negative place value (−128 in a byte), so a 1 there makes the whole number negative.
- overflow
- What happens when an arithmetic result exceeds what a fixed-width register can hold: the value silently wraps around, as when an unsigned byte computes 255 + 1 = 0.
- floating point
- Binary scientific notation for storing fractional numbers as a sign, a mantissa, and an exponent, standardized by IEEE 754 in 1985; it trades exactness for enormous range.
- mantissa
- The significant-digits field of a floating-point number (also called the significand or fraction), which controls precision; float32 gives it 23 bits.
- exponent
- The field of a floating-point number that scales the mantissa by a power of two, controlling range; when it cannot hold a needed power, the number overflows.
- quantization
- Reducing the bits used per number — for example mapping float32 model weights onto 256 int8 levels plus a scale factor — shrinking memory and data movement at a small cost in precision.
- endianness
- The convention for which byte of a multi-byte number is stored first in memory: least-significant first (little-endian, x86 and most ARM) or most-significant first (big-endian, traditional in network protocols).
- gibibyte
- The binary giga-unit, GiB = 1,024³ bytes, defined by the IEC in 1998 to end the ambiguity with the decimal gigabyte (GB = 10⁹ bytes) that makes a "1 TB" drive report about 931 GiB.
Sources and further reading
- Binary number — Wikipedia (accessed 2026-07-12) — Place value, base conversion methods, binary arithmetic, and the Leibniz history.
- Two's complement — Wikipedia (accessed 2026-07-12) — The invert-and-add-one construction, the asymmetric range, and why one adder serves both signs.
- IEEE 754 — Wikipedia (accessed 2026-07-12) — The 1985 floating-point standard: field layouts of the binary formats and rounding behavior.
- Code: The Hidden Language of Computer Hardware and Software (2nd ed.) — Charles Petzold / Microsoft Press (accessed 2026-07-12) — The gentlest thorough treatment of binary codes, bytes, and hexadecimal in print.
- Crash Course Computer Science — PBS Digital Studios (accessed 2026-07-12) — Episode 4 covers binary and data representation; episode 5 builds the ALU that adds it.
Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.