C program for adding two Binary Numbers

We all know how to add two decimal numbers (2+2 = 4) «m a genius»wink , but do you know how to add two binary numbers (100010 + 010010 = ?) if you don’t then do not worry in this article we will see how to add two binary numbers and then we will write a C program to calculate the sum of two binary numbers.

So let’s get started, there are 4 elements present in addition of two binary numbers

  • First binary number bit (a)
  • Second Binary number bit (b)
  • resultant bit (d)
  • Carry bit (c)

Now the rules to add two binary numbers is shown in the below image

Binary Addition in C

In the above results when we add binary 1 and 1 we got resultant 0 and carry 1 because adding two 1 will make the resultant numbers greater than a single bit so we store that extra bit in our Carry ‘C’

Now will try to add some binary numbers using the above results.

c 0 0 1 1 1
——————
1 0 0 1 0 1
+ 0 1 0 0 1 1
——————
1 1 1 0 0 0

‘c’ represents carry

In the given example we simply follow the above set of rules, when we got 1 and 1 the resultant was 0 and the carry was 1 and the carry bit is then added into the next bits.

Now we know how to add two binary numbers, now we will write a C program to add two binary numbers.

For the program we are going to make three functions

  • take_bin() : to input a binary number in form of an array.
  • print_bin() : to output a givent binary number.
  • add_bin(): to add two binary numbers(one we are lookin for)

So first lets code eacho of the above function

take_bin()

int take_bin(int x[]){ int i; for(i=0;i<COUNT;i++){ scanf("\%d",&x[i]); }  return 0; }

The above function will simply run a loop upto the constant COUNT to input all the elements of an array, the reason why we are using array is that if we store our binary number using int datatype it will trim 010010 to 10010 which is not correct and we don’t want that.

Now lets see the print_bin() function

int print_bin(int x[]){ int i; for(i=0;i<COUNT;i++){ printf("\%d",x[i]); } printf("");  }

print_bin() function will simply print all the elements of the array using a for loop upto COUNT in our case COUNT is 4 which means a 4-BIT binary number.

add_bin() function

int add_bin(int x[],int y[]){ int i,carry=0; for(i = COUNT-1;i>=0;i--){ if(x[i]==0 && y[i]==0 && carry==0){ sum[i] = 0; carry = 0; } else if(x[i]==0 && y[i]==0 && carry==1){                       sum[i] = 1;                       carry = 0;               } else if(((x[i]==1 && y[i]==0)||(x[i]==0 && y[i]==1)) && carry==0){ sum[i] = 1; carry = 0; } else if(((x[i]==1 && y[i]==0)||(x[i]==0 && y[i]==1)) && carry==1){                       sum[i] = 0;                       carry = 1;               } else if(x[i]==1 && y[i]==1 && carry==0){ sum[i] = 0; carry = 1; } else if(x[i]==1 && y[i]==1 && carry==1){                       sum[i] = 1;                       carry = 1;               } } printf("+"); printf("--------"); print_bin(sum); }

In the above function we are following the same logic as we have seen previously, we are checking for the different conditions for each bit stored in our arrays and accordingly we are inserting value into the sum[] array which will store the resultant bits and carry variable which will strore the carry bit.

Let’s C the full code.

#include<stdio.h> #define COUNT 4   //change the value of COUNT to increase the no. of bits int sum[COUNT]; int take_bin(int x[]); int print_bin(int x[]); int add_bin(int x[],int y[]); int main(){ int m[COUNT],q[COUNT]; printf("Enter first Binary number(space between each bit): "); take_bin(m); printf("Enter second  Binary number (space between each bit): "); take_bin(q); print_bin(m); print_bin(q); add_bin(m,q); return 0; } int take_bin(int x[]){ int i; for(i=0;i<COUNT;i++){ scanf("\%d",&x[i]); }  } int print_bin(int x[]){ int i; for(i=0;i<COUNT;i++){ printf("\%d",x[i]); } printf("");  } int add_bin(int x[],int y[]){ int i,carry=0; for(i = COUNT-1;i>=0;i--){ if(x[i]==0 && y[i]==0 && carry==0){ sum[i] = 0; carry = 0; } else if(x[i]==0 && y[i]==0 && carry==1){                       sum[i] = 1;                       carry = 0;               } else if(((x[i]==1 && y[i]==0)||(x[i]==0 && y[i]==1)) && carry==0){ sum[i] = 1; carry = 0; } else if(((x[i]==1 && y[i]==0)||(x[i]==0 && y[i]==1)) && carry==1){                       sum[i] = 0;                       carry = 1;               } else if(x[i]==1 && y[i]==1 && carry==0){ sum[i] = 0; carry = 1; } else if(x[i]==1 && y[i]==1 && carry==1){                       sum[i] = 1;                       carry = 1;               } } printf("+"); printf("--------"); print_bin(sum); }

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий