Lesson
The theory — Memory and Bytes: Endianness, Types and Addresses
A byte is eight bits, a value from 0 to 255, and each address in memory holds one. A wider number takes consecutive addresses: four for a 32-bit integer. Endianness is the order of those bytes. Little-endian puts the least significant byte at the lowest address, big-endian the most significant. Read as an unsigned integer, four bytes are b₀·256⁰ + b₁·256¹ + b₂·256² + b₃·256³, where bₖ is the byte holding place k. A signed read subtracts 2³² when that sum is 2³¹ or more, and a float read decodes the same bits by IEEE 754 instead.
What each symbol means
bₖ- the byte in place k, from 0 (least significant) to 3
256ᵏ- its place value: 1, 256, 65,536 or 16,777,216
- Assumes
- Signed integers are two's complement and floats are IEEE 754 single precision, as on every current desktop and phone processor. Text is one byte per character, ASCII; UTF-8, which most files now use, takes up to four bytes for one character.
Problem solved in full
-
How big is this picture? 4 steps
Press PNG header. These are the first 24 bytes of a PNG file. The PNG specification says every integer wider than a byte is big-endian, and that the IHDR chunk begins with the width and then the height, four bytes each. Find the width and height, and work out what a program that read them little-endian would think it had.
-
The file opens with an eight-byte signature, 89 50 4E 47 0D 0A 1A 0A. The next four bytes are the first chunk's length, big-endian: 00 00 00 0D is 13. Then its type, 49 48 44 52, which as text is IHDR. So the chunk's data starts at address 16.
-
Width, addresses 16 to 19: 00 00 01 E0. Big-endian, the 01 is in the 256¹ place and E0, 224, in the 256⁰ place: 256 + 224 = 480. The tool's read at 16 prints the same.
-
Height, addresses 20 to 23: 00 00 01 0E, which is 256 + 14 = 270.
-
Little-endian, the last byte becomes the most significant. The width would be 224 × 256³ + 1 × 256² = 3,758,161,920 and the height 14 × 256³ + 256² = 234,946,560.
Answer
480 × 270 pixels. Read little-endian, the same bytes claim 3,758,161,920 × 234,946,560, about 8.83 × 10¹⁷ pixels, which at four bytes a pixel is 3.53 × 10¹⁸ bytes. The order is not in the file. It is in the specification, and a program has to be told it.
-
Learning path
How a computer runs a program
References (2)
- Where the names little-endian and big-endian come from, and the date the first insight gives: Danny Cohen, "On Holy Wars and a Plea for Peace." Internet Experiment Note IEN 137, 1 April 1980.
- The worked problem's file: the signature bytes, network byte order for every multi-byte integer, and the IHDR fields in order: W3C, "Portable Network Graphics (PNG) Specification (Third Edition)." W3C Recommendation, 24 June 2025.