C programming
decimal to binary conversion
binary algorithm
coding tutorial
computer science

Decimal to binary algorithm in C

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a decimal (base-10) integer to binary (base-2) in C uses the repeated division algorithm: divide the number by 2, record the remainder (0 or 1), and repeat with the quotient until it reaches 0. The remainders, read in reverse order, form the binary representation. C provides no built-in function for this, so you implement it with a loop and an array, recursion, or bitwise operations. This article covers all three approaches plus handling of negative numbers and zero.

Iterative Method (Array)

c
1#include <stdio.h>
2
3void decimalToBinary(int n) {
4    if (n == 0) {
5        printf("0");
6        return;
7    }
8
9    int binary[32];  // 32 bits max for int
10    int i = 0;
11
12    int num = n;
13    if (num < 0) num = -num;  // Handle negative separately
14
15    while (num > 0) {
16        binary[i] = num % 2;
17        num = num / 2;
18        i++;
19    }
20
21    if (n < 0) printf("-");
22
23    // Print in reverse order (MSB first)
24    for (int j = i - 1; j >= 0; j--) {
25        printf("%d", binary[j]);
26    }
27}
28
29int main() {
30    decimalToBinary(13);   // Output: 1101
31    printf("\n");
32    decimalToBinary(255);  // Output: 11111111
33    printf("\n");
34    decimalToBinary(0);    // Output: 0
35    return 0;
36}

The algorithm for 13: 13/2=6 r1, 6/2=3 r0, 3/2=1 r1, 1/2=0 r1. Remainders reversed: 1101.

Recursive Method

c
1#include <stdio.h>
2
3void decimalToBinaryRecursive(int n) {
4    if (n == 0) return;
5
6    decimalToBinaryRecursive(n / 2);
7    printf("%d", n % 2);
8}
9
10int main() {
11    int num = 42;
12    if (num == 0) {
13        printf("0");
14    } else {
15        decimalToBinaryRecursive(num);
16    }
17    printf("\n");  // Output: 101010
18    return 0;
19}

Recursion naturally reverses the output order because the print happens after the recursive call (post-order). The base case is when n reaches 0.

Bitwise Method

c
1#include <stdio.h>
2
3void decimalToBinaryBitwise(unsigned int n) {
4    if (n == 0) {
5        printf("0");
6        return;
7    }
8
9    // Find the highest set bit
10    int bits = 0;
11    unsigned int temp = n;
12    while (temp > 0) {
13        bits++;
14        temp >>= 1;
15    }
16
17    // Print from MSB to LSB
18    for (int i = bits - 1; i >= 0; i--) {
19        printf("%d", (n >> i) & 1);
20    }
21}
22
23int main() {
24    decimalToBinaryBitwise(42);   // Output: 101010
25    printf("\n");
26    decimalToBinaryBitwise(255);  // Output: 11111111
27    printf("\n");
28    return 0;
29}

(n >> i) & 1 extracts the bit at position i. Shifting right by i positions moves that bit to position 0, and & 1 isolates it. This avoids arrays and modular arithmetic.

Returning as String

c
1#include <stdio.h>
2#include <string.h>
3
4char* decimalToBinaryString(int n, char* buffer, int size) {
5    buffer[size - 1] = '\0';
6    int pos = size - 2;
7
8    if (n == 0) {
9        buffer[pos] = '0';
10        return &buffer[pos];
11    }
12
13    unsigned int num = (n < 0) ? (unsigned int)(-n) : (unsigned int)n;
14
15    while (num > 0 && pos >= 0) {
16        buffer[pos--] = (num % 2) + '0';
17        num /= 2;
18    }
19
20    if (n < 0 && pos >= 0) {
21        buffer[pos--] = '-';
22    }
23
24    return &buffer[pos + 1];
25}
26
27int main() {
28    char buffer[34];  // 32 bits + sign + null terminator
29    printf("%s\n", decimalToBinaryString(13, buffer, 34));   // 1101
30    printf("%s\n", decimalToBinaryString(-13, buffer, 34));  // -1101
31    printf("%s\n", decimalToBinaryString(0, buffer, 34));    // 0
32    return 0;
33}

Fixed-Width Binary (8-bit, 16-bit, 32-bit)

c
1#include <stdio.h>
2
3void printBinary8(unsigned char n) {
4    for (int i = 7; i >= 0; i--) {
5        printf("%d", (n >> i) & 1);
6    }
7}
8
9void printBinary32(unsigned int n) {
10    for (int i = 31; i >= 0; i--) {
11        printf("%d", (n >> i) & 1);
12        if (i % 8 == 0 && i != 0) printf(" ");  // Space every 8 bits
13    }
14}
15
16int main() {
17    printBinary8(13);    // 00001101
18    printf("\n");
19    printBinary32(13);   // 00000000 00000000 00000000 00001101
20    printf("\n");
21    printBinary32(255);  // 00000000 00000000 00000000 11111111
22    printf("\n");
23    return 0;
24}

Fixed-width output shows leading zeros, which is useful for debugging memory layouts, network protocols, and hardware registers.

Two's Complement for Negative Numbers

c
1#include <stdio.h>
2
3void printTwosComplement(int n) {
4    // Cast to unsigned to see the actual bit pattern
5    unsigned int bits = *(unsigned int*)&n;
6    for (int i = 31; i >= 0; i--) {
7        printf("%d", (bits >> i) & 1);
8        if (i % 8 == 0 && i != 0) printf(" ");
9    }
10}
11
12int main() {
13    printTwosComplement(13);   // 00000000 00000000 00000000 00001101
14    printf("\n");
15    printTwosComplement(-13);  // 11111111 11111111 11111111 11110011
16    printf("\n");
17    printTwosComplement(-1);   // 11111111 11111111 11111111 11111111
18    printf("\n");
19    return 0;
20}

Negative integers in C use two's complement. The sign-magnitude approach (-1101) is not how hardware represents them. Use unsigned casting to see the actual bit pattern.

Common Pitfalls

  • Forgetting to handle zero: 0 / 2 = 0 with remainder 0, but the loop exits immediately producing no output. Always check for n == 0 as a special case.
  • Printing remainders in wrong order: The first remainder is the least significant bit (LSB). Print the array in reverse order or use recursion to get MSB-first output.
  • Integer overflow with negative numbers: -(-2147483648) overflows a 32-bit signed int because the positive range only goes to 2147483647. Use unsigned int for the absolute value.
  • Array too small for large numbers: A 32-bit integer needs at most 32 binary digits. Allocating a smaller array causes buffer overflow. Always use at least 32 elements.
  • Using %d format for binary: C's printf has no %b format specifier for binary (unlike some other languages). You must implement the conversion manually.

Summary

  • The repeated division algorithm divides by 2 and collects remainders in reverse order
  • Iterative method stores digits in an array and prints them backward
  • Recursive method uses the call stack to reverse the output naturally
  • Bitwise method uses (n >> i) & 1 to extract each bit without division
  • For fixed-width output, loop from the highest bit position down to 0
  • Negative numbers use two's complement in C — cast to unsigned to see the raw bit pattern

Course illustration
Course illustration

All Rights Reserved.