C Decimal to Binary Conversion

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is Decimal to Binary Conversion?

Decimal (base 10) is the everyday number system. Binary (base 2) uses only bits 0 and 1. Conversion means rewriting a decimal integer as a string of bits.

Remember
Divide by 2 → remainder is the next bit (LSB first)
Reverse the remainders → MSB … LSB binary string

Example: 13
  13 ÷ 2 = 6 rem 1
   6 ÷ 2 = 3 rem 0
   3 ÷ 2 = 1 rem 1
   1 ÷ 2 = 0 rem 1
  Remainders (LSB→MSB): 1 0 1 1
  Binary (MSB→LSB):     1101
DecimalBinaryCheck
00special case
1311018+4+1
4210101032+8+2

How to Convert Decimal to Binary in C

Four clear steps — then pick a remainder loop or an MSB bit scan.

  1. Handle n == 0 by printing 0.
  2. While n > 0, record n % 2 and set n = n / 2.
  3. Print the collected remainders from last to first (MSB … LSB).
  4. Or find the highest set bit and print (n >> i) & 1 downward.
MethodIdeaTimeBest for
Remainder loopDivide by 2; reverse remaindersO(log n)Interviews, learning place values
MSB bit scanPrint bits from high to lowO(log n)No reverse step, bit practice

Pseudocode

Pseudocode
function DecimalToBinary(n):
    if n == 0:
        print 0
        return

    bits = empty array
    while n > 0:
        bits.append(n mod 2)
        n = n / 2

    for i from length(bits) - 1 down to 0:
        print bits[i]

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
OperationDivide by 2; remainder is a bit
First remainderLSB (rightmost bit)
ZeroPrint 0 — do not skip
Bit test(n >> i) & 1
NegativesUsually restrict to n >= 0 in tutorials
Classic check13 → 1101

Live Preview

Enter a nonnegative integer to see the remainder steps and the binary result.

Nonnegative integers (capped at 2147483647). Tap a chip or type a value.

Result
n = 13
Remainders (LSB first): 1, 0, 1, 1
Binary = 1101

Worked Walkthrough — n = 13

Divide by 2 until the quotient is 0. Collect remainders, then reverse.

Calculation
13 ÷ 2 = 6  remainder 1   ← LSB
 6 ÷ 2 = 3  remainder 0
 3 ÷ 2 = 1  remainder 1
 1 ÷ 2 = 0  remainder 1   ← MSB

Remainders collected: 1 0 1 1
Reversed binary:      1 1 0 1  →  1101

Check: 8 + 4 + 0 + 1 = 13

C Programs

Start with the remainder loop so you can explain every bit. Then use an MSB bit scan when you want to print without reversing an array.

Example 1 — Remainder Loop

Store n % 2 values in an array, then print them from last index to first.

C
#include <stdio.h>

void decimal_to_binary(int n)
{
    int bits[32];
    int count = 0;
    int i;

    if (n < 0)
    {
        printf("Use a nonnegative integer.\n");
        return;
    }
    if (n == 0)
    {
        printf("0\n");
        return;
    }

    while (n > 0)
    {
        bits[count++] = n % 2;
        n /= 2;
    }

    for (i = count - 1; i >= 0; i--)
    {
        printf("%d", bits[i]);
    }
    printf("\n");
}

int main(void)
{
    int number = 13;
    printf("%d -> ", number);
    decimal_to_binary(number);
    return 0;
}

How It Works

  1. Reject negatives for this beginner version; print 0 immediately when n is zero.
  2. Each n % 2 is the next bit from the right (LSB first).
  3. n /= 2 shifts toward higher bits until the quotient becomes 0.
  4. Print from count - 1 down to 0 so the string reads MSB to LSB.
  5. For 13, remainders 1,0,1,1 reverse to 1101.

Example 2 — MSB Bit Scan

Find the highest set bit, then print each bit from that position down to 0.

C
#include <stdio.h>

void decimal_to_binary(int n)
{
    int i;

    if (n < 0)
    {
        printf("Use a nonnegative integer.\n");
        return;
    }
    if (n == 0)
    {
        printf("0\n");
        return;
    }

    /* Find highest set bit */
    i = 31;
    while (i > 0 && ((n >> i) & 1) == 0)
    {
        i--;
    }

    for (; i >= 0; i--)
    {
        printf("%d", (n >> i) & 1);
    }
    printf("\n");
}

int main(void)
{
    decimal_to_binary(13); /* 1101 */
    decimal_to_binary(42); /* 101010 */
    decimal_to_binary(0);  /* 0 */
    return 0;
}

How It Works

  1. Keep the same nonnegative rule and special-case 0.
  2. Scan from bit 31 downward until you find the first 1 — that is the MSB of the minimal form.
  3. From that index down to 0, print (n >> i) & 1.
  4. No remainder array and no reverse step are required.
  5. Use the remainder loop in interviews when you must show divide-by-2; use the bit scan when bit tricks are welcome.

Edge Cases & Pitfalls

Check these before calling the solution done.

n = 0

Print 0

A bare while (n > 0) loop produces no output otherwise.

Reverse

LSB comes first

Printing remainders in collection order yields the bits backwards.

Negatives

Define the policy

Tutorials usually require n >= 0. Two’s complement needs a fixed width.

Width

Minimal vs padded

Default output drops leading zeros. Pad only when the problem asks for 8/16/32 bits.

Time and Space Complexity

ProgramTimeExtra space
Remainder loopO(log n)O(log n) for the bit array
MSB bit scanO(log n)O(1)

Key Takeaways

  • Method: divide by 2; remainders are bits (LSB first).
  • Reverse: print from last remainder to first for normal binary.
  • Bit scan: print (n >> i) & 1 from the MSB downward.
  • Test set: 0 → 0; 13 → 1101; 42 → 101010.

One line: collect n % 2 remainders and reverse them — or scan bits from the MSB.

Frequently Asked Questions

Repeatedly divide by 2 and collect remainders (LSB first), then print them in reverse — or scan bits from the highest set bit down to bit 0.
The first remainder is the least significant bit. Printing from last remainder to first gives the usual left-to-right binary string.
Handle n == 0 separately and print 0. A while (n > 0) loop would otherwise produce no output.
Find the highest bit that is set, then walk downward printing (n >> i) & 1 for each position. No array reverse is needed.
Both methods are O(b), where b is the number of output bits — about O(log n) for positive n.

Next: Decimal to Octal

Convert a base-10 integer into base 8 with divide-by-8 remainders.

Decimal to octal tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful