Decimal to binary conversion is the process of converting a decimal number (base 10) into its equivalent binary number (base 2). Decimal numbers use digits from 0 to 9, while binary numbers use only 0 and 1.
- Repeated division by 2 generates binary digits as remainders.
- Bitwise operations can also be used to extract the binary digits of a number.
Examples
Input: n = 12
Output: "1100"
Explanation: the binary representation of 12 is "1100", since 12 = 1×23 + 1×22 + 0×21+ 0×20 = 12Input: n = 33
Output: "100001"
Explanation: the binary representation of 33 is "100001", since 1×25 + 0×24 + 0×23 + 0×22 + 0×21 + 1×20 = 33
Approaches to Convert Decimal to Binary in C
We can convert a decimal number into its binary equivalent using repeated division by 2, bitwise operations, or recursion.
1. Decimal to Binary Conversion Using Repeated Division by 2
We repeatedly divide the decimal number by 2 and store the remainder obtained at each step. The remainders are then printed in reverse order to obtain the binary representation.
Algorithm:
- Initialize an array to store the binary digits.
- Divide the decimal number by 2 and store the remainder using n % 2.
- Update the number using n = n / 2.
- Repeat until the number becomes 0.
- Print the stored binary digits in reverse order.
#include <stdio.h>
// Function to convert decimal to binary
void decToBinary(unsigned int n){
// Handle the special case of 0
if (n == 0) {
printf("0");
return;
}
int binary[sizeof(n) * 8];
int i = 0;
// Store binary digits as remainders
while (n > 0) {
binary[i] = n % 2;
n /= 2;
i++;
}
// Print binary digits in reverse order
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
}
int main(){
unsigned int n = 10;
decToBinary(n);
return 0;
}
Output
1010
Explanation: For n = 10, repeated division by 2 produces the remainders 0, 1, 0, 1. Reversing these remainders gives 1010, which is the binary equivalent of 10.
2. Decimal to Binary Conversion Using Bitwise Operators
We use the bitwise AND (&) and right-shift (>>) operators to extract binary digits. The AND operator checks the last bit, while the right-shift operator moves the remaining bits one position to the right.
Algorithm:
- Initialize an array to store the binary digits.
- Extract the rightmost bit using n & 1.
- Right-shift the number by one position using n >> 1.
- Repeat until the number becomes 0.
- Print the stored bits in reverse order.
#include <stdio.h>
// Function to convert decimal to binary
void decToBinary(unsigned int n){
// Handle the special case of 0
if (n == 0){
printf("0");
return;
}
int binary[sizeof(n) * 8];
int i = 0;
// Extract binary digits using bitwise operations
while (n > 0) {
binary[i] = n & 1;
n = n >> 1;
i++;
}
// Print binary digits in reverse order
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
}
int main(){
unsigned int n = 17;
decToBinary(n);
return 0;
}
Output
10001
Explanation: The program extracts each binary digit using n & 1 and shifts the number right using n >> 1. The extracted bits are printed in reverse order, producing 10001 as the binary equivalent of 17.
3. Decimal to Binary Conversion Using Recursion
We can also convert a decimal number to binary using recursion. In this method, the function repeatedly calls itself with the number divided by 2 and prints the remainder while returning from the recursive calls.
Algorithm:
- Create a recursive function that accepts a decimal number.
- If the number is greater than 1, recursively call the function with n / 2.
- Print the remainder using n % 2.
- Continue until all binary digits are printed.
#include <stdio.h>
// Recursive function to convert decimal to binary
void decToBinary(unsigned int n){
// Recursively process higher-order bits
if (n > 1) {
decToBinary(n / 2);
}
// Print the current binary digit
printf("%u", n % 2);
}
int main(){
unsigned int n = 10;
decToBinary(n);
return 0;
}
Output
1010
Explanation: The function recursively divides 10 by 2 until it reaches the most significant binary digit. As the recursive calls return, the remainders are printed in the correct order, resulting in 1010.