Decimal to Binary
The idea behind the conversion
Our everyday numbers are in base 10 (decimal), using digits 0-9. Computers use base 2 (binary), using only 0 and 1. Converting a decimal number to binary means rewriting it using only those two digits.
The trick is simple: keep dividing the number by 2 and note the remainder each time. Each remainder is a binary digit (a bit). When the number reaches 0, you have all the bits — you just read them in reverse. Let's see exactly why with a dry run.
Step-by-step dry run
Let's convert 13 to binary by hand, dividing by 2 each step:
| Step | Number | ÷ 2 quotient | Remainder (bit) |
|---|---|---|---|
| 1 | 13 | 6 | 1 |
| 2 | 6 | 3 | 0 |
| 3 | 3 | 1 | 1 |
| 4 | 1 | 0 | 1 |
The remainders in the order we found them are 1, 0, 1, 1. Reading them bottom to top (reverse) gives 1101 — that is 13 in binary.
Method 1: divide by 2 (array)
The most common program stores each remainder in an array, then prints the array backwards.
#include <stdio.h>
int main() {
int n = 13, binary[32], i = 0;
if (n == 0) { printf("0"); return 0; } // handle zero
while (n > 0) {
binary[i] = n % 2; // save the remainder
n = n / 2; // move to next
i++;
}
// print in reverse
for (int j = i - 1; j >= 0; j--)
printf("%d", binary[j]);
return 0;
}1101
The loop fills the array with 1, 0, 1, 1; the reverse-print turns it into 1101.
Method 2: using recursion
Recursion removes the array entirely. By calling itself before printing, the bits naturally come out in the right order.
#include <stdio.h>
void toBinary(int n) {
if (n == 0) return;
toBinary(n / 2); // go deeper first
printf("%d", n % 2); // print on the way back
}
int main() {
int n = 13;
if (n == 0) printf("0");
else toBinary(n);
return 0;
}1101
Because the function calls itself before printing, the deepest call (the most significant bit) prints first. The reversal happens for free.
Method 3: using bitwise operators
You can also inspect each bit directly with the right-shift >> and AND & operators.
#include <stdio.h>
int main() {
int n = 13;
int started = 0;
for (int i = 31; i >= 0; i--) {
int bit = (n >> i) & 1; // read the i-th bit
if (bit) started = 1;
if (started) printf("%d", bit);
}
return 0;
}1101
This walks from the highest bit down, skipping leading zeros until the first 1 appears. It is closest to how the computer actually stores the number.
Quick reference: decimal to binary
| Decimal | Binary |
|---|---|
| 2 | 10 |
| 5 | 101 |
| 8 | 1000 |
| 10 | 1010 |
| 13 | 1101 |
| 16 | 10000 |
Common mistakes
- Printing the remainders in the order collected instead of reversing them.
- Forgetting the special case for 0, which prints nothing otherwise.
- Using too small an array for large numbers (32 bits is a safe size for an int).
- Confusing
n % 2(the bit) withn / 2(the next number).
Modify Method 1 to read the number from the user with scanf, and test it with 0, 1, 25 and 255. Confirm 255 gives 11111111.
Summary
- Convert decimal to binary by repeatedly dividing by 2 and collecting remainders.
- The remainders must be read in reverse to get the correct binary number.
- Method 1 uses an array; Method 2 uses recursion; Method 3 uses bitwise operators.
- Always handle 0 as a special case.
- Example: 13 becomes
1101.
Frequently Asked Questions
How do you convert a decimal number to binary in C?
Why do we read the remainders in reverse?
What is 13 in binary?
1101. You can check it by adding place values: 8 + 4 + 0 + 1 = 13, which matches the bits 1-1-0-1 for the places 8, 4, 2, 1.Can I convert decimal to binary without an array in C?
n / 2 first, then prints n % 2 on the way back, which naturally produces the bits in the correct order without storing them in an array.How do I handle 0 when converting to binary?
n > 0 is false from the start, so it prints nothing. Handle it as a special case by printing 0 directly when the input is 0.