How to Add Two Numbers without using any arithmetic operators in C

If you know the Half duplex & Full duplex circuit then probably it is easy for you to understand this problem.

In Duplex circuit, you will use two gates AND & XOR.

Here also you need to do the bitwise AND & XOR operation for each bit for the variable. Generated carry for the first bit will be transferred for next bit calculation. 

To Make it easy I will show you the basic example:

0+0: Sum = 0
1+0 : Sum =1 
0+1 : Sum = 1
1+1 : Sum = 10 (2)

But in a single bit we can not accommodate 10 = 2. So we need a carry bit. 
Which will hold this second bit? 

So now,

1+1 : Sum = 0 with Carry = 1

So now,

0+0: Sum = 0 with carry = 0
1+0 : Sum =1  with carry = 0
0+1 : Sum = 1 with carry =0
1+1 : Sum = 0 with carry =1

If you see the output observed for carry bit is equal to the truth table of AND gate.
In        Out
(0,0) = 0
(0,1) = 0
(1,0) = 0
(1,1) = 1

And output observed for sum bit is equal to the truth table of XOR gate.

But this is true for one-bit calculation, what happen if we have 8 bits means we have the byte of data for addition. 

Example:1
2-bit addition

    11 = 3
+  10 = 2
_____
   101

First Bit addition: 0+1, sum =1, carry = 0, now this carry will be passed for second bit operation.
Second bit addition: 1+1+0, sum = 0, carry = 1

Example:2
2-bit addition

    11 = 3
+ 11 = 3
_____
  110 = 6

First Bit addition: 1+1, sum =0, carry = 1, now this carry will be passed for second bit operation.
Second bit addition: 1+1+1, sum = 1, carry = 1
or
(1+1, sum = 0 carry = 1 then 0+1, sum = 1)

Example:3
2-bit addition

    11 = 3
+  01 = 1
_____
   100 = 4 (1= carry, 0=sum, 0=sum)

First Bit addition: 1+1, sum =0, carry = 1, now this carry will be passed for second bit operation.
Second bit addition: 1+1+0, sum = 0, carry = 1
or
(1+1, sum = 0 carry = 1 then 0+1, sum = 1)

Once carry bit is 1 then it will remain as 1 then we need to add it with next bit sum to generate the new carry.

Below code show the implementation:

#include<stdio.h>
#include <stdlib.h>

void printbin(int pnum){    
    
    if(pnum != 0)
    {
        printbin(pnum >> 1);
        
        if (pnum & 1){
        printf("1");
        }
        else{
        printf("0");
        }
    }
    else{
        printf("0");        
    }
}

#define VNAME(x) #x
#define F_PRINTBIN(pnum) {      \
    printf("%s :",VNAME(pnum)); \
    printbin(pnum);             \
    printf(",%d",pnum);         \
    printf("\n");               \
}

void _printbin(int pnum){
    printbin(pnum);
    printf("\n");
}

int Sum(int x, int y)
{
    int carry, sum, correctSum, newCarry;
    correctSum = x + y;
    sum = x;
    
    F_PRINTBIN(x);
    F_PRINTBIN(y);
    
    F_PRINTBIN(correctSum);
    
    while (y != 0)
    {
        carry = sum & y;
        sum = sum ^ y;
        F_PRINTBIN(carry);
        y = carry << 1;
        F_PRINTBIN(sum);
        F_PRINTBIN(y);
    }
    return sum;
}

int main()
{
    int a = 10;
    int b = 15;
    printf("Final sum = %d\n", Sum(a, b));
    return 0;
}
 


Output:
x :01010,10                                                                                                                                                                     
y :01111,15                                                                                                                                                                     
correctSum :011001,25                                                                                                                                                           
carry :01010,10                                                                                                                                                                 
sum :0101,5                                                                                                                                                                     
y :010100,20                                                                                                                                                                    
carry :0100,4                                                                                                                                                                   
sum :010001,17                                                                                                                                                                  
y :01000,8                                                                                                                                                                      
carry :0,0                                                                                                                                                                      
sum :011001,25                                                                                                                                                                  
y :0,0                                                                                                                                                                          
Final sum = 25


Related Post:


Removing Space from Given String & Different Methods to Count Number of Ones in Given Value

Booting Sequence for Boot Process in Linux

How to count number of 1 or set bits in a given number

What is Segmentation fault ?

How to Access Private Data Members in C++ without using friend function

Process Vs Thread 

How to Add Two Numbers Without Using + Operator in C 

Overview of Function Pointer in C with Example - What is function pointer & how to use it

How to Fork child process with waitpid & execl in linux with c example code

Share on Google Plus

About Kapil Thakar

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...