consider 2 nos a = 1010 b = 1011 ( in bits)
take xor of these
c = a ^ b;
c = 1010
^1011;
c = 0001
idea is if there are 2 1s then make it 0 , if one 1 and one 0 then make it 1;
d = a&b;
when there are 2 ones u have to produce a carry .
d = 1010
1011
1000
this d needs to be right shifted so that we can carry on addition ,
just add n carry problem
d = d<<1
d = 10000
now c and d are your 2 new nos
a = c ;
b = d;
proceed until b becomes zero
a = 1011
b =10000
c = 11011
d = 00000
a =c ;
a is the final answer
Hope it,s clear to you
On Thu, Oct 1, 2009 at 10:59 AM, Miroslav Balaz <[email protected]> wrote:
> make into bits and then using logic operators. but this kind of problem
> sucks.
> the good way is
> let A and B are the numbers
>
>
> string s(A,'*'),p(B,'*');
> s.append(p);
> printf(" %d + %d=%d",A,B,s.size());
>
> 2009/9/7 ritter <[email protected]>
>>
>> how to add two integers without using arithmetic operators?
>> can anyone help me.
>>
>>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---