← Binary Course — Part A, lesson 3 of 4 (Counting in other bases)
3. Hex: Binary in Groups of Four
The concept
Binary is precise, but it's painful for a person to read, write, or copy by hand. An 8-bit number is already eight symbols to keep straight, and real machine words are often twice or four times that long — a long run of visually similar 0s and 1s is exactly the kind of thing an eye slips on while copying it down. Hex exists to fix that readability problem, not to give the computer a third way to store numbers — underneath, it's still plain binary. Hex is just a shorthand for writing binary more compactly.
The shorthand works because of a coincidence of arithmetic: a group of 4
bits has exactly 16 possible patterns (0000 through 1111), and 16 is
also how many symbols a single hex digit can hold. That match means every
possible 4-bit pattern maps to exactly one hex digit, with none left over
and none overlapping. So the whole trick is: split a binary number into
groups of 4 bits (called nibbles) starting from the right, then replace
each nibble with the one hex digit that stands for it.
Since a nibble can hold 16 different values, hex needs 16 different digit
symbols — more than decimal's 0-9. It reuses 0-9 for the first ten values,
then continues with the letters a through f for the remaining six:
binary decimal hex
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 a
1011 11 b
1100 12 c
1101 13 d
1110 14 e
1111 15 f
This lesson writes hex numbers with a 0x prefix and lowercase letters —
0x4c, not 4C or $4c — so a hex number is never mistaken for a decimal
one or a variable name. And unlike binary, hex numbers don't need internal
grouping to stay readable: each digit already is one nibble, so hex is
already about as compact as this shorthand gets.
Example 1 — binary to hex
Take 0110 1101 — the same 8-bit number from Lesson 2's Example 2, whose
decimal value was 109. It's already split into two nibbles. Convert each
one using the table above:
0110 → 6
1101 → 13 → d
So 0110 1101 is 0x6d in hex. Nothing about the value changed — 109,
0110 1101, and 0x6d are three different ways of writing the exact same
number.
Example 2 — hex to binary, then to decimal
Take 0xb4 and reverse the process: expand each hex digit back into its
4-bit nibble.
b → 1011
4 → 0100
So 0xb4 is 1011 0100 in binary. From there, Lesson 2's place-value
method gives the decimal value: bits 7, 5, 4, and 2 hold a 1, with place
values 128, 32, 16, and 4:
128 + 32 + 16 + 4 = 180
0xb4 is 180.
Example 3 — why octal doesn't fit as neatly (a brief aside)
Some historic machines grouped bits in threes instead of fours (octal, digits
0-7) rather than hex — it's the same shorthand idea, just with a
3-bit group instead of a 4-bit one. The catch is that 8-bit widths don't
divide evenly by 3: splitting 1011 0100 into groups of three from the
right gives 10 110 100 — two clean 3-bit groups, but a leftover
2-bit group at the front that needs an extra padding zero (010) before it
means anything. Hex never has this problem for the widths this course
uses, because 8, 16, and 32 are all multiples of 4 — groups of four always
come out even. That clean fit, not any deeper advantage, is why hex is this
course's default shorthand and octal only gets a passing mention.
Exercises
Exercise 1. Using Example 1's method, what's the hex form of the 8-bit
binary number 1001 0111?
Exercise 2. What decimal value does 0xc5 represent? (First expand it
to binary using the nibble table, then find the decimal value using Lesson
2's place-value method, the same two-step path Example 2 used.)
Exercise 3. A classmate converts 1010 1100 to hex. They split it into
the correct nibbles, 1010 and 1100, but then read each nibble
backwards — treating its leftmost bit as the lowest place value instead
of the highest. Doing that to 1010 gives them 5, and doing it to 1100
gives them 3, so they write 0x53. What did they get wrong, and what's the
correct hex value?
Show Answer Key
1. 1001 → 9, 0111 → 7, so 1001 0111 is 0x97.
2. c → 1100, 5 → 0101, so 0xc5 is 1100 0101 in binary. Bits 7,
6, 2, and 0 hold a 1, with place values 128, 64, 4, and 1:
128 + 64 + 4 + 1 = 197. 0xc5 is 197.
3. They reversed which end of the nibble counts as the low bit —
the same kind of indexing mistake Lesson 2 warned against, just applied
inside a single 4-bit group instead of across a whole byte. Read correctly
(leftmost bit of the nibble is the highest place value, rightmost is the
lowest, same as every wider binary number in this course), 1010 is 8 + 2
= 10 = a, and 1100 is 8 + 4 = 12 = c. The correct hex value is
0xac.
Sign in to mark lessons complete and keep your progress.