How to Convert Decimal to Binary
Divide by 2 and collect the remainders, or take away powers of 2. Both work, but the hard part is everything around them: how many bits to use, what to do with negative numbers, and why some fractions never end. This guide covers all of it. To check an answer as you go, use the decimal to binary converter.
What a binary number means
In decimal, each place is worth ten times the one to its right: ones, tens, hundreds. In binary, each place is worth twice the one to its right: 1, 2, 4, 8, 16 and so on. Every place holds a 0 or a 1, so a binary number is a list of which powers of 2 add up to the value.
Here is 217 written that way.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |
128 + 64 + 16 + 8 + 1 = 217, so 217 in binary is 11011001. Converting from decimal means finding those powers of 2. There is only ever one way to do it, so every number has exactly one binary form.
The two hand methods
Divide by 2
Divide by 2 and write down the remainder. Repeat with the quotient until it reaches 0, then read the remainders from the last one to the first. For 217 the remainders are 1, 0, 0, 1, 1, 0, 1, 1. Read backwards, that's 11011001.
It works because each remainder tells you whether the current number is odd, and only the 1s place can make a number odd. Halving the number then shifts every bit one place to the right, so the next remainder reveals the next bit.
Subtract powers of 2
Start from the largest power of 2 that fits. 128 fits into 217, leaving 89. 64 fits, leaving 25. 32 doesn't fit. 16 fits, leaving 9. 8 fits, leaving 1. 4 and 2 don't fit, and 1 does. Write 1 for each power you used and 0 for each one you skipped.
The converter shows both methods for any number you type, with every step laid out in a table.
How many bits you need
With n bits you can write every whole number from 0 up to 2n − 1. To find the bits a number needs, find the first power of 2 that is bigger than it. 217 is below 256, which is 28, so it fits in 8 bits. 300 is above 256, so it needs 9.
| Bits | Unsigned range | Signed range |
|---|---|---|
| 4 | 0 to 15 | −8 to 7 |
| 8 | 0 to 255 | −128 to 127 |
| 16 | 0 to 65,535 | −32,768 to 32,767 |
| 32 | 0 to 4,294,967,295 | −2,147,483,648 to 2,147,483,647 |
Computers store numbers at a fixed width, so you often pad with zeros on the left. 5 is 101, but as a byte it is 00000101. The value doesn't change.
If a number is too big for its width, the extra bits on the left are lost. 300 is 100101100. Kept in 8 bits, only 00101100 survives, which is 44. This is called overflow, and it is a common source of bugs.
Big numbers: go through hex
Dividing 50,000 by 2 takes sixteen steps. Dividing by 16 takes four, and each hex digit then turns into four bits at once.
Divide by 16 and keep the remainders
50,000 ÷ 16 is 3,125 remainder 0. 3,125 ÷ 16 is 195 remainder 5. 195 ÷ 16 is 12 remainder 3. 12 ÷ 16 is 0 remainder 12.
Read them backwards as hex
12 is C, so the hex number is C350.
Swap each hex digit for four bits
C is 1100, 3 is 0011, 5 is 0101 and 0 is 0000.
Join the groups
50,000 in binary is
1100001101010000.
The last two steps are the reverse of grouping bits, which the binary to hex guide explains in detail. You can also try the first step with the decimal to hex converter.
Negative numbers in two's complement
On paper you can write −89 as -1011001. Computers can't store a minus sign, so most of them use two's complement. Before you start, pick the width. The same number looks different in 8 bits and in 16 bits.
Flip and add 1
Write 89 in 8 bits: 01011001. Flip every bit to get 10100110. Add 1 to get 10100111. That is −89 in 8-bit two's complement.
The subtraction shortcut
For n bits, add 2n to the negative number and convert the result as usual. In 8 bits that's 256 − 89 = 167, and 167 is 10100111, the same answer with no flipping.
Check it
In two's complement the leftmost bit is worth minus its normal value. So 10100111 is −128 + 32 + 4 + 2 + 1, which is −89. If the leftmost bit is 0, the number is positive. The binary converter shows two's complement at 8, 16, 32 and 64 bits.
Fractions
Places to the right of the binary point are worth ½, ¼, ⅛ and so on. To convert a fraction, multiply it by 2. The digit before the point is your next bit, and you carry on with what's after it. Stop when the fraction reaches 0.
For 5.375, the whole part 5 is 101. Then 0.375 × 2 = 0.75, giving 0. 0.75 × 2 = 1.5, giving 1. 0.5 × 2 = 1.0, giving 1. Read these bits from the top down and join the parts: 101.011.
Why 0.1 never ends
A fraction ends in binary only if its denominator, in lowest terms, is a power of 2. 0.375 is ⅜, so it ends. 0.1 is 1/10, and 10 has a factor of 5, so it repeats forever: 0.0001100110011…, with 0011 repeating.
A computer has to cut that pattern off somewhere, so it stores a value very slightly off from 0.1. That's why 0.1 + 0.2 prints 0.30000000000000004 in JavaScript and Python. It is not a bug in the language. It is the same thing that happens when you write ⅓ as 0.333 in decimal.
Convert decimal to binary in code
Each example prints 217 in binary. The Python and JavaScript ones also pad to 8 bits and show −89 as a byte.
Python
print(format(217, 'b')) # 11011001
print(format(5, '08b')) # 00000101
print(format(-89 & 0xFF, '08b')) # 10100111JavaScript
console.log((217).toString(2)); // 11011001
console.log((5).toString(2).padStart(8, '0')); // 00000101
console.log((-89 & 0xff).toString(2).padStart(8, '0')); // 10100111C
#include <stdio.h>
int main(void) {
unsigned n = 217;
for (int i = 7; i >= 0; i--) putchar((n >> i) & 1 ? '1' : '0');
putchar('\n'); /* 11011001 */
return 0;
}Bash
echo "obase=2; 217" | bc # 11011001Watch out for negative numbers. (-89).toString(2) in JavaScript gives -1011001, with a minus sign. To get the stored bits, mask the number to the width you want first, as the examples above do with & 0xFF.
Excel and Google Sheets
=DEC2BIN(217) gives 11011001, and =DEC2BIN(5, 8) gives 00000101. The function only takes numbers from −512 to 511. Negative numbers come back as 10-bit two's complement, so =DEC2BIN(-89) gives 1110100111.
Common mistakes
Choosing the width after you flip
Flipping 1011001 (89 without padding) gives 0100110, and adding 1 gives 0100111, which is 39. Pad to the full width first, then flip.
Reading fraction bits backwards
Remainders from dividing are read from the bottom up, but bits from multiplying a fraction are read from the top down. Mixing them up turns 0.011 into 0.110.
Trusting a fraction to be exact
Never compare money or measurements stored as binary fractions with ==. Round them, or store whole units such as cents instead.
Practice questions
Work each one out before you open the answer.
Convert 75 to binary
1001011. That's 64 + 8 + 2 + 1.
How many bits does 300 need, and what is it in binary?
9 bits, because 256 ≤ 300 < 512. It is 100101100, or 256 + 32 + 8 + 4.
Write −20 in 8-bit two's complement
11101100. Using the shortcut, 256 − 20 = 236, which is 128 + 64 + 32 + 8 + 4.
Convert 0.75 to binary
0.11. 0.75 × 2 = 1.5 gives 1, then 0.5 × 2 = 1.0 gives 1.
Convert 1,000 to binary by going through hex
1111101000. 1,000 is 3E8 in hex, and 3, E and 8 are 0011, 1110 and 1000.