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)

☆ Read later

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

#include <stdio.h> int main(void) { printf("char %zu bytes\n", sizeof(char)); printf("short %zu bytes\n", sizeof(short)); printf("int %zu bytes\n", sizeof(int)); printf("long %zu bytes\n", sizeof(long)); printf("void * %zu bytes\n", sizeof(void *)); return 0; } $ clang -O0 size.c -o size && ./size char 1 bytes short 2 bytes int 4 bytes long 8 bytes void * 8 bytes

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

#include <stdio.h> #include <string.h> int main(void) { unsigned int x = 0x12345678; unsigned char b[4]; memcpy(b, &x, 4); // copy the 4 bytes where x lives, as they are printf("x = 0x%08x\n", x); printf("bytes in memory: "); for (int i = 0; i < 4; i++) printf("%02x ", b[i]); printf("\n"); return 0; }

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

$ clang -O0 endian.c -o endian && ./endian x = 0x12345678 bytes in memory: 78 56 34 12

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.

editor.py

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.

Keyboard shortcuts

Keys available in a lesson
KeyAction
[ or Alt + ←Go to the previous lesson
] or Alt + →Go to the next lesson
?Open this list
EscClose this list

The keys do not work while the cursor is in a code editor or an input field.