In systems programming, data is not abstract numbers; it is a sequence of bits physically stored in hardware. Mastering the representation of this data is the first step to “seeing the matrix.”
The Intuition of Bases
We are used to Base 10 (Decimal) because we have ten fingers. Computers use Base 2 (Binary) because they have switches (transistors) that are either ON or OFF.
Hexadecimal: The Programmer’s Shorthand
Writing out 32 bits of binary (11011010...) is prone to error and hard to read. Hexadecimal (Base 16) is not just another random base; it is the perfect compression algorithm for binary.
- Fact: .
- Implication: Exactly 4 bits map to 1 hex digit.
This perfect alignment means you never need to do math to convert Binary Hex. You just do pattern matching.
| Pattern | Hex | Dec | Mnemonic / Logic |
|---|---|---|---|
0000 | 0 | 0 | Empty. |
0001 | 1 | 1 | Just the 1s bit. |
0010 | 2 | 2 | Just the 2s bit. |
0011 | 3 | 3 | 2 + 1. |
0100 | 4 | 4 | Just the 4s bit. |
0101 | 5 | 5 | 4 + 1. |
0110 | 6 | 6 | 4 + 2. |
0111 | 7 | 7 | 4 + 2 + 1. All low bits on. |
1000 | 8 | 8 | The MSB of the nibble. |
1001 | 9 | 9 | 8 + 1. |
1010 | A | 10 | 8 + 2. (Ten starts with T, but we use letters now). |
1011 | B | 11 | 8 + 2 + 1. |
1100 | C | 12 | 8 + 4. |
1101 | D | 13 | 8 + 4 + 1. |
1110 | E | 14 | 8 + 4 + 2. Even (E) number, almost full. |
1111 | F | 15 | Full. All bits on. |
The 3 Critical Patterns (Memorize These)
0xF=1111(All ones).0x5=0101(Alternating 1s, starting with 1).0xA=1010(Alternating 1s, starting with 0).
Byte Ordering: Endianness
This is where the abstraction of “numbers” leaks.
In math, the number 4,123 is abstract. In a computer, a 32-bit integer is 4 bytes. Memory is a linear array of single bytes. We have to decide: which byte goes first?
Consider the hex value: 0xDEADBEEF.
- Bytes:
DE,AD,BE,EF. - Most Significant Byte (MSB):
DE(contains the highest value bits). - Least Significant Byte (LSB):
EF(contains the lowest value bits).
The Two Schools of Thought
1. Big Endian (“The Human Way”)
Store the Biggest end first. Unconsciously, we write numbers 4,123 with the “thousands” place (biggest) on the left. Big Endian mimics this mapping to memory addresses.
Memory Layout:
Address 0x00: DE (MSB)
Address 0x01: AD
Address 0x02: BE
Address 0x03: EF (LSB)
Where is it used? Network Protocols (TCP/IP), Legacy IBM Mainframes.
2. Little Endian (“The Math Way”)
Store the Littlest end first at the lowest address. This seems backwards to humans reading a memory dump, but it makes perfect sense for the mechanical adder logic (add LSBs first, carry to higher bits).
Memory Layout:
Address 0x00: EF (LSB)
Address 0x01: BE
Address 0x02: AD
Address 0x03: DE (MSB)
Where is it used? x86 (Intel/AMD), most ARM (Android/iOS), Apple Silicon.
Visualizing the Difference
Imagine writing the number 0x1234 (two bytes) to memory addresses A and A+1.
MSB LSB
Val: 12 34
Big Endian: [ 12 ] [ 34 ]
Addr: A A+1
Little Endian: [ 34 ] [ 12 ]
Addr: A A+1The C Casting Trick
You can observe Endianness directly in C by “fooling” the type system. You treat a multi-byte int as an array of chars.
#include <stdio.h>
int main() {
int i = 1; // 0x00000001
char *c = (char*)&i; // Look at the first byte of i
if (*c == 1) {
printf("Little Endian\n"); // The '1' was at the first address
} else {
printf("Big Endian\n"); // The '1' was at the last address (so we saw 00)
}
}The Network Byte Order Trap
When writing networking code, you must convert your host’s integer representation (likely Little Endian) to the network’s standard (Big Endian). Use helper functions:
htons()(Host to Network Short),htonl()(Host to Network Long).
Practical Drills
Drill 1: The “Sanity Check”
You see a memory address 0x7fff0010 contains 0A 00 00 00. You interpret this as a 32-bit int.
- Little Endian Interpretation: The bytes are reversed. .
- Big Endian Interpretation: The bytes are straight. .
Lesson: If you see a small number like 10 represented as
0A 00...on an Intel mac, that’s Little Endian working correctly.
Drill 2: Binary Arithmetic
What is 0010 1100 + 0001 0101?
0010 1100 (44)
+ 0001 0101 (21)
-----------
0100 0001 (65)Tip: Do it column by column, just like decimal math. 1+1 = 0 carry 1.
Drill 3: Large Hex Conversion
Convert 0x800 to decimal.
- .
- .
- . Or memorize: , so .