> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/hard/add-without-plus.md).

# Add Without Plus

**17.1 Add Without Plus:** Write a function that adds two numbers. You should not use + or any arithmetic operators.

* Implemented a full adder from scratch (min SOP through K-maps)

```cpp
// initially I pushed back the bits into a vector
// this isnt the entire solution to the problem
// I've left the sum vector for learning purposes when I look back
// full adder
vector<int> sum(int a, int b) {
    int c_in = 0;
    vector<int> c_out;
    vector<int> sum;
    int s;
    int temp;

    int count = 0;
    c_out.push_back(c_in);

    int bit_a, bit_b;
    while (b != 0 || a != 0) {
        // look only at first bit
        bit_a = a & 1;
        bit_b = b & 1;

        // add bits using min SOP 
        /*
        sum.push_back((!bit_a && bit_b && !c_out.at(count)) ||
        (!bit_a && !bit_b && c_out.at(count)) ||
        (bit_a && !bit_b && !c_out.at(count)) ||
        (bit_a && bit_b && c_out.at(count)));
        */
        // equivalent
        sum.push_back(bit_a ^ bit_b ^ c_out.at(count));

        // find next c_out
        c_out.push_back((bit_b && c_out.at(count)) || (bit_a && bit_b) || (bit_a && c_out.at(count)));
        count++;

        // look at next bit
        a = a >> 1; b = b >> 1;
    }

    // add final carry
    sum.push_back(c_out.at(count));
    return sum;
}

// clean version
int add(int a, int b) {
    int c_in, c_out, s, final_s, base;
    c_in = c_out = final_s = base = 0;

    // s = current bit sum
    // final_s = current bit sum + previous sum

    int bit_a, bit_b;
    while (b != 0 || a != 0) {
        // look only at first bit
        bit_a = a & 1;
        bit_b = b & 1;

        // sum bits
        s = bit_a ^ bit_b ^ c_out;

        // find next c_out
        c_out = (bit_b && c_out) || (bit_a && bit_b) || (bit_a && c_out);

        // concatinate current bit to prev bit to form an actual number with significant base
        final_s = (s << base++) | final_s;

        // look at next bit
        a = a >> 1; b = b >> 1;
    }

    // add final carry
    final_s = (c_out << base++) | final_s;

    return final_s;
}

int main()
{
    //vector<int> s = sum(10, 10);
    //for (int i = s.size() - 1; i >= 0; i--) cout << s.at(i);
    //cout << endl;

    //s = sum(5, 10);
    //for (int i = s.size() - 1; i >= 0; i--) cout << s.at(i);
    //cout << endl;

    //s = sum(10, 7);
    //for (int i = s.size() - 1; i >= 0; i--) cout << s.at(i);
    //cout << endl;

    cout << add(10, 10) << endl;
    cout << add(7, 10) << endl;
    cout << add(20, 10) << endl;
    cout << add(45, 3) << endl;
}
```


---

# 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://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/hard/add-without-plus.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.
