Binary Adder

Description:           This program performs the addition of two binary numbers.

 
Primary Inputs:    Two binary numbers
Primary Output:    Summation of the two input binary numbers
Platform Used:      Turbo C++ version 3.0, Borland International Inc.
 


/* This program adds two binary numbers */
#include
#include
#include
#define null 0
struct node
{
struct node *next; /*Pointer to next bit of this number*/
int item; /*To store the current bit of this number*/
};
typedef struct node nodeptr;
nodeptr *bin1,*bin2,*temp,*sum;
void main()
{
char ch;
int carry=0,flag=1;
clrscr();
printf("\n Enter the first binary no. : ");
bin1=((nodeptr *) malloc(sizeof(nodeptr)));
bin1->item=0;
bin1->next=null;
bin2=(nodeptr *) malloc(sizeof(nodeptr));
bin2->item=0;
bin2->next=null;

/*Loop for creating the linked list of the bits of first binary number*/
/*Loop while read input character is not a newline character(\n) */
/*Important Point: We store the binary number here in reverse order of bits i.e.
least significant bit is stored in the first node while highest significant bit is
stored in the last node of the linked list*/
while((ch=getchar())!='\n')
{
/*create a temporary node in the bin1 linked list*/
temp=(nodeptr *) malloc(sizeof(nodeptr));

/*Since we are taking character type input so we must convert it to
corresponding integer number. This is done by subtracting the ASCII
value of 0 from the ASCII value of current bit*/
temp->item=ch-'0';

/*Link the current read bit at the front of the list*/
temp->next=bin1;

/*Move bin1 pointer to point to the beginning of the linked list*/
bin1=temp;
}
printf("\n Enter the second binary no. : ");

/* Loop for creating the linked list of the bits of second binary number
Rest of the logic is same here*/
while((ch=getchar())!='\n')
{
temp=(nodeptr *) malloc(sizeof(nodeptr));
temp->item=ch-'0';
temp->next=bin2;
bin2=temp;
}

/*Initialize the sum pointer for storing the resultant sum value*/
sum=(nodeptr *) malloc(sizeof(nodeptr));
sum->next=null;

/*Loop for summing up the corresponding bits of the two numbers
This loop will continue untill atleast one of the lists has reached its end*/
while((bin1->next)!=null&&(bin2->next)!=null)
{
temp=(nodeptr *) malloc(sizeof(nodeptr));
temp->item=(bin1->item+bin2->item+carry)%2;
carry=(bin1->item+bin2->item+carry)/2;
temp->next=sum;
sum=temp;
bin1=bin1->next;
bin2=bin2->next;
}

/*At least One of the lists has reached its end, so we need to check for the remaining
bits in the other (if any) number*/
while(flag)
{
if(bin2->next!=null)
{
temp=(nodeptr *) malloc(sizeof(nodeptr));
temp->item=(bin2->item+carry)%2;
carry=(bin2->item+carry)/2;
temp->next=sum;
sum=temp;
bin2=bin2->next;
}
else if(bin1->next!=null)
{
temp=(nodeptr *) malloc(sizeof(nodeptr));
temp->item=(bin1->item+carry)%2;
carry=(bin1->item+carry)/2;
temp->next=sum;
sum=temp;
bin1=bin1->next;
}
else
flag=0;
}
printf("\n The sum of the above two nos. is : ");

/*Code for printing out the resultant binary number*/
if(carry!=0)
printf("%d",carry);
while(sum->next!=null)
{
printf("%d",sum->item);
temp=sum;
sum=sum->next;
free((char *) temp);
}
getch();
}

 

Rate this post

Leave a Reply