Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Binary, Hex and Endianness  ›  Lesson

Hexadecimal — Four Bits per Digit

Endianess 12 minHexadecimal is base sixteen, and because one hex digit stands for exactly four bits, two hex digits describe exactly one byte.

Hexadecimal — Four Bits per Digit

Look at a colour code like #FF8800, a memory address like 0x7ffe, or a network card's address like 3c:22:fb:10:9a:01. They are all hexadecimal. Programmers did not pick base sixteen to be difficult. They picked it because it lines up perfectly with bits.

The rule

One hex digit is four bits; two hex digits are one byte.

Hexadecimal (hex) is base 16. It needs sixteen digits, so after 0–9 it borrows letters:

hex:     0  1  2  3  4  5  6  7  8  9  A   B   C   D   E   F
decimal: 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15
binary:  0000 … 1001 for 0–9, then 1010 1011 1100 1101 1110 1111

Four bits have exactly 16 patterns, so each hex digit stands for one group of four bits (sometimes called a nibble). A byte is two nibbles, so it is always exactly two hex digits, from 00 to FF. A 32-bit number is always 8 hex digits.

Hex is usually marked with a 0x prefix, as in 0x2F, so it is not confused with decimal.

For the decimal value, the places are worth 1, 16, 256, … So 0x2F = 2 × 16 + 15 = 47.

The shortcut

Never convert hex to binary through decimal. Translate each hex digit into its own four bits, and glue the groups together. Going the other way, split the bits into fours from the right and translate each group.

Translating 0x3A to binary and decimalStep 1 of 4
  1. Split into digits

    0x3A has two digits: 3 and A.

The trap: treating 0x10 as ten. In hex, 10 means one sixteen and no ones, which is decimal 16.

Drills

Answer fast — trust the rule.

quick drill

What is 0x1A in decimal?

quick drill

What is the byte 1100 0011 in hex?

quick drill

How many hex digits does it take to write any 32-bit number?

quick drill

What is 0xFF in decimal?

quick drill

In the colour #FF8800, how much of each colour channel is there?

Remember

  • One hex digit is four bits; two hex digits are one byte.
  • Translate digit by digit between hex and binary; never go through decimal.
  • The trap: 0x10 is sixteen, not ten — always check the prefix.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy programmers write hex

Explain why programmers write memory contents and colours in hexadecimal rather than binary or decimal. Use the byte 11000011 as an example, converting it to hex and to decimal.