What binary is
Binary is a way of writing numbers with only two digits, 0 and 1. We normally count in decimal, which uses ten digits, 0 to 9. Both systems work the same way: the position of a digit decides what it is worth. In decimal each place is ten times bigger than the one on its right (1, 10, 100, 1,000). In binary each place is only two times bigger (1, 2, 4, 8, 16).
A single 0 or 1 is called a bit. Computers use binary because a bit is easy to build as a switch: off is 0, on is 1. Everything a computer stores, from a photo to a message, is a long row of these switches.
Here is the number 45 in binary. Each box is one bit, and the number above it is what that place is worth. Add up the places that hold a 1 and you get the decimal number.
- top row = the value of each place, doubling as you go left
- bottom row = the bits; a bold digit with a dark outline is 1, a light one is 0
32 + 8 + 4 + 1 = 45, so 45 is written 101101 (the zeros on the left do not change the number).
How to use the converter
- To change a decimal number, type it in the top box. The binary version appears in the bottom box at once.
- To change a binary number, type it in the bottom box. You can leave gaps between the groups of four if that helps you read it.
- Leave Binary format on plain binary for ordinary whole numbers of any size, up to 256 digits.
- Pick 4, 8, 16, 32 or 64 bits when you want a fixed length, or when you need negative numbers. Then type a minus sign in the decimal box and the tool shows the three steps that give the negative binary form.
- Use the copy button in a box to take the number away, and Share to send a link with your number already filled in.
The converter works with whole numbers. If you need a number with a decimal point, such as 10.5, use the Decimal to Binary converter instead.
Decimal to binary: the division method
Divide the number by 2 again and again, and write down the remainder each time. Stop when you reach 0. The remainders, read from the last one to the first, are the binary number.
Take 37:
| Result | Remainder | |
|---|---|---|
| 37 ÷ 2 | 18 | 1 |
| 18 ÷ 2 | 9 | 0 |
| 9 ÷ 2 | 4 | 1 |
| 4 ÷ 2 | 2 | 0 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
The remainders in order are 1, 0, 1, 0, 0, 1. Read them upwards and you get 100101. Check it: 32 + 4 + 1 = 37.
There is a second way that some people find quicker: find the biggest power of 2 that fits, subtract it, and repeat with what is left. For 37, 32 fits (37 − 32 = 5), 16 and 8 do not, 4 fits (5 − 4 = 1), 2 does not, and 1 fits. That gives a 1 in the 32, 4 and 1 places: 100101.
Binary to decimal: add the place values
Write the place value above every digit, starting with 1 on the far right and doubling as you move left. Then add up the place values that sit above a 1.
- d = a binary digit, either 0 or 1
- n = how many places the digit is from the right end (the last digit is place 0)
- 2n = the value of that place
Take 101101. The places are 32, 16, 8, 4, 2, 1 from the left, and the digits are 1, 0, 1, 1, 0, 1. The places with a 1 are 32, 8, 4, 1, so 32 + 8 + 4 + 1 = 45.
Binary numbers from 0 to 31
Learning these by heart is not needed, but seeing the pattern helps. Every number is shown with five bits so the columns line up.
- 0 = 00000
- 1 = 00001
- 2 = 00010
- 3 = 00011
- 4 = 00100
- 5 = 00101
- 6 = 00110
- 7 = 00111
- 8 = 01000
- 9 = 01001
- 10 = 01010
- 11 = 01011
- 12 = 01100
- 13 = 01101
- 14 = 01110
- 15 = 01111
- 16 = 10000
- 17 = 10001
- 18 = 10010
- 19 = 10011
- 20 = 10100
- 21 = 10101
- 22 = 10110
- 23 = 10111
- 24 = 11000
- 25 = 11001
- 26 = 11010
- 27 = 11011
- 28 = 11100
- 29 = 11101
- 30 = 11110
- 31 = 11111
Negative numbers and two’s complement
A computer has no minus sign to store, only bits. So it uses a trick called two’s complement, which needs a fixed number of bits. The first bit on the left is given a negative value, and the rest keep their normal positive values.
In 8 bits the first place is worth −128. Here is −5:
−128 + 64 + 32 + 16 + 8 + 2 + 1 = −5. So −5 is written 1111 1011 in 8 bits.
You do not have to add it up each time. There is a shortcut to turn a positive number into its negative:
- Write the number in binary using all the bits: 5 becomes 0000 0101.
- Flip every bit, turning 0 into 1 and 1 into 0. This is the one’s complement: 1111 1010.
- Add 1. This is the two’s complement: 1111 1011.
Check: the result as an ordinary number is 251, and 251 − 256 = −5. The converter shows these three steps when you type a negative number in a fixed width.
Two things are worth remembering. A number whose first bit is 1 is negative, and all ones (11111111 in 8 bits) is always −1.
How many bits do you need?
With n bits you can make 2n different patterns. Used for whole numbers from zero, that covers 0 up to 2n − 1. Used with two’s complement, half of the patterns become negative.
- n = the number of bits
- 2n = how many different values n bits can hold
| Width | With two’s complement | Without a sign |
|---|---|---|
| 4 bits | −8 to 7 | 0 to 15 |
| 8 bits | −128 to 127 | 0 to 255 |
| 16 bits | −32,768 to 32,767 | 0 to 65,535 |
| 32 bits | −2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
| 64 bits | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 |
To find the bits for a number, write it in binary and count the digits. 37 is 100101, which has 6 digits, so it needs 6 bits. A neat check is that 255 fits in 8 bits but 256 does not, because 28 is 256.
Where you meet binary every day
- Bytes. A byte is 8 bits, so it holds 256 different values (0 to 255). That is why colour values on screen, such as red 255, stop at 255.
- Network addresses. Each part of an IPv4 address is one byte. The address 192.168.1.1 is 11000000.10101000.00000001.00000001 in binary.
- Memory and storage. A 16-bit number can hold 65,536 values, a 32-bit one 4,294,967,296, and a 64-bit one 18,446,744,073,709,551,616. These sizes decide how much memory a program can reach.
- Counting on your fingers. Treat each finger as a bit, up for 1 and down for 0. Ten fingers can count from 0 to 1,023.
Common mistakes
- Reading the places the wrong way round. The smallest place is on the right. When you divide by 2 the remainders come out backwards, so read them from the bottom up.
- Starting the places at 2 instead of 1. The last digit is worth 20, which is 1, not 2.
- Reading 1000 as one thousand. In binary, 1000 is 8.
- Forgetting the width. 11111111 is 255 as an ordinary number but −1 in 8-bit two’s complement. Always know which one you mean.
- Typing too many digits. A 9-digit binary number does not fit in 8 bits.
Handy shortcuts
- Add a 0 on the right to double a number. 101 is 5, and 1010 is 10.
- Drop the last digit to halve it (ignoring the remainder). 1010 is 10, and 101 is 5.
- The last digit tells you odd or even. A 1 on the right means odd, a 0 means even.
- Memorise the powers of 2: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1,024. Every other number is a sum of these.