How to find the sum of two numbers without using the arithmetic operator (+/-)?

I encountered such a question today. The
insert image description here
title said that arithmetic operators cannot be used, so we can only start with logical operators and shift operators .

We know that the ^ (exclusive or) operation is a carry-free summation 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, the addition of two numbers will generate a carry, and to get a 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, there will be a carry, that is, under the condition that the corresponding position A&B==1, the carry of each bit can be obtained , but the carry is to add this 1. To the corresponding higher digit, so we will shift the whole obtained carry to the left by one , and then add it to the "sum of no carry" obtained before to get the sum of the two numbers.

We found that we can do infinite nesting of dolls here. To get the sum of "sum without carry" and "carry generated", then treat these two numbers as two new numbers and sum them up. It also repeats the previous operation until the two numbers cannot generate a carry, and the result can be obtained directly through the ^ operation.

Suppose we find the value of 23 + 16
insert image description here
insert image description here
According to the above idea, we can easily write the code

public 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 the logarithm here and 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 result:
insert image description here
The final output result is true, indicating that we have calculated randomly generated positive numbers + positive numbers, negative numbers + negative numbers, and positive numbers + negative numbers 100,000,000 times respectively, and there is no error, indicating that the function is reliable.

Guess you like

Origin blog.csdn.net/qq_45792749/article/details/123851448