# 不用加减乘除做加法

## 剑指 Offer 65. 不用加减乘除做加法

### 题目

```
写一个函数，求两个整数之和，要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号。



示例:

输入: a = 1, b = 1
输出: 2


提示：

a, b 均可能是负数或 0
结果不会溢出 32 位整数
```

### 题解

#### 位运算

```javascript
/**
 * @param {number} a
 * @param {number} b
 * @return {number}
 */
var add = function(a, b) {
    // sum = a + b
    //     = n + c
    // n为无进位和： a ^ b
    // c为进位和：   (a & b)<<1
    // 直至c为0时 sum = n   
    let n = a^b,
        c = (a&b)<<1;
    while(c){
        const _n = n,_c = c;
        n ^= c;
        c = (_n & _c)<<1;
    }
    return n
};
```

```
1. ^ 不进位的加法
2. & 判断进位点
3. << 1 进位
function Add(num1, num2)
{
    return num2 ? Add(num1 ^ num2,(num1 & num2) << 1) : num1;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://voyz.gitbook.io/voyz-algorithm/wei-yun-suan/bu-yong-jia-jian-cheng-chu-zuo-jia-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
