Decimal to binary algorithm in C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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)
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
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
(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
Fixed-Width Binary (8-bit, 16-bit, 32-bit)
Fixed-width output shows leading zeros, which is useful for debugging memory layouts, network protocols, and hardware registers.
Two's Complement for Negative Numbers
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 = 0with remainder 0, but the loop exits immediately producing no output. Always check forn == 0as 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. Useunsigned intfor 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
printfhas no%bformat 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) & 1to 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
Related reading
- Decision boundaries for nearest centroid
- Decision tree implementation for returning the next feature to split the tree
- Decision Tree in Matlab
- Decision Tree Learning and Impurity
- Default parameter for value must be a compile time constant?
- Designing distributed application in C
- Decision trees. Choosing thresholds to split objects
- Deep Q Network is not learning

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.