The C Path — learn C, visually

🧮 Foundations — Before C

Hexadecimal & Octal: binary for humans

⏱ 10 min · free interactive lesson · quizzes, visualizations & a real compiler

▶ Open the interactive lesson — free, no signup
Why you're learning this

You've already used hexadecimal without knowing it: every web color like #FF8800 is one, and so are the cryptic codes on a crashed blue screen. It's the shorthand that turns an unreadable wall of 0s and 1s into two characters per byte — and after this lesson you'll convert between the two in your head, no arithmetic required.

Binary is what machines speak, but for humans it's painfully verbose: a number like 0b11111111101010001001000000000000 is unreadable. Hexadecimal (base 16) fixes that: each hex digit packs exactly four bits, so any byte is just two characters.

Sixteen digits

Base 16 needs 16 symbols. We use 0–9, then borrow letters: A=10, B=11, C=12, D=13, E=14, F=15.

binaryhexdecbinaryhexdec
000000100088
000111100199
0010221010A10
0011331011B11
0100441100C12
0101551101D13
0110661110E14
0111771111F15

The trick: to convert binary ↔ hex you never do arithmetic — you just group bits in fours. 1011 0110B6. Done.

This spot has an interactive baseconv widget — open the interactive lesson to play with it.

🧠 Checkpoint: What is hex 0x2F in decimal?

  • 37
  • 47
  • 52
  • 215
Show answer

47 — 0x2F = 2×16 + 15 = 47. The F is 15, and the 2 sits in the sixteens place.

Hex in C: the 0x prefix

hex.c
#include <stdio.h>

int main(void) {
    int color = 0xFF8800;         /* an orange, as one int  */
    int red   = (color >> 16) & 0xFF;
    int green = (color >> 8)  & 0xFF;
    int blue  =  color        & 0xFF;

    printf("red=%d green=%d blue=%d\n", red, green, blue);
    printf("42 is 0x%x in hex, %o in octal\n", 42, 42);
    return 0;
}

Notice %x in printf prints a value as hex, and %o prints octal (base 8, prefix 0 — a leading zero!).

⚠️

Classic trap: in C, int x = 010; is octal — it means 8, not ten! A leading zero changes the base. Never zero-pad integer literals.

🧠 Checkpoint: In C, what is the value of int x = 011;?

  • 11
  • 9
  • 3
  • compile error
Show answer

9 — The leading zero makes it octal: 011 = 1×8 + 1 = 9. This surprising rule dates back to the 1970s — beware!

Where you'll meet hex daily

🧠 Checkpoint: How many bits does one hex digit represent?

  • 2
  • 4
  • 8
  • 16
Show answer

4 — One hex digit covers exactly 16 values = 2⁴ = 4 bits (a "nibble"). That is why two hex digits describe a byte perfectly.

▶ Practice this lesson interactively (with live gcc)