Developer Tools

Binary, hex and decimal: counting in bases 2, 10 and 16

Computers store everything as bits, but people read decimal, and programmers often use hexadecimal as a shortcut. The same number, 202, is 11001010 in binary and CA in hex. This guide shows how to convert between them by hand, why one hex digit equals four bits, and where you meet these systems every day.

By the Toolcloak team8 October 20264 min read
In this article
  1. Place value: the idea behind every base
  2. Reading a binary number
  3. Decimal to binary by dividing by 2
  4. Why hex exists
  5. How big can a number get?
  6. Where you meet these every day
  7. Quick tips

You count in tens because you have ten fingers. A computer counts in twos because a switch has two states. Hexadecimal, base 16, sits between them as a tidy way for people to write long strings of bits. None of these systems is harder than decimal. They only change what each position is worth.

Place value: the idea behind every base

In decimal, the number 305 means 3 hundreds, 0 tens and 5 ones. Each place is worth ten times the one on its right. Binary does the same with two. Hex does the same with sixteen.

What each binary place is worth (8 bits)
128bit 764bit 632bit 516bit 48bit 34bit 22bit 11bit 0

Every place is double the one to its right. Add up the places that are switched on to get the number. All eight on gives 255.

Reading a binary number

Take 11001010. Write the place values above each bit and add the ones that are on.

Place value1286432168421
Bit11001010
Counts?12864––8–2–

128 + 64 + 8 + 2 = 202.

202 as eight switches (filled = on)
  • On: 128 and 64bits 7 and 6
  • Off: 32 and 16bits 5 and 4
  • On: 8bit 3
  • Off: 4bit 2
  • On: 2bit 1
  • Off: 1bit 0

Decimal to binary by dividing by 2

To convert, divide by 2 again and again, and write down each remainder. Read the remainders from the last to the first.

Converting 202 to binary
  1. 1202 ÷ 2 = 101Remainder 0. This is the last bit (bit 0).
  2. 2101 ÷ 2 = 50Remainder 1.
  3. 350 ÷ 2 = 25 · 25 ÷ 2 = 12Remainders 0 and 1.
  4. 412 ÷ 2 = 6 · 6 ÷ 2 = 3Remainders 0 and 0.
  5. 53 ÷ 2 = 1 · 1 ÷ 2 = 0Remainders 1 and 1. Stop when you reach 0.
  6. 6Read from the bottom up11001010, which is 202.
Type a number in any base and see it in binary, octal, decimal and hex at once.Open Number Base Converter

Why hex exists

Long strings of ones and zeros are hard for people to read and easy to mistype. Hex is a shortcut. Because 16 is 2⁴, one hex digit stands for exactly four bits. You can convert a binary number to hex by splitting it into groups of four from the right.

DecimalBinaryHexDecimalBinaryHex
000000810008
100011910019
200102101010A
300113111011B
401004121100C
501015131101D
601106141110E
701117151111F

Hex uses the letters A to F for the values 10 to 15.

For 11001010, split into 1100 and 1010. 1100 is C (12) and 1010 is A (10). So the hex is CA. Check it: C × 16 + A = 12 × 16 + 10 = 202.

The same byte, three ways
For people202
  • Decimal, easy to read
  • Hard to see which bits are on
For machines and programmers11001010 = 0xCA
  • Binary shows every bit
  • Hex is a short form: two digits per byte

How big can a number get?

The largest value that fits in each size
2558 bits (1 byte)Hex FF
65,53516 bitsHex FFFF
4.29 billion32 bits4,294,967,295 · IPv4 addresses
1.8 × 10¹⁹64 bits18,446,744,073,709,551,615

Each extra bit doubles the number of values. n bits can hold 2ⁿ different values, from 0 up to 2ⁿ − 1.

Where you meet these every day

  • Web colours. #6366f1 is three bytes: 63, 66 and F1 in hex, which are 99, 102 and 241 in decimal. That is red 99, green 102, blue 241. The contrast checker guide uses these values.
  • IP addresses. Each part of 192.168.1.10 is one byte (8 bits). See what /24 and /26 mean.
  • File permissions. The chmod number 755 is octal (base 8). Each digit is three bits: 7 is 111 (read, write, execute), 5 is 101 (read and execute).
  • Characters. The letter A is 65 in decimal, 41 in hex and 01000001 in binary.
  • Hashes. A SHA-256 hash is 256 bits, shown as 64 hex digits. See what a hash is.

Quick tips

  1. Memorise the place values 1, 2, 4, 8, 16, 32, 64, 128. Everything else is addition.
  2. Learn the sixteen four-bit patterns above. Then hex and binary conversions become instant.
  3. Write 0x before a hex number in code (0xCA) and 0b before binary (0b11001010) so they are not mistaken for decimal.
  4. Check by converting back. If you end up at the number you started with, the conversion is right.

Questions people ask

How do I convert binary to decimal?

Write the place values 128, 64, 32, 16, 8, 4, 2, 1 above the bits and add the values where the bit is 1. For 11001010 that is 128 + 64 + 8 + 2 = 202.

How do I convert decimal to binary?

Divide by 2 repeatedly and write down the remainders. Read the remainders from last to first.

Why does hexadecimal use letters?

Base 16 needs sixteen symbols. After 0 to 9, the letters A to F stand for 10 to 15.

Why is one hex digit four bits?

Because 16 = 2⁴. Four bits can hold exactly 16 different values, which one hex digit represents.

What does 0x mean?

In many programming languages, 0x in front of a number means it is written in hexadecimal, for example 0xFF is 255.