Number systems

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

Binary number
0

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.

  1. Divide the number by 2. Write down the whole-number quotient and the remainder (0 or 1).
  2. Divide the quotient by 2 again, and keep going until the quotient is 0.
  3. Read the remainders from the last one to the first one. That is the binary number.
13 in binaryQuotientRemainder
13 ÷ 261
6 ÷ 230
3 ÷ 211
1 ÷ 201

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.

Decimal8-bit two's complement
12701111111
500000101
100000001
000000000
-111111111
-511111011
-12810000000

Decimal to binary table

DecimalBinaryDecimalBinaryDecimalBinary
0011101132100000
1112110050110010
210131101641000000
311141110991100011
41001511111001100100
510116100001271111111
6110171000112810000000
7111181001020011001000
81000191001125511111111
910012010100256100000000
101010211010110001111101000

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

decimal_to_binary.py Run this code
# 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

decimal_to_binary.c Run this code
#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

decimalToBinary.js Run this code
// 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.