每日一题 为了工作 2020 056 第六十四题

package com.swust.bit.example63.example64;
/**
 * 【问题】
 *       只用位运算完成整数的加法运算
 * 【分析】
 *       用位运算实现加法运算。如果在不考虑进位的情况下,a^b就是正确结果,
 * 因为0加0为0(0&0),0加1为1(0&1),1加0为1(1&0),1加1为0(1&1)。
 *
 * @author 雪瞳
 * @Slogan 时钟尚且前行,人怎能就此止步!
 * @Function
 *
 */
public class BitAdd {
    public static int add(int a,int b){
        int sum = a;
        while (b!=0){
            sum = a^b;
            b = (a&b)<<1;
            a = sum;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.err.println(add(3,6));
    }
}

  

原文地址:https://www.cnblogs.com/walxt/p/12835589.html