371. Sum of Two Integers (实现二进制全加器)

来源:互联网 发布:mac能打的网游 编辑:程序博客网 时间:2024/06/06 03:38

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:

Given a = 1 and b = 2, return 3.

public class Solution {    public int getSum(int a, int b) {      int temp=1,prev=0,tempA,tempB,tempRes,result = 0;for(int i=0;i<32;i++){tempA = a&temp;tempB = b&temp;tempRes = tempA^tempB^prev;prev = ((tempA^tempB)&prev|tempA&tempB)<<1;result |= tempRes;temp<<=1;}return result;      }}


0 0
原创粉丝点击