- First, we add the h standard input/output library to our C program.
- The binary Addition function returns the total as a long, long integer , which requires two binary values as arguments.
- We define variables for the current bit's carry, result, and index inside the binary Addition function.
- The while loop continues to run until both binary integers are zero .
- We use the modulus operator% to extract the least significant bits of both values on each iteration of the loop.
- We add the two parts together with the carry from the previous iteration.
- After the sum is split by two to determine the carry for the following iteration, the remainder becomes the current bit of the result.
- The result is added to the current bit after being multiplied by the appropriate power of 10 .
- After all the bits have been handled, the function determines whether any carry is still there and adds it to the result appropriately.
- The primary function requests two binary numbers from the user, invokes the binary Addition function, and outputs the result.
C Program for Adding Binary Numbers:
#include <stdio.h>
#include <math.h>
long longbinaryAddition(long long binaryNum1, long long binaryNum2) {
int carry = 0;
long long result = 0;
int i = 0;
while (binaryNum1 != 0 || binaryNum2 != 0) {
int bit1 = binaryNum1 % 10;
int bit2 = binaryNum2 % 10;
int sum = bit1 + bit2 + carry;
carry = sum / 2;
sum = sum % 2;
result = result + (sum * pow(10, i));
i++;
binaryNum1 = binaryNum1 / 10;
binaryNum2 = binaryNum2 / 10;
}
if (carry != 0) {
result = result + (carry * pow(10, i));
}
return result;
}
int main() {
long long binaryNum1, binaryNum2;
printf("Enter the first binary number: ");
scanf("%lld", &binaryNum1);
printf("Enter the second binary number: ");
scanf("%lld", &binaryNum2);
long long sum = longbinaryAddition(binaryNum1, binaryNum2);
printf("The sum of %lld and %lld is: %lld\n", binaryNum1, binaryNum2, sum);
return 0;
}
Output:
When combining the binary numbers 1101 and 1010, the result is the sum 10111.
Enter the first binary number: 1101
Enter the second binary number: 1010
The sum of 1101 and 1010 is: 10111
Conclusion:
In essence, performing addition of binary numbers in the C programming language involves the conversion of binary values to decimal, the addition operation, and the subsequent conversion of the result back to binary. By inputting two binary numbers into the provided C program, users can efficiently execute this procedure and obtain the sum as the final output.
Programmers can gain valuable insights by understanding binary addition and its application in various computer science and digital electronics domains. Bitwise manipulations, data representation, and logic gate configurations are among the critical areas where binary addition plays a crucial role. Proficiency in this concept empowers developers to work proficiently with low-level programming languages and manipulate binary information effectively.
The showcased C program employs concepts such as modulo, division, and basic math functions to illustrate the systematic process of binary addition. This program is versatile and can adjust to various scenarios as it is designed to manage binary values regardless of their length.
Readers of this article will gain a thorough grasp of binary addition in C through an in-depth exploration of the code, syntax, and illustrations provided here. By delving into these resources, developers can enhance their coding skills and expand their comprehension of fundamental processes. Equipped with this insight, programmers can tackle more demanding projects and dive deeper into the intricacies of binary operations within the realm of computer science.