How to Convert Decimal to Binary
Binary (base 2) writes every number using only 0 and 1 - the language of every digital circuit. There are two common hand methods: repeated division by 2, and subtracting powers of two. This guide covers both, with a live worked example.
Method 1: repeated division by 2
The mechanical method that always works:
- Divide the number by 2.
- Write down the remainder (always 0 or 1).
- Divide the quotient by 2 and record the remainder again.
- Repeat until the quotient is 0.
- Read the remainders from last to first.
Method 2: subtract powers of two
Often faster mentally: find the largest power of two that fits, subtract it, and repeat. For 45: the largest power that fits is 32, leaving 13; then 8, leaving 5; then 4, leaving 1; then 1. Powers used: 32+8+4+1, so bits 5, 3, 2, and 0 are set, giving 101101.
This is why programmers memorize powers of two: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024... Every binary number is just a checklist of which powers are included.
How many bits will I need?
A number n needs ⌊log₂(n)⌋ + 1 bits. Practical landmarks: anything up to 255 fits in 8 bits (one byte), up to 65535 in 16 bits, and up to about 4.29 billion in 32 bits. Leading zeros never change the value - 101 and 00000101 are both five.
How 45 converts from decimal to binary
Divide by 2 repeatedly and keep each remainder, then read the remainders from bottom to top:
Result (remainders bottom to top): 101101
Doing it in code
const decimal = 10;
const binary = decimal.toString(2); // "1010"
const padded = decimal.toString(2).padStart(8, '0'); // "00001010"Frequently Asked Questions
What is the fastest way to convert decimal to binary?
Mentally, subtract the largest fitting powers of two and mark those bit positions with a 1. On paper, repeated division by 2 is more mechanical and less error-prone. In code, use num.toString(2) in JavaScript or bin(num) in Python.
Why do computers use binary instead of decimal?
Electronic circuits distinguish two voltage states reliably - on and off. Ten distinct states per wire would be far more error-prone. Binary maps directly onto that physical reality, and all other bases are just human-friendly views of it.
How do I convert negative decimals to binary?
Computers store negative integers in two's complement: write the positive value, invert every bit, and add 1, within a fixed width like 8 or 32 bits. -5 in 8-bit two's complement is 11111011.