不使用“+”计算两个正整数的和

public int aplusb( int a , int b ){

  if( a == 0 ) return b;

  if( b == 0 ) return a;

  /*

    两个正整数进行异或运算,得到的结果为两个正整数不进位相加

    两个正整数进行与运算,表示需要进位的位置,将结果左移1位(x2),得到进位后的数据

    使用递归,直到a或b中出现0,则跳出递归返回结果

  */

  int x = a^b;

  int y = ( a&b ) <<1;

  return aplusb( x , y );

}

猜你喜欢

转载自www.cnblogs.com/i-fly/p/10224021.html