TheAnonymousAMGR's blog

By TheAnonymousAMGR, history, 9 months ago, In English

Method: Adding the digits and any carry-over value at the right end of each binary integer, we store the result in a new string. The procedure is now repeated for the following digit on the left until all of the digits in both binary numbers have been reached.

After adding all the digits, if there is any remaining value, add it to the end of the new string. The new string is then returned as the total of the two binary numbers after being inverted.

Your code here...
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a, b;
    cin >> a >> b;
    string ans;
    int carry = 0;
    int i = a.length() &mdash; 1;
    int j = b.length() &mdash; 1;

    while (i >= 0 || j >= 0 || carry)
    {
        if (i >= 0)
            carry += a[i--] - '0';
        if (j >= 0)
            carry += b[j--] - '0';
        ans += carry % 2 + '0';
        carry /= 2;
    }
    reverse(begin(ans), end(ans));
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i];
    }
}

Explain: _To hold the total and two integer variables, "carry" and "i," which represent the carry-over value and the current position in the first binary number (a), respectively, we first initialize a StringBuilder object. _In order to maintain track of the current location in the second binary number (b), we further initialize another integer variable called "j."Here is how the code initializes these variables

StringBuilder sb = new StringBuilder();
int carry = 0;
int i = a.length() &mdash; 1;
int j = b.length() &mdash; 1;

_After that, a while loop is inserted and iterates until all the digits in both binary numbers have been processed and there is no more carry-over value. _ In each iteration, we add the digits from both numbers at the current position and the carry-over value. _We also update the carry-over value based on the sum of the digits. _In each iteration, the current position in each binary number is moved one digit to the left (if there are any digits left to process) by decrementing the value of i and j. _f there is a carry-over value from the previous iteration or the addition of the two digits produces a carry-over value, we set the value of 'carry' to 1; otherwise, we set it to 0. _We also append the sum of the digits to the StringBuilder object by computing the remainder of 'carry' divided by 2 (which is either 0 or 1). _Finally, we update the value of 'carry' by dividing it by 2 (which gives either 0 or 1) so that we can carry over any remaining value to the next iteration. _After the while loop completes, we reverse the StringBuilder object and convert it to a string using the toString() method. This gives us the sum of the two binary numbers in binary format.

[Learn from YT and Leetcode]

  • Vote: I like it
  • -16
  • Vote: I do not like it

»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by TheAnonymousAMGR (previous revision, new revision, compare).