Cattle off network - to prove safety office- not do addition and subtraction, multiplication and division

Title : Write a function, and the sum of two integers, the function may not be used in vivo requires +, -, *, / four arithmetic symbols.
Ideas : bit computing.
XOR can be calculated without considering the carry bit of the adder, the carry may be calculated with the calculation, and finally do the XOR operation result, can answer.

class Solution {
public:
	int Add(int num1, int num2)
	{
		int sum, carry;
		do{
			sum = num1^num2;
			carry = (num1&num2) << 1;
			num1 = sum;
			num2 = carry;
		} while (num2 != 0);
		return num1;
			
	}
};

Guess you like

Origin blog.csdn.net/qq_43387999/article/details/91369343