Computer Basics
Number Systems
Binary, octal, decimal and hexadecimal: place values, converting between any two bases, and why programmers use hex — with an interactive converter.
We count in decimal (base 10) because we have ten fingers. Computers use binary (base 2) because a transistor is either off (0) or on (1). Programmers also use octal (base 8) and hexadecimal (base 16) as compact ways to write binary.
| System | Base | Digits | Example | C / Python literal |
|---|---|---|---|---|
| Binary | 2 | 0 1 | 1011 | 0b1011 |
| Octal | 8 | 0–7 | 755 | 0755 (C), 0o755 (Python) |
| Decimal | 10 | 0–9 | 255 | 255 |
| Hexadecimal | 16 | 0–9, A–F (A = 10 … F = 15) | FF | 0xFF |
Place values
In base b, each position is worth a power of b, growing from right to left.
1011₂ = 1·2³ + 0·2² + 1·2¹ + 1·2⁰ = 8 + 0 + 2 + 1 = 11 2F₁₆ = 2·16¹ + 15·16⁰ = 32 + 15 = 47 157₈ = 1·8² + 5·8¹ + 7·8⁰ = 64 + 40 + 7 = 111
Decimal to any base: repeated division
Divide by the base, record the remainder, repeat with the quotient until it reaches 0, then read the remainders bottom to top.
Convert 45 to binary:
| Divide | Quotient | Remainder |
|---|---|---|
| 45 ÷ 2 | 22 | 1 |
| 22 ÷ 2 | 11 | 0 |
| 11 ÷ 2 | 5 | 1 |
| 5 ÷ 2 | 2 | 1 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
Reading upwards: 45 = 101101₂.
Try it
Binary ↔ octal and hexadecimal: grouping
Because 8 = 2³ and 16 = 2⁴, each octal digit is exactly 3 bits and each hex digit exactly 4 bits. Group bits from the right:
1101 0110 1010₂ → D 6 A → D6A₁₆ 110 101 101 010₂ → 6 5 5 2 → 6552₈
| Hex | Binary | Hex | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | A | 1010 |
| 3 | 0011 | B | 1011 |
| 4 | 0100 | C | 1100 |
| 5 | 0101 | D | 1101 |
| 6 | 0110 | E | 1110 |
| 7 | 0111 | F | 1111 |
That is why programmers love hex: one hex digit = 4 bits, two hex digits = one byte. Colours (#FF8800), memory addresses (0x7ffe…) and file signatures are all written in hex.
Fractions in binary
Positions after the point are negative powers: 0.101₂ = 1/2 + 0/4 + 1/8 = 0.625. To convert a decimal fraction, multiply by 2 repeatedly and take the integer parts: 0.625 → 1.25 (1) → 0.5 (0) → 1.0 (1) → 0.101₂. Some fractions never terminate in binary — 0.1 is 0.000110011…₂ — which is why 0.1 + 0.2 != 0.3 in every language.
Converting in code
#include <stdio.h>
void toBase(unsigned n, unsigned base) {
const char *digits = "0123456789ABCDEF";
char buf[40];
int i = 0;
if (n == 0) buf[i++] = '0';
while (n > 0) { buf[i++] = digits[n % base]; n /= base; } // remainders, last first
while (i > 0) putchar(buf[--i]); // print them in reverse
}
int main(void) {
unsigned n = 2026;
printf("%u in decimal\n", n);
printf("binary: "); toBase(n, 2); printf("\n");
printf("octal: %o\nhex: %X\n", n, n);
printf("base 5: "); toBase(n, 5); printf("\n");
printf("0x7E2 = %d, 0b11111101010 = %d\n", 0x7E2, 0b11111101010);
return 0;
}2026 in decimal binary: 11111101010 octal: 3752 hex: 7EA base 5: 31101 0x7E2 = 2018, 0b11111101010 = 2026
n = 2026
print(bin(n), oct(n), hex(n))
print(int("11111101010", 2), int("3752", 8), int("7ea", 16), int("31101", 5))
print(f"{n:b} {n:o} {n:X} {n:#010b}")0b11111101010 0o3752 0x7ea 2026 2026 2026 2026 11111101010 3752 7EA 0b11111101010
Test yourself
4 questions · pick an answer to check it instantly
- 1
What is 110101₂ in decimal?
32 + 16 + 4 + 1 = 53. - 2
What is 200 in hexadecimal?
200 = 12 × 16 + 8, and 12 is C. - 3
How many bits does one hexadecimal digit represent?
16 = 2⁴, so each hex digit is exactly 4 bits. - 4
What is 10110111₂ in hexadecimal?
Group as 1011 0111 → B 7.