How binary numbers work
Binary is a base-2 number system that uses only the digits 0 and 1. Each place is worth twice the place to its right, so instead of ones, tens and hundreds you get ones, twos, fours, eights and so on. Computers use it because a circuit that is either on or off can store exactly one binary digit, called a bit.
| Place from the right | Power of 2 | Value |
|---|---|---|
| 1st | 2⁰ | 1 |
| 2nd | 2¹ | 2 |
| 3rd | 2² | 4 |
| 4th | 2³ | 8 |
| 5th | 2⁴ | 16 |
| 6th | 2⁵ | 32 |
| 7th | 2⁶ | 64 |
| 8th | 2⁷ | 128 |
Example: reading 101010
- Line the digits up with their place values: 32, 16, 8, 4, 2, 1
- The 1s sit under 32, 8 and 2
- 32 + 8 + 2 = 42
Binary 101010 is 42 in decimal.
How to convert decimal to binary
The standard method is repeated division by 2. Divide the number by 2, write down the remainder (always 0 or 1), and keep dividing the answer until you reach 0. The remainders, read from the last one back to the first, are the binary digits.
Example: 42 to binary
- 42 ÷ 2 = 21 remainder 0
- 21 ÷ 2 = 10 remainder 1
- 10 ÷ 2 = 5 remainder 0
- 5 ÷ 2 = 2 remainder 1
- 2 ÷ 2 = 1 remainder 0
- 1 ÷ 2 = 0 remainder 1
- Read the remainders from the bottom up: 101010
42 in binary is 101010.
For small numbers, subtracting powers of 2 is often quicker in your head. Take the largest power that fits, subtract it, and repeat: 42 − 32 = 10, 10 − 8 = 2, 2 − 2 = 0. Put a 1 in the 32, 8 and 2 places and a 0 everywhere else, and you get the same 101010.
Binary to hex and octal
Long strings of bits are hard to read, so programmers shorten them. Hexadecimal (base 16) uses 0 to 9 and then A to F for 10 to 15. Because 16 is 2⁴, every hex digit stands for exactly four bits, and one byte is always two hex digits.
| Binary | Hex | Decimal |
|---|---|---|
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 3 | 3 |
| 0100 | 4 | 4 |
| 0101 | 5 | 5 |
| 0110 | 6 | 6 |
| 0111 | 7 | 7 |
| 1000 | 8 | 8 |
| 1001 | 9 | 9 |
| 1010 | A | 10 |
| 1011 | B | 11 |
| 1100 | C | 12 |
| 1101 | D | 13 |
| 1110 | E | 14 |
| 1111 | F | 15 |
To convert, split the binary into groups of four from the right and swap each group for its hex digit: 1010 1111 becomes AF, which is 175. Web colors use this shorthand. In #FF8800, FF is 255 parts red, 88 is 136 parts green and 00 is no blue. Octal (base 8) works the same way with groups of three bits, which is why a Unix file permission such as 755 maps neatly onto 111 101 101.
How text becomes binary
Text has no binary form until it is encoded. Every character first gets a number, its Unicode code point, and an encoding turns that number into bytes. UTF-8, the encoding most web pages use, stores the 128 basic ASCII characters (English letters, digits and common punctuation) as one byte each, so H is 72, or 01001000, and i is 105, or 01101001.
| Code points | Bytes | Examples |
|---|---|---|
| U+0000 to U+007F | 1 | A, z, 7 and ? |
| U+0080 to U+07FF | 2 | é, ñ, ü and Greek, Cyrillic, Hebrew or Arabic letters |
| U+0800 to U+FFFF | 3 | € and most Chinese, Japanese and Hindi characters |
| U+10000 to U+10FFFF | 4 | Most emoji and rarer scripts |
Example: the euro sign
- € is code point U+20AC
- That falls in the three-byte range
- UTF-8 bytes: 11100010 10000010 10101100, or E2 82 AC in hex
One character, three bytes, 24 bits.
Capital and small letters differ by a single bit. A is 01000001 (65) and a is 01100001 (97); the only change is the bit worth 32, which is why switching case is so cheap for a computer.
Bits, bytes and the largest numbers they hold
With n bits you can write 2ⁿ different patterns, so the largest whole number is 2ⁿ − 1. Every extra bit doubles the range.
| Bits | Largest unsigned value | Signed range (two's complement) |
|---|---|---|
| 8 (one byte) | 255 | −128 to 127 |
| 16 | 65,535 | −32,768 to 32,767 |
| 32 | 4,294,967,295 | −2,147,483,648 to 2,147,483,647 |
| 64 | 18,446,744,073,709,551,615 | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Computers usually store negative whole numbers in two's complement: flip every bit of the positive value and add 1. In 8 bits, 5 is 00000101, so −5 is 11111010 + 1 = 11111011. The converter writes negatives with a minus sign and shows the two's complement pattern underneath.
Ordinary JavaScript numbers lose precision above 2⁵³, which is 9,007,199,254,740,992, so this converter does its arithmetic with exact whole numbers of any length instead. A 64-bit value such as 18,446,744,073,709,551,615 comes out as sixteen Fs in hex without losing a digit.
Mistakes to avoid when converting binary
- Reading the remainders top down. In the division method the first remainder is the rightmost digit, so read from the bottom up.
- Dropping leading zeros in bytes. 1000001 and 01000001 are the same number, but byte-based tools expect all 8 bits, so pad on the left.
- Reading 10 as ten. In binary, 10 means two. Writing 0b10, or adding the words base 2, avoids the mix-up.
- Confusing B with 8. In hex, B is 11, and D (13) is easy to misread as 0. Writing hex in capitals helps.
- Mixing up bits and bytes. Internet speeds are quoted in bits per second and file sizes in bytes, with 8 bits to a byte. The data storage converter handles those units.
Binary is one of several ways to write the same number. For a much older system that works without place value at all, try the Roman numeral converter.