Java 兩個整型相加,不能用+號和其餘算術運算符?
用到的知識點:二進制按位異或,等值於求和。(本位)
二進制按位相與再乘2,等值於求和(進位)
突破口:當進位爲0,加完,退出。ide
public class Add { public int addAB(int A, int B) { // write code here while (B!=0) { int xor = A ^ B;//求和 本位 int and = (A & B)<<1;// A=xor; B=and;//進位爲0,加法完畢 } return A; } }