A byte means nothing until two sides agree how to read it
Bits carry no meaning of their own. A file type, a data type or a protocol is the agreement that says which reader to use.
Everything a computer stores is bits. Here are eight of them, one byte. They never change in this picture. What changes is the reader. Read as a whole number, they are sixty-five. Read as text, the letter A. Read as a pixel, a dark grey dot. Read by a processor, an instruction to add one. The agreement between writer and reader decides which. When the two sides use different agreements, nothing fails loudly. The bytes just decode wrong, like the garbled accents you have seen on web pages.
Bytes and encodings · Going deeper · 1 of 2
The whole stack, bit to wire
This lesson climbs one stack of agreements: bits, bytes, numbers, text, media, then the tools that work on raw bytes. Most “corrupted file” symptoms are agreement mismatches: a JPEG opened as text, an integer field read as a float.
Bytes and encodings · Going deeper · 2 of 2
One byte, three readings
The agreement lives in file headers, type systems and protocol specifications. The bits 01000001 are identical in every case; only the decoder differs.
Bits and binary
Two states survive noise, and every extra bit doubles what you can say
A bit is the smallest distinction real matter can hold reliably. Stack n of them and you get 2ⁿ patterns.
Why only ones and zeros? Real matter is noisy. A voltage never sits still. But if all you ask is whether it is above or below one line, the answer survives the noise. That single decision is a bit. One bit gives two patterns. Each extra bit doubles them: four, eight, sixteen, thirty-two, sixty-four. Eight bits give two hundred and fifty-six patterns. Thirty-two bits give about four point three billion. Sixty-four bits give more than you could ever count through. Almost every design decision in computing is how many bits to spend, and what that buys.
Bits and binary · Going deeper
The substrate does not matter
Voltage above or below a threshold, a magnetic domain pointing one way or the other, an optical pit present or absent: all are bits. Doubling the bit count squares the number of patterns, which is why 64-bit addresses will not run out the way 32-bit ones did.
Integers in binary
A number is the lit columns added up
Binary works like decimal with columns worth 1, 2, 4, 8. The number of columns is fixed, so every integer has a ceiling.
The first agreement is counting. Each column has a value that doubles as you move left: one, two, four, eight, up to one hundred and twenty-eight. Add the columns that hold a one. Here that is sixty-four plus one: sixty-five. Programmers rarely write the bits out. They group them in fours and write one hex digit for each group, so this byte is four one. Eight columns can only reach two hundred and fifty-five. Add one more and the value wraps round to zero. That is integer overflow, and it is behind many real bugs.
Integers in binary · Going deeper · 1 of 3
One value, four notations
Decimal is for people, binary is what hardware stores, and hex is the readable shorthand because each hex digit is exactly four bits. Two hex digits cover one byte, which is why hex dominates debuggers, colour codes and addresses.
Integers in binary · Going deeper · 2 of 3
Negative numbers: make the top column negative
In two’s complement the top column is worth −128 instead of +128, so 1111 1011 is −5. To negate, invert every bit and add 1. The payoff: one adder handles both signs, and a − b is just a + (−b). The range is lopsided, −128 to +127, so negating the lowest value overflows.
Integers in binary · Going deeper · 3 of 3
Working on single bits
AND keeps only the bits in a mask, OR sets bits, XOR toggles them, NOT inverts all. They are how packed flags are read and written. Shifting left by one doubles a value in a single cycle, which is why compilers turn x × 2 into a shift.
Floating point
A fraction is stored as the nearest value, not the exact one
A float trades exactness for range. Most decimal fractions, 0.1 included, do not exist in binary, so they are rounded on the way in.
Whole numbers are exact. Fractions are not. A float splits its thirty-two bits into a sign, a field that says where the point sits, and twenty-three bits of digits, good for about seven decimal digits. That means only certain values exist. Here is the number line near zero point one, zoomed in a hundred million times. The ticks are the only values a float can hold. Zero point one falls between two ticks, so the nearest tick is stored instead. That is why zero point one plus zero point two does not equal zero point three. It is not a bug; it is the format. So money is counted in whole cents, and floats are compared with a tolerance.
Floating point · Going deeper · 1 of 2
IEEE 754, field by field
Single precision is 32 bits split 1, 8 and 23; double is 64 bits split 1, 11 and 52. The stored exponent has a bias of 127 added. A few exponent values are reserved for ±0, ±infinity and NaN, the result of 0 ÷ 0.
Floating point · Going deeper · 2 of 2
Where 0.1 goes wrong
0.1 in binary is a repeating fraction, cut off at 23 or 52 bits. The stored float32 is off by about 1.5 × 10⁻⁹, and adding two slightly-off numbers compounds the error. More bits shrink the error; they never remove it.
Check yourself
A shop adds 0.10 and 0.20 as floats, and the total fails a check against 0.30. What is the right fix?
More bits make the error smaller but never zero. Whole cents are integers, and integers are exact.
Unicode and UTF-8
UTF-8 stores every script in one to four bytes
Unicode gives every character a number. UTF-8 turns that number into bytes, and the first bits of each byte say how to read it.
Text is the next agreement. Unicode gives every character in every script a number. UTF-8 decides how that number becomes bytes. An English letter takes one byte, starting with a zero. An accented letter takes two. The euro sign, like most other scripts, takes three. An emoji takes four. The purple bits are the agreement itself. They tell the reader how many bytes belong together, and every continuing byte starts with one zero, so a reader that loses its place finds the next character straight away. Plain English files were already valid UTF-8, which is why it won.
Unicode and UTF-8 · Going deeper · 1 of 3
Number first, bytes second
Unicode has 1,114,112 slots in 17 planes, written U+0000 to U+10FFFF. It only assigns numbers; UTF-8, UTF-16 and UTF-32 are separate ways to write them as bytes. Confusing the two is behind half of all encoding bugs.
Unicode and UTF-8 · Going deeper · 2 of 3
Why string length lies
A codepoint is a number, a glyph is a drawing, and a grapheme is what the reader sees as one character. A family emoji is seven codepoints in one grapheme, so most languages report its length as 7 or 11, not 1.
Unicode and UTF-8 · Going deeper · 3 of 3
One word, two byte sequences
“café” can store é as one codepoint or as e plus a combining accent. They look identical and compare unequal. Normalise on the way in (NFC) for filenames, identifiers and database keys; Linux and macOS disagree on the default.
é was saved as two UTF-8 bytes, C3 A9. A reader using an older one-byte-per-letter encoding shows each byte as its own character.
Sampling images and sound
Pictures and sound are measurements, and too few of them invent things
Sound and light are continuous, so we sample them: measure at fixed steps. What is not captured then can never be recovered.
A sound is a smooth wave, and bits cannot hold smooth. So we measure it. Take enough measurements, join the dots, and the wave comes back. A CD takes forty-four thousand one hundred a second. Now measure the same wave too rarely. The dots trace a slow wave that was never in the room. This is aliasing, and no software can remove it afterwards. The rule is to measure at least twice per cycle of the highest pitch you want to keep. A picture works the same way, on a grid of pixels. And all those measurements are huge: one second of raw video is three gigabits. That is why the next idea exists.
Sampling images and sound · Going deeper · 1 of 2
Three media, one trick
An image is a grid of pixels at 8 bits per colour channel, 16.7 million shades. CD audio is 44,100 samples a second, 16 bits, two channels: 1.41 Mbps. Each extra bit of depth adds about 6 dB of range, so 16 bits give 96 dB. Decode with the wrong depth or channel count and you get noise.
Sampling images and sound · Going deeper · 2 of 2
Aliasing, precisely
Nyquist–Shannon: to keep frequencies up to f you need at least 2f samples a second. 44.1 kHz captures up to about 22 kHz, just above human hearing. Anything higher must be filtered out before sampling, in the analogue domain.
Compression
Compression gives the common things the short codes
Real data repeats. Compression spends fewer bits on what is frequent, down to a floor set by how predictable the data is.
Real data is repetitive, and compression exploits that. Take this word of eleven letters. Stored plainly, each letter costs eight bits. But A appears five times, so give it the shortest code: a single bit. The rarer letters get three bits each. Add it up: eighty-eight bits become twenty-three, and every letter comes back exactly. There is a hard floor, worked out by Claude Shannon. For this word it is twenty-two point four bits, so this code is almost perfect. Lossy formats like JPEG and MP3 go below the floor by also throwing away detail you cannot see or hear.
Compression · Going deeper · 1 of 3
The floor: entropy
Entropy measures how unpredictable a source is, in bits per symbol. Random bytes need all 8 and cannot shrink. A stream that is 99% one letter carries almost nothing per byte. No lossless coder beats this on average.
Compression · Going deeper · 2 of 3
Pointing back instead of repeating
LZ77 slides a window over the input and replaces a repeated string with “go back 12 bytes, copy 4”. gzip, ZIP and PNG run this first, then Huffman codes on what is left. Together they are called DEFLATE.
Compression · Going deeper · 3 of 3
Lossy: pick the elbow
JPEG turns 8 × 8 pixel blocks into frequencies and drops the fine detail the eye barely sees. The first drop in quality is almost free; the last step to “perfect” costs ten times the bytes. Video codecs add motion between frames for 100 to 500 times.
Error detection and correction
Spare bits are how a machine notices its own mistakes
Disks, memory and radio flip bits. Adding redundant bits lets the receiver detect a flip, and with enough of them, repair it.
Bits do not always come back the way they went in. So the sender adds a spare bit, set so that the number of ones is even. If the byte arrives intact, the count is still even, and it is accepted. If one bit flips, the count turns odd. The receiver knows something broke and asks again. But if two bits flip, the count is even again, and a wrong byte is accepted. One spare bit is not much protection. Thirty-two spare bits per message, a CRC, catch whole bursts of damage. And with about twelve percent more bits, server memory repairs a flipped bit on the spot, with no resend.
Error detection and correction · Going deeper
Detect or correct?
It depends on whether asking again is cheap. TCP can resend, so it only detects. Memory cannot, and a space probe is light-minutes away, so they correct. ECC memory fixes any single-bit error and detects any double-bit error per word; storage and radio use stronger codes such as Reed–Solomon and LDPC.
Check yourself
A byte is sent with one parity bit. On the way, two of its bits flip. What does the receiver do?
Parity only knows odd from even. Two flips cancel out, which is why real links use 32-bit checks instead.
Hashes
A hash is a fixed-size fingerprint of any amount of data
A hash function turns any input into a short fixed-size value. Change one bit of the input and the whole value changes.
Often you need to know whether two pieces of data are the same without comparing every byte. A hash function takes the word hello and produces two hundred and fifty-six bits. Add one exclamation mark, and the result has nothing in common with the first. Feed in a four gigabyte film, and the result is still just two hundred and fifty-six bits. The same input always gives the same fingerprint, and you cannot work backwards from it. So if two fingerprints match, the data matches. Git names every version of your code this way.
Hashes · Going deeper
Two families for two jobs
Fast hashes (xxHash, MurmurHash) spread keys evenly for hash tables, which stay O(1) while the table is under about 70% full. Cryptographic hashes (SHA-256, BLAKE3) are slower and built so that nobody can craft two inputs with the same output. Inside, the input is padded, cut into blocks and stirred round after round into a fixed state.
Serialization
To leave a program, data is flattened to bytes both ends agree on
Serialization turns structured data into bytes. The format is a contract: break it and the other side decodes nonsense without an error.
The last step is sending data to another program. Here is one record written as JSON. It takes twenty bytes, because the field names travel inside the data. Anyone can read it. The same record in Protocol Buffers takes five bytes. The names are replaced by field numbers, and the meaning of each number lives in a schema that both sides hold. Even a single number needs an agreement. Most processors store the smallest byte first. If the reader assumes the opposite order, the same four bytes become a completely different number, with no error. So every format fixes the byte order in writing.
Serialization · Going deeper · 1 of 2
Byte order across machines
Big-endian stores the most significant byte first and is the convention on the network; little-endian dominates CPUs (x86, ARM). Inside one machine it is invisible. At the boundary, code converts with htonl and ntohl.
Serialization · Going deeper · 2 of 2
Changing a format without breaking old code
Protobuf identifies fields by number, not name. Add a field with a new number and old readers skip it. Retire one by reserving its number forever. Reuse a number for a different type and old code decodes garbage in production with no error.
Summary
Bits plus an agreement: that is all information is
Every layer here is fixed by a written standard, which is why a file made on one machine opens on another decades later. Next: the machine that moves these bits.
Here is the whole lesson as one stack. A bit is a decision that survives noise. Eight make a byte, with no meaning yet. Numbers, text, pictures and sound are the same bytes read by an agreed reader. Compression, error checks and hashes work on any bytes, whatever they mean. And serialization flattens structure for another program. Beside each layer is the written standard that fixes the agreement. Next lesson: the machine that stores and moves these bits.
Lesson complete
You finished “Information”
Answer the questions along the way to see your score here.