Saturday, May 9, 2015

Add Without Add Operator

来源:cc150

原帖:http://www.hawstein.com/posts/20.1.html

题目:
Implement add without add operator ('+'). Bit operation.

代码:
 int add(int a, int b) {  
   do {  
     int n1 = a ^ b;  
     int n2 = (a & b) << 1;  
     a = n1;   
     b = n2;  
   } while (b != 0);  
   return a;  
 }  

No comments:

Post a Comment