Addition of 2 numbers without + sign

Status
Not open for further replies.

Garbage

God of Mistakes...
Hello Friends,

Here is the another assignment problem.
This time, I've to add 2 numbers without using + sign.

I got some solutions as ..
Code:
a+b = a - (-b)               ====> This I don't want !!!

This works on paper ===>
Code:
                1 1
a= 6 ==>    0 1 1 0
                           +
b=10==>    1 0 1 0
               ---------
c=16==>  1 0 0 0 0
So, this is the BINARY ADDITION.

But how I can implement it ???

I NEED LOGIC ONLY !!! NO PROGRAM PLEASE !!!
 

arunks

Hey here is the aks
i think we can use | (OR operator ) to perform addition... plz check i m not sure
this program is giving corect answer thats why i m saying
main()
{
int a=3,b=4,c=0;
c=a|b;
printf("%d",c)
}
 

sam_1710

Youngling
^^ nope dats does not give da ans..
It does not give for some values..
Try givin a=8 and b=8, or ... 15 and 10 or so..

_____________________
This code works for certain numbers...

Code:
if(a<b)
{
     t=a;
     a=b;
     b=t;
}
printf("%d",a^b);

_____________
EDIT
got it..
Code:
unsigned int add(unsigned int a, unsigned int b)
{      unsigned int c=0, r=0, t=~0;
        for(t= ~0;t;t>>=1)
       {       r<<=1;
                r|=(a^b^c)&1;
                c=((a|b)&c|a&b)&1;
                a>>=1;
                b>>=1;
         }
        for (t=~0,c=~t;t;t>>=1)
         {     c<<= 1;
                c|= r&1;
                r>>= 1;
         }     return c;
}

From here
 
Last edited:
Status
Not open for further replies.
Top Bottom