Binary to decimal and hexadecimal, explained

Base conversion looks like computer magic, but it comes down to adding place values and grouping bits in fours. Here is the one table worth memorising, each method with a worked example, and the slips that trip people up.

The core idea: every position has a weight

The decimal number 352 means 3 × 100 + 5 × 10 + 2 × 1. Each position is worth ten times the one to its right, because we count in base 10.

Binary works the same way in base 2. There are only two digits, 0 and 1, and each position is worth twice the one to its right. Reading from right to left, the weights are 1, 2, 4, 8, 16, 32, 64, 128 and so on.

Hexadecimal is base 16. It needs sixteen digits, so after 9 it borrows letters: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Its weights are 1, 16, 256, 4096…

To keep bases apart, write the base as a subscript (1010₂, 10₁₀, A₁₆) or use the prefixes programmers use: 0b for binary (0b1010) and 0x for hex (0xA).

The 0–15 table

A group of four bits, called a nibble, runs from 0000 to 1111: 0 to 15, which is exactly one hex digit. This table unlocks everything else. Know it by heart and binary-to-hex becomes a lookup, not a calculation.

Decimal Binary Hex
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

Two anchors make it stick: odd numbers end in 1, and 8 (1000) is the halfway mark. From there, 9 is 8 + 1 (1001), C is 8 + 4 (1100), and the rest follow.

Binary to decimal: add up the weights

Write the weights above the bits, starting with 1 on the right, then add only the weights that sit over a 1.

Example: 1011 0110₂

Weight 128 64 32 16 8 4 2 1
Bit 1 0 1 1 0 1 1 0

128 + 32 + 16 + 4 + 2 = 182.

When a number is mostly ones, subtracting is quicker. 1111 1110₂ is a full byte (255) minus 1, so 254.

Decimal to binary: two methods

Subtract powers of two. Find the largest power of two that fits, subtract it and put a 1 in that position. Repeat with the remainder; every position you skip gets a 0.

Take 200: 128 fits (72 left), 64 fits (8 left), 32 and 16 don’t, 8 fits (0 left), and 4, 2 and 1 don’t. Result: 1100 1000₂.

Divide by two. Divide again and again, writing down each remainder. Read the remainders from the bottom up and you have the binary number.

Take 156:

Division Quotient Remainder
156 ÷ 2 78 0
78 ÷ 2 39 0
39 ÷ 2 19 1
19 ÷ 2 9 1
9 ÷ 2 4 1
4 ÷ 2 2 0
2 ÷ 2 1 0
1 ÷ 2 0 1

Bottom to top: 1001 1100₂. Check: 128 + 16 + 8 + 4 = 156.

Subtracting powers is faster in your head for small numbers. Division is mechanical and safe for big ones.

Binary and hex: four bits at a time

Because one hex digit is exactly four bits, you never need to go through decimal:

  1. Split the bits into groups of four starting from the right.
  2. If the leftmost group is short, pad it with zeros.
  3. Swap each group for its digit from the table.

So 1011 0110₂ → 1011 = B and 0110 = 6 → B6₁₆. Going back is the same in reverse: each hex digit expands to its four bits, so 3F₁₆ = 0011 1111₂.

Hex to decimal and back

Hex to decimal: multiply each digit by its weight (1, 16, 256, 4096) and add.

  • 2F₁₆ = 2 × 16 + 15 = 47
  • 0x1A3 = 1 × 256 + 10 × 16 + 3 = 256 + 160 + 3 = 419
  • B6₁₆ = 11 × 16 + 6 = 182, the same number as the binary example above.

Decimal to hex: divide by 16 and collect the remainders, exactly as with base 2. Remainders from 10 to 15 become letters.

  • 419 ÷ 16 = 26, remainder 3
  • 26 ÷ 16 = 1, remainder 10 (A)
  • 1 ÷ 16 = 0, remainder 1

Bottom to top: 1A3₁₆. One more: 2024 leaves remainders 8, 14 (E) and 7, so 2024 = 7E8₁₆.

Where you will actually meet it

  • Web colours: #FF8800 is three bytes: red FF (255), green 88 (136), blue 00 (0). That mix is orange.
  • Bytes: a byte is 8 bits, 0 to 255, always written as two hex digits (00 to FF).
  • Networking: the subnet mask 255.255.255.0 is 24 ones followed by 8 zeros. MAC addresses are six bytes in hex (like 3C:22:FB:0A:41:9E), and IPv6 addresses are eight 16-bit groups in hex.
  • Code and electronics: bit flags, memory dumps, character codes and microcontroller registers are all read in hex or binary.

Common mistakes and quick checks

  • Grouping from the left. 101101₂ grouped the wrong way gives 1011 01 → B1, which is 177. Grouped from the right: 10 1101 → 0010 1101 → 2D₁₆ = 45.
  • Dropping leading zeros. In a byte, 5 is 0000 0101. The value doesn’t change, but it matters when you compare bits or fill a fixed-width field.
  • Off-by-one letters. A is 10, not 11. If the letters blur, remember F = 15 and count back.
  • Reading remainders top-down. The first remainder is the rightmost digit.

For sanity checks, learn the powers of two:

2⁰ 2¹ 2² 2³ 2⁴ 2⁵ 2⁶ 2⁷ 2⁸ 2¹⁰ 2¹² 2¹⁶
1 2 4 8 16 32 64 128 256 1,024 4,096 65,536

An n-bit number never exceeds 2ⁿ − 1: a byte tops out at 255 and 16 bits at 65,535 (FFFF₁₆). If your answer lands outside that range, something slipped.

How to make it automatic

Understand each method once, then drill: speed comes from recognising all sixteen nibbles without thinking. The binary and hex game is built in that order. 4 bits (0–15) locks in the table, 1 byte (0–255) chains two nibbles, and 16 bits (0–65,535) is for when you are fluent. Each round lasts 60 seconds, and the bit helper shows the weights while you add; turn it off once you stop glancing at it. It accepts 0x and 0b prefixes and spaces between groups, so you can type answers the way you would in code.

FAQ

Why do computers use hex instead of decimal?

One hex digit maps to exactly four bits and two digits to one byte. You convert from binary without arithmetic, and the result is far shorter to read: FFFF instead of 1111 1111 1111 1111.

What is the largest number a byte can hold?

255 in decimal, which is 1111 1111 in binary and FF in hex. Eight bits give 256 possible values, from 0 to 255.

What do 0x and 0b mean in front of a number?

They are prefixes used by most programming languages: 0x marks hexadecimal (0x1A3 = 419) and 0b marks binary (0b1010 = 10). They don’t change the value; they only say which base it is written in.

Put it into practice

Binary and hex

Convert between binary, decimal and hex against the clock.

Play Binary and hex →