Write a function that add two numbers A and B. You should not use + or any arithmetic operators.
Notice
There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and return it.
Clarification
Are a and b both 32-bit integers?
- Yes.
Can I use bit operation?
- Sure you can.
Example
Given a=1 and b=2 return 3
Challenge
Of course you can just return a + b to get accepted. But Can you challenge not do it like that?
解法一:
1 class Solution { 2 public: 3 /* 4 * @param a: The first integer 5 * @param b: The second integer 6 * @return: The sum of a and b 7 */ 8 int aplusb(int a, int b) { 9 while (b != 0) { 10 int aa = a ^ b; 11 int bb = (a & b) << 1; 12 a = aa; 13 b = bb; 14 } 15 16 return a; 17 } 18 };