Add binary numbers and output binary numbers (no. of digits> 10^4)

IMPLEMENTED WITH HELP OF LOOK AHEAD CARRY ADDER
Input: a = "11", b = "1"
Output: "100"
Input: a = "1010", b = "1011"
Output: "10101"

 string addBinary(string astring b)

{
    int carry = 0sum = 0ijxy;
    string s = "";
    i = a.size() - 1;
    j = b.size() - 1;
    while (i >= 0 && j >= 0)
    {
        sum = ((a[i] - '0') ^ (b[j] - '0')) ^ (carry);
        s = (char)(sum + '0') + s;

        x = (a[i] - '0') ^ (b[j] - '0');
        x = x && carry;
        y = (a[i] - '0') && (b[j] - '0');
        carry = x || y;
        
        i -= 1;
        j -= 1;
    }
    while (i >= 0)
    {
        sum = (a[i] - '0') ^ (carry);

        s = (char)(sum + '0') + s;

        carry = carry && (a[i] - '0');

        i -= 1;
    }
    while (j >= 0)
    {
        sum = (b[j] - '0') ^ (carry);

        s = (char)(sum + '0') + s;

        carry = carry && (b[j] - '0');

        j -= 1;
    }
    if (carry == 1)
        s = (char)(carry + '0') + s;
    return s;
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree