# Addition of two binary numbers using C

Addition of two binary numbers using C

Basic Theory
Addition of binary numbers is far simpler than that of decimal number. This is because, binary addition includes addition between 1 and 0 only. The addition process results into two unit: sum and carry
 sum = a xor b xor c carry = ab + bc+ ac

Initially the carry bit is set to be 0. This process is continue until all the bits in a binary number finish.
Code Assumption

The following code uses 8 – bit binary unsigned integer. This means each integer can hold from 0 to 255 in decimal. So user can add up to 255. If the result is greater than 255, then condition of overflow occurs. The code contains following segments
• Decimal to Binary Conversion
• Addition of two binary numbers
• Binary to Decimal conversion
This is , first user enters the operands in decimal format like 100 and 125. Then program converts both operands into equivalent binary format like 011001000 and 01111101. The program adds two binary numbers using above method. The results is obtained in binary format like 11100001. Then finally the binary number is converted back to decimal format like 225.
Source Code:
```  1: #include
2: #include
3:
4:
5: void decimalToBinary(int op1, int aOp[]){
6:     int result, i = 0;
7:     do{
8:         result = op1 % 2;
9:         op1 /= 2;
10:         aOp[i] = result;
11:         i++;
12:     }while(op1 > 0);
13: }
14:
15: int binaryToDecimal(int array[]){
16:     int sum = 0, i;
17:     for(i = 0; i < 8; i++){
18:         if(array[i]) sum += pow(2,i);
19:     }
20:     return sum;
21:
22: }
23: void showBinary(int array[], int n){
24:     int i;
25:     for(i = n -1; i >=0; i--){
26:         printf("%d ", array[i]);
27:     }
28:     printf("\n");
29: }
30:
31: int addBinary(int a1[], int a2[], int result[]){
32:     int i, c = 0;
33:     for(i = 0; i < 8 ; i++){
34:         result[i] = ((a1[i] ^ a2[i]) ^ c); //a xor b xor c
35:         c = ((a1[i] & a2[i]) | (a1[i] &c)) | (a2[i] & c); //ab+bc+ca
36:     }
37:     result[i] = c;
38:     return c;
39: }
40:
41: int main(){
42:     int op1, op2, sum;
43:     int  aOp1 = {0,0,0,0,0,0,0,0};
44:     int  aOp2 = {0,0,0,0,0,0,0,0};
45:     int  aSum = {0,0,0,0,0,0,0,0};
46:     printf("Enter two operands (0 to 255): ");
47:     scanf("%d %d", &op1, &op2);
48:     while(op1 < 0 || op1 > 255 || op2 < 0 || op2 > 255 ){
49:         printf("Enter two operands (0 to 31): ");
50:         scanf("%d %d", &op1, &op2);
51:     }
52:
53:     decimalToBinary(op1, aOp1);
54:     decimalToBinary(op2, aOp2);
55:
56:     printf("Binary Equivalent of %d is ",op1);
57:     showBinary(aOp1, 8);
58:     printf("Binary Equivalent of %d is ",op2);
59:     showBinary(aOp2, 8);
60:
61:     if(!addBinary(aOp1, aOp2, aSum)){
62:         printf("Sum of the two number is : ");
63:         showBinary(aSum, 8);
64:
65:         sum = binaryToDecimal(aSum);
66:         printf("Binary eqivalent is: %d", sum);
67:     }else{
68:         printf("Overflow");
69:     }
70:     return 0;
71: }
72:
```