371.java realize binary addition

sum-of-two-integers

Description Title
without using operators + and - calculates two integers a, b sum.

Example 1:

Input: a = 1, b = 2
Output: 3

Example 2:

Input: a = -2, b = 3
Output: 1

Code

public class Solution {
	public int getSum(int a,int b){
		int sum,carry;
		sum = a^b;
		carry = (a&b)<<1;//专门记录进位
		if(carry!=0){
			return getSum(sum,carry);
		}
		return sum;
	}
}

Performance
Performance

Published 75 original articles · won praise 0 · Views 1513

Guess you like

Origin blog.csdn.net/qq_34087914/article/details/104095888