Program to add two Binary numbers














































Program to add two Binary numbers



Description:
Binary numbers are the numbers which are expressed in the binary system or base 2 numeral system generally consisting of two numerical values 1 and 0.Binary addition is generally done as follows case -i If A=0 and B=0 then sum=0 and carry=0.
case-ii If A=0 and B=1 or A=1 and B=0 then the sum=1 and the carry=0.
case-iii If A=1 and B=1 then the sum=o and a carry=1 will be generated.

Let us consider a example:


From the above example we can assume the algorithm as
Algorithm: We need to check both the given numbers by traversing digits of both the numbers ,there would be 3 possible cases.
Case-1 if both the given numbers are zero that is A=0 and B=0.
             if there is no previous carry then the answer would be 0
             else if there is a previous carry then the answer would be 1.
Case-2 if one of the number is 1 and the other is 0 that is if A=0 and B=1 or A=1 and B=0.
            if there is no previous carry then the answer would be 1.
            else if there is a previous carry then the answer would be 0 and it would generate a further carry 1.
Case-3 if both the numbers are 1
                 if there is no previous carry then the answer would be 0.
            else if there is a previous carry then the answer would be 0 and it would generate a further carry 1.
Code:
#include<iostream>
using namespace std;
int addBinary(int a,int b);/* declaring function to add the binary numbers*/
 int reverse(int n);/*function to reverse the number*/

int main(){
/*declaring two variables a and b to take two numbers as input*/
 int a,b;
 cout<<"Enter the value of the 1st Binary number";
 cin>>a;
  cout<<"Enter the value of the 2nd Binary number";
 cin>>b;
 /* printing the value returned by the function that is result or sum of the 2 binary numbers*/
 cout<< addBinary(a,b)<<endl;
 
}
int addBinary(int a,int b){
    /* variable to store the sum and initialising its value to zero*/
    int ans=1;
    /*variable to store the previous carry  and initialising its value to zero*/
    int prevCarry=0;
    while(a>0 && b>0){
        /* coding the 3 cases learned above*/
        if(a%2==0 && b%2==0){
            ans=ans*10+prevCarry;
            /*initialising prevCarry=0 because there is no prevCarry generated above*/
            prevCarry=0;
        }
        else if((a%2==0 && b%2==1) || (a%2==1 && b%2==0)){
            /*firstly we need to check if prevCarry was 0 or 1*/
            if(prevCarry==1){
                ans=ans*10+0;
                prevCarry=1;
            }
            else{
                ans=ans*10+1;
                prevCarry=0;
            }
            
        }
        else /* if both the numbers are 1*/
        {
            ans=ans*10+prevCarry;
            prevCarry=1; /*since 1+1 always generates a carry 1*/
        }
        /* updating a and b values*/
        a/=10;
        b/=10;
    }
    /* if one of the bnumbers a or b has been finished traversing  that is the input numbers a and b are not of same lenght then we need to take care of the other number and add it with  the carry with the left out numbers*/
    while(a>0){
        if(prevCarry==1){
           /* we need to check whether the number to which we are adding the prevCarry is 1 or not*/
           if(a%2==1){
               ans=ans*10 + 0;
               prevCarry=1;/*since 1+1 generates a carry*/
           }
           else{
               ans=ans*10 + 1;
               prevCarry=0;/*initialising it to zero since the prevCarry has been utilized above*/
               
           }
        }
        /* if prevCarry==0*/
        else{
            ans=ans*10 + (a%2);
        }
        /*updating value of a*/
        a/=10;
    }

/* same goes for b as well if b has been leftout */
    while(b>0){
        if(prevCarry==1){
           /* we need to check whether the number to which we are adding the prevCarry is 1 or not*/
           if(b%2==1){
               ans=ans*10 + 0;
               prevCarry=1;/*since 1+1 generates a carry*/
           }
           else{
               ans=ans*10 + 1;
               prevCarry=0;/*initialising it to zero since the prevCarry has been utilized above*/
               
           }
        }
        /* if prevCarry==0*/
        else{
            ans=ans*10 + (b%2);
        }
        /*updating value of b*/
        b/=10;
    }
    /* if and b values are of same length that is input numbers are equal then atlast if we are leftout with a carry*/
    if(prevCarry==1){
        /* adding the prevCarry to the final answer*/
        ans= ans*10+1;
    }
    /* we need to reverse the result because we are adding the numbers here since we are adding numbers by using the %10 logic the first number will become the last number so we need to reverse  the number*/
    /*function to reverse the number*/
   ans =reverse(ans);
   return ans;
}

int reverse(int n){/*declaring n as a parameter to store the value of the answer from the above function*/
    int ans=0;/* declaring  variable to store the reverse number and initialising it to 0 */
    while(n>0){
        int lastnum=n%10;
        ans=ans*10+lastnum;
         n/=10;/*updating n value*/
    }
         /*returning value to the above function where reverse function has been called*/
         ans=ans/10 ;
    return ans;
}



            





More Articles of Shaik Aftab Ahmed:

Name Views Likes
C++ Program to Find the Frequency of Odd & Even Numbers in the given Matrix 197 1
C++ program to Sort a Linked List Using Merge Sort 192 1
C++ Program to Implement a Linked List representation of a Binary tree 179 1
C++ Program to Check for balanced parentheses in an expression 130 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 137 1
C++ program to print Indian flag 167 1
C++ program to Convert a multi level linked list to a singly linked list 134 1
C++ program to print right view of a Binary tree using queue 130 1
C++ Program to implement Huffman Coding Compression Algorithm 164 1
C++ Program to Create a Height Balanced Binary Tree 135 1
C++ program to implement Prims algorithm 192 1
C++ Program for BFS Traversal 157 1
C++ Progam to Evaluate a Prefix Expression 153 1
C++ Program to Implement Queue using Linked List 162 1
C++ implementation of Stack using Linked list 160 1
C++ program to find the intersection point of two linked lists 211 1
C++ program to count the inversions in the given array 203 1
C++ program to perform D.N.F sort 218 1
C++ program to print all possible subsets of a String 201 1
C++ program to count the number of ones in a binary representation of a number 219 1
C++ program to print all possible subsets of a set 222 1
C++ program to find the largest word in a String 210 1
C++ Program to print a matrix in Spiral order 217 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 216 1
C limits library 233 1
Program to add two Binary numbers 218 1

Comments