> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/math/a-+-b-problem.md).

# A + B Problem

{% embed url="<https://www.lintcode.com/problem/1/>" %}

## Solution 1 (Java)

```java
public class Solution {
    /**
     * @param a: An integer
     * @param b: An integer
     * @return: The sum of a and b 
     */
    public int aplusb(int a, int b) {
        return a + b;
    }
}
```

## Solution 2 (Java)

```java
public class Solution {
    /**
     * @param a: An integer
     * @param b: An integer
     * @return: The sum of a and b 
     */
    public int aplusb(int a, int b) {
        while (b != 0) {
            int aTemp = a ^ b;
            int bTemp = (a & b) << 1;
            a = aTemp;
            b = bTemp;
        }
        return a;
    }
}
// ADD
// 1110 = 14
// 1011 = 11
//11001 = 25

// XOR (不进位加法)
// 1110   00101   10001   a
// 1011   10100   01000   b
// 0101   10001   11001   a ^ b

// AND
// 1110   00101   10001   a
// 1011   10100   01000   b
// 1010   00100   00000   a & b

// a = 1110,  0101, 10001, 11001
// b = 1011, 10100, 01000, 00000
```

### Notes

In order to avoid using the plus sign in this problem, we can use the XOR operation (if two digits are different, the result is 1; otherwise, the result is 0. The XOR operation is essentially adding without carrying. Thus, we can first XOR the two numbers and then add the carry bits.&#x20;

{% hint style="info" %}
This means `a + b = (a ^ b) + (a & b << 1)`
{% endhint %}

Eventually, the carry bits are zeros (`b == 0`) and we end the operation. Example of tracing the code is shown above. We have 14 + 11 = 25, or in binary: 1110 + 1011 = 11011.

### Citation

{% embed url="<https://www.lintcode.com/problem/1/solution/29459>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://blog.yushunchen.com/algo/math/a-+-b-problem.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
