How Computers Work for Beginners / Chapter 1 — Numbers and Representation (lessons 1–4) / Lesson 1
Bits and Bytes (Looking at the Layout in Memory)
Time needed: about 5 min (estimate)
Reading settings
These settings are remembered on this device only.
You can try the opening of this lesson without an account.
What is this one byte?
Memory is a single long tape of bytes, nothing more and nothing less.
A byte is 8 bits, which means eight 0s and 1s.
The pattern 0110_0001 is 97 if you read it as an integer, 'a' if you read it as a character, and a dark red if you read it as a color.
Bits have no meaning in themselves; the meaning lies in how you read them.
This course spends 30 lessons checking that "how you read them," one piece at a time.
size.c and its output
In what order are the bytes of a 4-byte integer stored?
If you put the 4-byte number 0x12345678 in memory, will it be laid out as 12 34 56 78, or the other way around?
The standard doesn't say either way.
Let's copy it into a byte array with memcpy and look at it one byte at a time.
endian.c
The low byte comes first: little-endian
Here's what you get when you run it.
The lowest byte, 78, is at the lowest address.
This is called little-endian, and it's what nearly every machine you'll touch today uses, Apple Silicon and Intel x86-64 alike.
The opposite, with 12 first, is big-endian, and it's the standard form network protocols use to carry numbers (network byte order).
Terminal
Read it in the wrong direction and you get a different number
The same 4 bytes become a completely different number if you read them in the wrong direction.
The code below runs right in your browser.
When a number you read from a file or the network comes out wildly wrong, suspect this first.
Tab indents. To leave the editor, press Esc, then Tab.
Create a free account to keep reading
With an account you can read the rest of this lesson, take the quiz, and read every free lesson in this course. Lessons you answer correctly are recorded as complete.
Report a problem with this lesson
Log in to send a report.