import java.util.*;
public class UnusualAdd {
public static int addAB(int A, int B) {
// write code here
int m=A^B;
int n=(A&B)<<1;
int sum=m+n;
return sum;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int s=addAB(a,b);
System.out.println(s);
}
}
需要用到位运算
本文介绍了一种使用位运算实现两个整数相加的方法。通过异或运算获取不进位的加法结果,与运算结合左移运算获取进位部分,然后将两者相加直至没有进位为止。此方法避免了直接使用加号运算符,展示了位操作的巧妙应用。

被折叠的 条评论
为什么被折叠?



