Binary and Hexadecimal
Computers store every number as a pattern of bits, and programmers often read those patterns in base 2 (binary) or base 16 (hexadecimal). This lesson shows how place value works in any base, how to convert by hand in both directions, and how Python's bin(), hex() and int(s, base) do it for you.
Place value in other bases
In our usual base 10, each place is worth ten times the place to its right (see Place Value and Rounding): . Nothing is special about ten. In base the places are powers of , and the digits run from 0 to .
| Base | Name | Digits | Place values (right to left) |
|---|---|---|---|
| 2 | binary | 0 1 | 1, 2, 4, 8, 16, 32, 64, 128 … |
| 8 | octal | 0–7 | 1, 8, 64, 512 … |
| 10 | decimal | 0–9 | 1, 10, 100, 1000 … |
| 16 | hexadecimal | 0–9, A–F | 1, 16, 256, 4096 … |
The binary place values are the Powers of Two. In hex the letters A–F stand for 10–15.
Binary to decimal
Write the place values over the bits and add those that sit under a 1:
place: 128 64 32 16 8 4 2 1
bits: 1 0 1 1 0 1 0 1
sum: 128 + 32 + 16 + 4 + 1 = 181So . A subscript names the base when it isn't obvious.
Decimal to binary: repeated division
Divide by 2 again and again and write down the remainders. Read the remainders from bottom to top:
181 ÷ 2 = 90 r 1 ↑
90 ÷ 2 = 45 r 0 │
45 ÷ 2 = 22 r 1 │
22 ÷ 2 = 11 r 0 │ read upward:
11 ÷ 2 = 5 r 1 │ 10110101
5 ÷ 2 = 2 r 1 │
2 ÷ 2 = 1 r 0 │
1 ÷ 2 = 0 r 1 │The same algorithm works for any base: divide by 16 to get hex digits. Here it is in code, next to Python's built-ins:
DIGITS = "0123456789ABCDEF"
def to_base(n, b):
if n == 0:
return "0"
out = ""
while n > 0:
n, r = divmod(n, b)
out = DIGITS[r] + out # the newest remainder goes on the left
return out
for n in [5, 181, 255, 2026]:
print(n, to_base(n, 2), to_base(n, 16), "|", bin(n), hex(n), oct(n))5 101 5 | 0b101 0x5 0o5
181 10110101 B5 | 0b10110101 0xb5 0o265
255 11111111 FF | 0b11111111 0xff 0o377
2026 11111101010 7EA | 0b11111101010 0x7ea 0o3752
Python marks the base with a prefix: 0b for binary, 0x for hex, 0o for octal. You can type these prefixes directly in code: 0xFF == 255.
Hex is shorthand for binary
, so one hex digit is exactly four bits (a "nibble"). To convert, group the bits in fours from the right and translate each group:
1011 0101 → B 5 → 0xB5
0111 1110 1010 → 7 E A → 0x7EAThat is why programmers prefer hex. A byte (8 bits) is always exactly two hex digits, from 00 to FF, and a 32-bit value is eight.
| Hex | Binary | Dec | Hex | Binary | Dec |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 8 | 1000 | 8 |
| 1 | 0001 | 1 | 9 | 1001 | 9 |
| 2 | 0010 | 2 | A | 1010 | 10 |
| 3 | 0011 | 3 | B | 1011 | 11 |
| 4 | 0100 | 4 | C | 1100 | 12 |
| 5 | 0101 | 5 | D | 1101 | 13 |
| 6 | 0110 | 6 | E | 1110 | 14 |
| 7 | 0111 | 7 | F | 1111 | 15 |
Going back from text to a number is int(text, base). format() with b/x and a width pads with leading zeros:
print(int("10110101", 2), int("b5", 16), int("FF", 16), int("777", 8))
print(int("0x7EA", 16), int("0b101", 0)) # base 0 reads the prefix
print(format(181, "08b"), format(181, "02X"), format(10, "08b"))
print(f"{48879:#x} {48879:,} {0b1111_0000}")181 181 255 511
2026 5
10110101 B5 00001010
0xbeef 48,879 240
In software
- Colours.
#FF8800is three bytes: red , green , blue . - Memory addresses and hashes are printed in hex:
0x7ffd5a3c, a Git commitdcc5623…, a SHA-256 digest. - Bit flags and permissions. Unix
chmod 755is octal: 7 =111(read, write, execute), 5 =101. - Network and file formats. MAC addresses, UUIDs and the bytes in a hex dump are all base 16.
color = "#FF8800"
r, g, b = (int(color[i:i + 2], 16) for i in (1, 3, 5))
print("rgb:", r, g, b)
print("back to hex:", f"#{r:02X}{g:02X}{b:02X}")
print("755 ->", [format(int(d), "03b") for d in "755"])rgb: 255 136 0
back to hex: #FF8800
755 -> ['111', '101', '101']
Exercises
Exercise 1: Binary to decimal
Convert to decimal by hand.
Answer
109
Solution
place: 64 32 16 8 4 2 1
bits: 1 1 0 1 1 0 1
sum: 64 + 32 + 8 + 4 + 1 = 109Exercise 2: Decimal to binary and hex
Convert 300 to binary by repeated division, then to hex by grouping bits.
Answer
= 0x12C
Solution
300 ÷ 2 = 150 r 0
150 ÷ 2 = 75 r 0
75 ÷ 2 = 37 r 1
37 ÷ 2 = 18 r 1
18 ÷ 2 = 9 r 0
9 ÷ 2 = 4 r 1
4 ÷ 2 = 2 r 0
2 ÷ 2 = 1 r 0
1 ÷ 2 = 0 r 1
read upward: 100101100
group from the right: 1 0010 1100 → 1 2 C → 0x12C
check: 1·256 + 2·16 + 12 = 300 ✓print(bin(300), hex(300))0b100101100 0x12c
Exercise 3: Hex to decimal
What is 0x3E8 in decimal?
Answer
1000
Solution
Exercise 4: Parse a colour
Write a function parse_rgb("#1E90FF") that returns the three channel values as a tuple of integers, without using any colour library.
Answer
(30, 144, 255)
Solution
Skip the #, take two hex digits per channel and read each pair with int(…, 16): , , .
def parse_rgb(s):
s = s.lstrip("#")
return tuple(int(s[i:i + 2], 16) for i in (0, 2, 4))
print(parse_rgb("#1E90FF"))(30, 144, 255)
Test yourself
What is in decimal?
- 4
- 11
- 15
- 16
How many bits does one hexadecimal digit represent?
- 2
- 4
- 8
- 16
What does int("ff", 16) return in Python?
- 255
- "0xff"
- 15
- An error