> int add(int a, int b)
> {
>           do
>           {
>                      a=a^b;// sum without carry
>                      b=((a^b)&b)<<1;// carry without addition
>
>           } while(b);//when carry equal to 0 a contains the sum
>
>           return(a);
> }
>
> Explanation:
>
   We can add 2 no.s in the following way:
    let the numbers be a=243, b=798
   now add the no.s without considering the carry in each bit position, i.e.
243+798=931
  now considering the carry for each bit position you get 1110
 now add 931+0110=1041

now add 243 and 798 using '+' operator u get same answer as above 1041


so the algorithm is
start a loop
1. xor the binary no.s  to find the sum without carry, because in this case
bit[k] =0 in the result only if bit[k[] in both a and b are 0  and  bit[k]=1
in the result only if bit[k] in a and b are different.
so this is clearly XOR operation

2. if we want carry only , then bit[k]=1 in result only if bit[k-1]=1 in
both a and b => (1 &1)<<1=10 , so this operation is AND followed by LEFT
SHIFT.

3. finally the loop ends when b=0 => iterate  until  nothing to carry



-- 
Thanks & Regards,
Priyanka Chatterjee
Final Year Undergraduate Student,
Computer Science & Engineering,
National Institute Of Technology,Durgapur
India
http://priyanka-nit.blogspot.com/

-- 
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?hl=en.

Reply via email to