To count in binary, use only the digits 0 and 1 and carry to the next column every time a column would reach 2: 0, 1, 10, 11, 100, 101, and so on. Each column is worth double the one on its right (1, 2, 4, 8, 16...), so you can read any binary number by adding up the columns that hold a 1.
Counting from 0 to 15 in binary
Decimal counting runs out of digits after 9, so you write 10 and carry a 1 into the next column. Binary does the same thing, but it runs out of digits after 1. The pattern below is padded with leading zeros to 4 digits so the columns line up.
| Decimal | Binary | Decimal | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | 10 | 1010 |
| 3 | 0011 | 11 | 1011 |
| 4 | 0100 | 12 | 1100 |
| 5 | 0101 | 13 | 1101 |
| 6 | 0110 | 14 | 1110 |
| 7 | 0111 | 15 | 1111 |
Look at the rightmost column: it alternates 0, 1, 0, 1 on every step. The next column alternates in pairs (0, 0, 1, 1), the one after that in fours, and the leftmost in eights. Each column flips half as often as the one to its right. If you ever lose your place, that pattern lets you rebuild the list.
How binary place values work
In decimal, the number 305 means 3 hundreds, 0 tens and 5 ones. The columns are powers of 10. Binary uses the same idea with powers of 2. For an 8-digit binary number (one byte), the column values from left to right are:
| Column | 8th | 7th | 6th | 5th | 4th | 3rd | 2nd | 1st |
|---|---|---|---|---|---|---|---|---|
| Power of 2 | 2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Each digit is called a bit. A byte is 8 bits; NIST's page on binary multiples states this as 1 B = 2^3 bit = 8 bit. Memorise the eight values above and every other step in this article becomes arithmetic.
How to convert binary to decimal
Write the column values under the digits, then add the values that sit under a 1. Take 10110:
digit: 1 0 1 1 0
value: 16 8 4 2 1
keep: 16 + 0 + 4 + 2 + 0 = 22
So binary 10110 is decimal 22. A second example with a full byte: 11001000 has 1s under 128, 64 and 8, so it is 128 + 64 + 8 = 200.
How to convert decimal to binary
Start with the largest column value that fits, write a 1 there, subtract it, and repeat with what is left. Columns you skip get a 0. For 45:
- The largest value that fits is 32. Write 1 and subtract: 45 - 32 = 13.
- 16 does not fit in 13, so write 0.
- 8 fits. Write 1: 13 - 8 = 5.
- 4 fits. Write 1: 5 - 4 = 1.
- 2 does not fit in 1, so write 0.
- 1 fits. Write 1: 1 - 1 = 0.
Reading the digits in order gives 101101. Check it the other way: 32 + 8 + 4 + 1 = 45.
The other common method is repeated division by 2. Divide the number by 2, write down the remainder (0 or 1), divide the quotient by 2 again, and continue until the quotient is 0. The remainders read from last to first are the binary number. For 13: 13 / 2 = 6 remainder 1, 6 / 2 = 3 remainder 0, 3 / 2 = 1 remainder 1, 1 / 2 = 0 remainder 1. Read bottom to top: 1101.
How high can you count with a given number of bits?
With n bits you get 2n different patterns, because each bit doubles the count. Since counting starts at 0, the largest number is 2n - 1.
| Bits | Different values | Largest number |
|---|---|---|
| 4 | 16 | 15 |
| 8 | 256 | 255 |
| 10 | 1,024 | 1,023 |
| 16 | 65,536 | 65,535 |
This is why 255 turns up so often in computing: it is the largest value one byte can hold as an unsigned number. It is also why you can count from 0 to 31 on the fingers of one hand (5 bits, each finger up for 1), and from 0 to 1,023 using both hands.
How to add in binary
Binary addition follows four rules for a single column: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10 (write 0, carry 1). Work from right to left, as in decimal. Adding 11 and 6:
1011 (11)
+ 0110 (6)
------
10001 (17)
Column by column from the right: 1 + 0 = 1. Then 1 + 1 = 0, carry 1. Then 0 + 1 + the carry = 0, carry 1. Then 1 + 0 + the carry = 0, carry 1. The last carry becomes a new leftmost digit. The result 10001 is 16 + 1 = 17, which matches 11 + 6.
One consequence worth knowing: adding 1 to a number that is all 1s rolls it over to a 1 followed by zeros, for example 0111 + 0001 = 1000 (7 + 1 = 8). In fixed-width hardware, if the result needs more bits than are available, the extra bit is lost. That is overflow.
Check your answers in Python
Python's built-in bin() converts an integer to a binary string with a 0b prefix, and int() with base 2 converts back. The Python documentation lists bin(3) as '0b11'. To get the digits without the prefix, use format(n, 'b'), and to pad to a fixed width, add a width and a leading zero flag.
print(bin(45)) # 0b101101
print(format(45, 'b')) # 101101
print(format(5, '08b')) # 00000101
print(int('11001000', 2)) # 200
Those four lines should print the values in the comments, which match the worked examples above (45 is 101101, and 11001000 is 200). The '08b' format pads to 8 digits, which is how a single byte is usually written.
Common mistakes when counting in binary
- Reading binary 10 as ten. It is two. Without a prefix or subscript,
10is ambiguous, which is why code often writes0b10. - Skipping a pattern. After
0111comes1000, not1001. Every column to the right of the new 1 resets to 0. - Reading columns from the wrong end. The leftmost digit is the largest value. The rightmost is always 1.
- Mixing up bits and bytes. A bit is one binary digit and a byte is 8 of them. If that distinction matters for speeds and file sizes, see Mbps vs MBps: bits vs bytes.
Where binary counting shows up in hardware
Storage and memory sizes are built on powers of 2. NIST defines the binary prefixes this way: kibi (Ki) is 210, or 1,024, and mebi (Mi) is 220, so 1 MiB = 1 048 576 B. Ten address bits can pick out 1,024 locations, which is where the 1,024 comes from. The same arithmetic explains why a 16-bit value tops out at 65,535 and why an 8-bit colour channel runs from 0 to 255.
Frequently asked questions
- What is 10 in binary?
- Decimal 10 is 1010 in binary, because 8 + 2 = 10. Binary 10 on its own is the number two, not ten, since the left digit has a place value of 2.
- How high can you count with 8 bits?
- With 8 bits you can count from 0 to 255, which is 256 different values. In general, n bits give 2 to the power n values, so the largest number is 2^n minus 1.
- Why do computers use binary?
- Digital circuits are built from components that are either on or off, which map directly to the digits 1 and 0. Counting with two symbols matches that hardware, so every number, character and instruction is stored as a pattern of bits.
- How many bits are in a byte?
- A byte is 8 bits. NIST's page on binary multiples gives this as 1 B = 2^3 bit = 8 bit.