Home >Java >javaTutorial >How to implement summation in Java without arithmetic operators
The question says that arithmetic operators cannot be used, so we can only start with logical operators and shift operators.
We know that the ^ (XOR) operation is a carry-free sum of two numbers. If the addition of two numbers does not produce a carry, then the sum is the sum of the two numbers. This is the key to solving the problem. Generally speaking, adding two numbers will produce a carry. To get the correct sum, we should add a carry on the basis of the "sum without carry". Then we should first get the carry. We know that when the corresponding positions of the two numbers are both 1, a carry will occur. That is, under the condition that the corresponding positions A&B==1, we can get a carry for each bit, but the carry is to add this 1 to the corresponding higher digit, so we shift the obtained carry to the left by one digit, and then add it to the "sum without carry" obtained previously, to get the sum of the two numbers.
We find that we can have infinite matryoshka dolls here. To get the sum of "sum without carry" and "generated carry", then treat these two numbers as two new numbers respectively. For Its summation. The previous operation is also repeated until the two numbers cannot produce a carry, and the result can be obtained directly through the ^ operation.
Suppose we find the value of 23 16
##According to the above ideas, we can easily write the codepublic static int addAB(int A, int B) { // 当两数的产生的进位为 0,就退出循环,返回其无进位和,就是结果 while ((A & B) != 0) { int A_B = A ^ B;// 无进位求和 int AB = (A & B) << 1;// 得到进位 // 接下来就是求 得到的进位 + 无进位之和,重复前面的操作,直到不产生进位 A = A_B; B = AB; } // 最后返回新的无进位之和 return (A ^ B); }So in order to verify its correctness, we use a logarithmizer here to compare the function with the ’ ’ operator.
public static void main(String[] args) { int count = 1_0000_0000;// 比较次数 100000000次 Random random = new Random();// 生成随机数字 boolean flag = true;// 相等则为 true for (int i = 0; i < count; i++) { int num1 = random.nextInt(Integer.MAX_VALUE/2);// 随机正数1 int num1_2 = random.nextInt(Integer.MAX_VALUE/2);// 随机正数2 // 随机正数1 + 随机正数2 if ((num1+num1_2) != addAB(num1,num1_2)){ // 有一次不相等的,则退出循环 flag = false; break; } int num2 = -random.nextInt(Integer.MAX_VALUE/2);// 随机负数1 int num2_2 = -random.nextInt(Integer.MAX_VALUE/2);// 随机负数2 // 随机负数1 + 随机负数2 if ((num2+num2_2) != addAB(num2,num2_2)){ // 有一次不相等的,则退出循环 flag = false; break; } int num3 = random.nextInt(Integer.MAX_VALUE);// 随机正数 int num4 = -random.nextInt(Integer.MAX_VALUE);// 随机负数 // 随机正数 + 随机负数 if ((num3+num4) != addAB(num3,num4)){ // 有一次不相等的,则退出循环 flag = false; break; } } System.out.println(flag); }Running results: The final output result is true, which means that we calculate randomly generated positive numbers, negative numbers, and positive and negative numbers of 100000000 respectively. times, without any errors, indicating that the function is reliable.
The above is the detailed content of How to implement summation in Java without arithmetic operators. For more information, please follow other related articles on the PHP Chinese website!