---
# System prepended metadata

title: Untitled

---

```cpp=
string addition(string str1, string str2)
{
    if (str1.length() > str2.length())
        swap(str1, str2);

    // Take an empty string for storing result
    string str = "";

    // Calculate length of both string
    int n1 = str1.length(), n2 = str2.length();
    stringstream ss;
    int carry = 0;
    for (int i=0; i<n1; i++)
    {
        // Do school mathematics, compute sum of
        // current digits and carry
        int digit1 = stoi(str1.substr(n1-i-1,1),0,16);
        int digit2 = i < n2?stoi(str2.substr(n2-i-1,1),0,16):0;
        int sum = digit1+digit2+carry;
        str.push_back(sum%10 + '0');

        // Calculate carry for next step
        carry = sum/16;
        ss<<uppercase<<hex<<sum%16;
        str += ss.str();
        ss.str("");
    }

    return str;
}
```