Decimal to Binary Converter
Type a decimal number to convert it to binary instantly, with the step-by-step working: divide by 2, write down the remainder, repeat. You also get the hex and octal values, and it works for very large numbers.
Decimal number to Binary number converter
Step by step
How to convert decimal to binary
Method 1: divide by 2. This works for any whole number and is the one teachers usually ask for.
- Divide the number by 2. Write down the whole-number quotient and the remainder (0 or 1).
- Divide the quotient by 2 again, and keep going until the quotient is 0.
- Read the remainders from the last one to the first one. That is the binary number.
| 13 in binary | Quotient | Remainder |
|---|---|---|
| 13 ÷ 2 | 6 | 1 |
| 6 ÷ 2 | 3 | 0 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 |
Reading the remainders from the bottom up gives 1101, so 13 in binary is 1101. Check: 8 + 4 + 1 = 13.
Method 2: subtract powers of 2. Find the largest power of 2 that fits (for 156 that is 128), write a 1 there and subtract (156 - 128 = 28). Then go down the places 64, 32, 16, 8, 4, 2, 1, writing 1 when the power fits into what is left and 0 when it does not: 156 = 128 + 16 + 8 + 4 = 10011100.
Negative numbers: two's complement
Computers store negative integers in two's complement. To write -5 in 8 bits: write 5 in binary (00000101), flip every bit (11111010), then add 1: 11111011. The leftmost bit is 1 for every negative number, and an 8-bit value holds -128 to 127.
| Decimal | 8-bit two's complement |
|---|---|
| 127 | 01111111 |
| 5 | 00000101 |
| 1 | 00000001 |
| 0 | 00000000 |
| -1 | 11111111 |
| -5 | 11111011 |
| -128 | 10000000 |
Decimal to binary table
| Decimal | Binary | Decimal | Binary | Decimal | Binary |
|---|---|---|---|---|---|
| 0 | 0 | 11 | 1011 | 32 | 100000 |
| 1 | 1 | 12 | 1100 | 50 | 110010 |
| 2 | 10 | 13 | 1101 | 64 | 1000000 |
| 3 | 11 | 14 | 1110 | 99 | 1100011 |
| 4 | 100 | 15 | 1111 | 100 | 1100100 |
| 5 | 101 | 16 | 10000 | 127 | 1111111 |
| 6 | 110 | 17 | 10001 | 128 | 10000000 |
| 7 | 111 | 18 | 10010 | 200 | 11001000 |
| 8 | 1000 | 19 | 10011 | 255 | 11111111 |
| 9 | 1001 | 20 | 10100 | 256 | 100000000 |
| 10 | 1010 | 21 | 10101 | 1000 | 1111101000 |
Need octal or hex too? Use the Number Base Converter. Studying for AP Computer Science Principles? Binary numbers are part of Big Idea 2, and the AP CSP practice test has questions on them.
Convert decimal to binary in code
Press Run this code to try each one in the online editor and change the numbers.
Python
# Built-in: bin() adds a "0b" prefix, format() does not
print(bin(156)) # 0b10011100
print(format(156, "b")) # 10011100
print(format(5, "08b")) # 00000101 (8 bits, zero-padded)
# By hand: divide by 2 and collect the remainders
def decimal_to_binary(n):
if n == 0:
return "0"
bits = ""
while n > 0:
bits = str(n % 2) + bits
n //= 2
return bits
for n in [13, 156, 255, 1024]:
print(n, "=", decimal_to_binary(n))
C
#include <stdio.h>
void print_binary(unsigned int n) {
char bits[33];
int i = 32;
bits[i] = '\0';
if (n == 0) bits[--i] = '0';
while (n > 0) {
bits[--i] = (char)('0' + n % 2); /* remainder is the next bit */
n /= 2;
}
printf("%s\n", &bits[i]);
}
int main(void) {
print_binary(156); /* 10011100 */
print_binary(13); /* 1101 */
return 0;
}
JavaScript
// Built-in: toString with radix 2
console.log((156).toString(2)); // 10011100
console.log((5).toString(2).padStart(8, "0")); // 00000101
// Negative numbers in 8-bit two's complement
const twosComplement8 = n => ((n + 256) % 256).toString(2).padStart(8, "0");
console.log(twosComplement8(-5)); // 11111011
Frequently asked questions
How do you convert decimal to binary?
Divide the number by 2 and write down the remainder. Keep dividing the quotient by 2 until it reaches 0, then read the remainders from the last to the first. For example, 13 gives the remainders 1, 0, 1, 1, so 13 is 1101 in binary.
What is 255 in binary?
11111111. 255 is 2 to the power of 8, minus 1, so all eight bits are 1.
What is 10 in binary?
1010, because 10 = 8 + 2.
How do you write a negative number in binary?
Computers use two's complement: write the positive number in binary with a fixed number of bits, flip every bit, then add 1. In 8 bits, -5 is 11111011.
How do I convert decimal to binary in Python?
Use bin(156), which returns '0b10011100', or format(156, 'b') for the digits only. format(5, '08b') pads it to 8 bits: 00000101.
Is this converter free?
Yes. It runs in your browser, is free and needs no account.
Related tools
- Binary to Decimal Converter with Steps - Free OnlineConvert binary to decimal instantly with step-by-step working: each bit, its place value and the sum. Also shows hex and octal, works for long numbers. Free.
- Truth Table Generator - Step by Step, Free OnlineMake a truth table for any Boolean expression, with a column for every step. AND, OR, NOT, XOR, NAND, NOR, implies and iff; minterms, SOP and POS. Free.
- Boolean Algebra Calculator - Simplify Expressions, K-MapSimplify Boolean expressions online: minimal sum of products and product of sums, Karnaugh map, minterms, canonical forms and an equivalence check. Free.