/** * 不用+法做加法 * @author liyiwen1 * @date 2016/12/21 */ public class BitAdd { public static void main(String[] args) { System.out.println(add(1,2)); System.out.println(add(1,3)); } public static int add(int a, int b){ int c = 0; int d = 0; do { c = a ^ b; d = (a & b) << 1; a = c; b = d; } while (d != 0); return c; } }

本文介绍了一种不使用加号实现两个整数相加的方法。通过位运算中的异或和与操作,结合移位操作实现了加法的功能。该方法不仅展示了位运算的魅力,而且对于理解计算机底层算术运算原理具有重要意义。

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



