The conversion between binary and decimal number systems is a fundamental concept in computer science, crucial for understanding how computers process and store data. Binary numbers, consisting solely of 0s and 1s, are native to machine-level operations, while decimal numbers are used by humans for more intuitive understanding and interaction.
In this article, you will learn how to implement conversions between binary and decimal numbers in C programming. By examining detailed code examples, gain insights into the logic and methods for performing these conversions efficiently.
Binary to decimal conversion involves reading a binary number (base-2) and transforming it into its decimal (base-10) equivalent. This process relies on understanding the weight (power of 2) of each binary digit based on its position.
Declare variables to store the binary number, decimal number, and the base exponent.
Use a loop to process each digit of the binary number from the least significant digit (rightmost) to the most significant digit (leftmost).
Extract each binary digit and multiply it by the appropriate power of two.
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long long binaryNumber) {
int decimalNumber = 0, i = 0, remainder;
while (binaryNumber != 0) {
remainder = binaryNumber % 10;
binaryNumber /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}
return decimalNumber;
}
int main() {
long long binaryNumber;
printf("Enter a binary number: ");
scanf("%lld", &binaryNumber);
printf("Decimal number is: %d\n", binaryToDecimal(binaryNumber));
return 0;
}
In this code, binaryToDecimal
function calculates the decimal value by adding the products of binary digits and their corresponding powers of two. The while
loop continues until the binary number diminishes to zero.
Converting a decimal number to binary involves dividing the decimal number by 2 and recording the remainder for each division operation until the number is reduced to 0. The remainders constitute the binary representation in reverse order.
Declare variables to store the decimal number and the binary equivalent.
Implement a loop to perform division by 2 and collect remainders.
Store or display the binary number in the correct order.
#include <stdio.h>
void decimalToBinary(int decimalNumber) {
int binaryNumber[32], i = 0;
while (decimalNumber > 0) {
binaryNumber[i] = decimalNumber % 2;
decimalNumber /= 2;
i++;
}
for (int j = i - 1; j >= 0; j--) {
printf("%d", binaryNumber[j]);
}
}
int main() {
int decimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
printf("Binary number is: ");
decimalToBinary(decimalNumber);
printf("\n");
return 0;
}
This snippet utilizes an array binaryNumber
to store binary digits. The conversion loop generates binary digits, stored in reverse, which are then printed in the correct sequence starting from the highest index.
Through clear examples, mastering the conversion of binary to decimal and vice versa in C becomes attainable. These fundamental operations are not only essential for various computing tasks but also serve as a great exercise for enhancing problem-solving and programming skills in C. Incorporate these conversions in projects where binary data manipulation is required, ensuring efficient data processing and manipulation.