C Programming Code Examples C > Bitwise Operators Code Examples C Program to Perform Binary Addition of Strings and Print it C Program to Perform Binary Addition of Strings and Print it #include <stdio.h> #include <string.h> /* global variables */ char string1[8], string2[8], string3[8]; int j, k; char carry = '0'; /* function prototype */ void binary_add(char *,char *); void main() { printf("enter string1\n"); scanf(" %[^\n]s", string1); printf("enter string2\n"); scanf(" %[^\n]s", string2); binary_add(string1, string2); printf("binary addition of number is\n"); if (carry == '1') { string3[j] = '1'; for (j = 1;j <= k + 1;j++) printf("%c", string3[j]); printf("\n"); } else { for (j = 1;j <= k + 1;j++) printf("%c", string3[j]); printf("\n"); } } /* function to add two binary numbers in a string */ void binary_add(char *string1, char *string2) { char *p1, *p2; p1 = string1; p2 = string2; k = strlen(string1); for (;*p1 != '\0' && *p2 != '\0';p1++, p2++); p1--; p2--; string3[k+1] = '\0'; for (j = k + 1;j >= 1;j--, p1--, p2--) { if (*p1 == '0' && *p2 == '0'&& carry == '0') { string3[j] = (*p1 ^ *p2) ^ carry; carry = '0'; } else if (*p1 == '0' && *p2 == '0' && carry == '1') { string3[j] = (*p1 ^ *p2)^ carry; carry = '0'; } else if (*p1 == '0' && *p2 == '1' && carry == '0') { string3[j] = (*p1 ^ *p2)^ carry; carry = '0'; } else if (*p1 == '0' && *p2 == '1' && carry == '1') { string3[j] = (*p1 ^ *p2)^ carry; carry = '1'; } else if (*p1 == '1' && *p2 == '0' && carry == '0') { string3[j] = (*p1 ^ *p2)^ carry; carry = '0'; } else if (*p1 == '1' && *p2 == '0' && carry == '1') { string3[j] = (*p1 ^ *p2)^ carry; carry = '1'; } else if (*p1 == '1' && *p2 == '1' && carry == '0') { string3[j] = (*p1 ^ *p2)^ carry; carry = '1'; } else { string3[j] = (*p1 ^ *p2)^ carry; carry = '1'; } } }