SYSTEMS / Learn
Saved on this device>
← Back to curriculum

Learning-oriented

Put the bytes in order

Arrange bytes in memory and convert between little-endian and big-endian.

1 · Understand → 2 · Explain from memory → 3 · Practise → 4 · Spaced review

A byte contains eight bits. A 32-bit integer takes four consecutive bytes in memory. Endianness describes which byte is stored at the lowest address, not how bits are ordered inside a byte.

Big-endian stores the most significant byte first. For 0x12345678, increasing memory addresses contain 12 34 56 78. Little-endian stores the least significant byte first, so the same value is stored as 78 56 34 12.

To convert a single integer between these memory layouts, reverse its byte order. Each two-digit hex byte stays intact: AB stays AB, not BA. Leading zero bytes still occupy memory. The integer’s value stays the same when you interpret the new bytes using the new endianness.

Explain it before you practise

Look away from the example. Explain why it works and predict a boundary case. Then check your reasoning.

Store 0x00AB as little-endian bytes. Why must the zero byte remain, and why does AB not become BA?

Check your reasoning

At increasing addresses the bytes are AB 00. A 16-bit value occupies two bytes even if one is zero. Endianness changes byte order, not the two hex digits or bits inside a byte.

Cement this knowledge

Solve ten fresh questions without the worked example. Mistakes return during the session; spaced review helps you recall the method on later days.

Practice this topic →
coffee-bytes / Systems LearningCS:APP + Hacker’s Delight