Number systems: decimal, binary and hex
Computers store every number as bits, and programmers often write those bits in hex. Follow one number, 173, as it is spelled three ways: 173 in decimal, 10101101 in binary and AD in hex. Watch place value add it up, repeated division turn it into bits, and groups of four bits become hex digits.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What is the binary number 1011 in decimal?
Add the places that hold a 1: 8 + 2 + 1 = 11. The 4s place holds 0, so it adds nothing.
02You divide 6 by 2 again and again: 6 → 3 r0, 3 → 1 r1, 1 → 0 r1. What is 6 in binary?
The first remainder is the ones bit, so read the remainders from the last to the first: 1, 1, 0. Check: 4 + 2 = 6.
03Why does one hex digit stand for exactly four bits?
2 × 2 × 2 × 2 = 16, so 0000 to 1111 match the hex digits 0 to F one for one. Two hex digits make one byte.
04What is 0xFF in decimal?
F is 15, so FF is 15 × 16 + 15 = 255. In binary it is 11111111, the largest number one byte can hold.
052026 in binary is 11111101010. Grouping the bits in fours from the right (111 1110 1010), what is it in hex?
111 is 7, 1110 is 14 (E) and 1010 is 10 (A), so 0x7EA. Check: 7 × 256 + 14 × 16 + 10 = 2026, and hex(2026) in Python gives '0x7ea'.
Glossary
- Place value
- What a digit is worth because of where it sits: in 173 the 1 is worth 100, because it is in the hundreds place.
- Base (radix)
- How many digits a number system has, and how much each place grows: 10 for decimal, 2 for binary, 16 for hex.
- Bit
- A binary digit, 0 or 1. In hardware it is one of two states, such as a low or high voltage.
- Byte
- A group of 8 bits. It can hold 256 patterns, the numbers 0 to 255.
- Nibble
- A group of 4 bits, written as one hex digit.
- Hexadecimal (hex)
- Base 16, with digits 0 to 9 and A to F. Written with the prefix 0x in Python, as in 0xFF.
- Octal
- Base 8, with digits 0 to 7; each octal digit stands for 3 bits. Written with the prefix 0o in Python.
- Repeated division
- A way to convert a number to another base: divide by the base, keep the remainder, repeat until the quotient is 0, then read the remainders backwards.
Notes and sources
- Repeated division works for any base: divide by the base, keep the remainder, and repeat with the quotient until it is 0. The remainders, read from the last one to the first, are the digits.
- A group of four bits is called a nibble. A byte is eight bits, so two hex digits always describe exactly one byte: 00 to FF, which is 0 to 255.
- Python prints hex digits in lower case ('0xad'); 0xAD and 0xad are the same number. 0b, 0o and 0x are prefixes that say which base a literal is written in.
- Bigger numbers work the same way: 2026 is 0b11111101010 in binary and 0x7EA in hex (7 × 256 + 14 × 16 + 10).
- Leading zeros change nothing: 0b00001010 and 0b1010 are both 10, just as 007 and 7 are the same number.
- Harris & Harris, Digital Design and Computer Architecture, 2nd ed., section 1.4 Number Systems
- MIT 6.004 Computation Structures notes: Basics of Information
- MIT 6.004 Computation Structures notes: The Digital Abstraction
- Harvard CS50 lecture notes, week 0 (binary)
- Python library: Built-in functions (bin, hex, oct, int)
- Python reference: Integer literals (0b, 0o, 0x)
Quiz Unlocks after the last step.