A number base (or radix) is how many distinct digits a number system uses before it carries over to the next column. We count in base 10 because we have ten fingers; computers use base 2 because a wire or transistor is either on or off. Octal (base 8) and hexadecimal (base 16) are shorthand for binary that people can read. Convert between them with the Number Base Converter.
Place value works the same in every base
In decimal, 202 means 2 × 100 + 0 × 10 + 2 × 1: each column is worth ten times the one on its right. In binary, each column is worth two times the one on its right, so the columns are 1, 2, 4, 8, 16, 32, 64, 128. Here is 202 written in binary:
| Position | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit of 202 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 |
Add the columns that hold a 1: 128 + 64 + 8 + 2 = 202. So decimal 202 is binary 11001010.
Converting decimal to binary
Divide by 2 repeatedly and write down the remainders; read them from the last to the first:
| Division | Quotient | Remainder (bit) |
|---|---|---|
| 202 ÷ 2 | 101 | 0 |
| 101 ÷ 2 | 50 | 1 |
| 50 ÷ 2 | 25 | 0 |
| 25 ÷ 2 | 12 | 1 |
| 12 ÷ 2 | 6 | 0 |
| 6 ÷ 2 | 3 | 0 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 |
Reading the remainders from bottom to top gives 11001010. Converting the other way, from binary to decimal, is the column sum above (try the Binary to Decimal Converter and Decimal to Binary Converter converters).
Hexadecimal: binary made readable
Hex uses 16 digits: 0-9 and then A-F for 10-15. One hex digit is exactly four bits, so converting is a matter of grouping:
| Hex | Decimal | Binary |
|---|---|---|
0 |
0 | 0000 |
1 |
1 | 0001 |
2 |
2 | 0010 |
3 |
3 | 0011 |
4 |
4 | 0100 |
5 |
5 | 0101 |
6 |
6 | 0110 |
7 |
7 | 0111 |
8 |
8 | 1000 |
9 |
9 | 1001 |
A |
10 | 1010 |
B |
11 | 1011 |
C |
12 | 1100 |
D |
13 | 1101 |
E |
14 | 1110 |
F |
15 | 1111 |
Split 11001010 into groups of four, 1100 and 1010, and look each up: C and A. So 11001010 is CA in hex, and 202 is 0xCA. A byte (8 bits) is always exactly two hex digits, which is why hex appears in memory dumps, MAC addresses, file signatures and colour codes. See the HEX to Decimal Converter and Decimal to HEX Converter converters.
Octal groups bits in threes: 11 001 010 becomes 3 1 2, so 202 is 312 in octal. It survives mostly in Unix file permissions (chmod and File Permissions Cheat Sheet).
Hex colours
A web colour such as #FF8800 is three bytes: red FF = 255, green 88 = 136, blue 00 = 0. Each channel runs from 00 (0) to FF (255), which is 256 levels; 256 × 256 × 256 gives about 16.7 million colours. Convert colour codes with the HEX to RGB Converter.
Bit operations
Programs often work on individual bits. These are the common operations on 202:
| Operation | Binary | Result |
|---|---|---|
202 AND 15: keep the low 4 bits |
11001010 → 00001010 |
10 (A) |
202 >> 4: shift right 4 places |
11001010 → 00001100 |
12 (C) |
202 XOR 255: flip every bit |
11001010 → 00110101 |
53 (35) |
Masks like AND 15 extract part of a value, and shifts multiply or divide by powers of two.
Negative numbers
A byte can be read as unsigned (0 to 255) or signed (-128 to 127). Signed numbers usually use two's complement, where the top bit counts as -128. The same bits 11001010 are 202 unsigned and -54 signed (202 - 256). That is why overflowing a byte can flip a positive value to a negative one.
Quick reference
- Decimal digits
0-9, binary0-1, octal0-7, hex0-9andA-F. - In code, prefixes mark the base:
0bbinary,0ooctal,0xhex (0b11001010,0o312,0xCAall equal 202). - 8 bits = 1 byte = 2 hex digits = values 0 to 255. Powers of two to remember: 2^8 = 256, 2^10 = 1,024, 2^16 = 65,536.
Do arithmetic directly in binary or hex with the Binary Calculator and Hex Calculator. Related terms: Binary, Hexadecimal and ASCII.