📜  添加两位字符串

📅  最后修改于: 2021-05-25 09:16:58             🧑  作者: Mango

给定两个位序列作为字符串,编写一个函数以返回两个序列的加法。位字符串也可以具有不同的长度。例如,如果字符串1为“ 1100011”,第二个字符串2为“ 10”,则该函数应返回“ 1100101”。

我们强烈建议您单击此处并进行实践,然后再继续解决方案。

由于两个字符串的大小可能不同,因此我们首先通过添加前导0来使较小的字符串的大小等于较大的字符串的大小。在使大小相同之后,我们从最右边的位到最左边的位一一加法。在每次迭代中,我们需要对3位求和:2个给定字符串的2位并进位。如果全部设置了3个位或设置了其中3个位,则总和为1。因此,我们可以对所有位进行XOR运算以找到总和位。如何找到进位–如果两位中的任意一位被置位,进位将为1。因此,我们可以通过对所有对进行“或”运算来找到进位。以下是逐步算法。

1.在较小的字符串开头加上0,以使其大小相等。
2.执行位加法
…..用于将3位a,b,c相加的布尔表达式
…..Sum = a XOR b XOR c
…..Carry =(a AND b)或(b AND c)OR(c AND a)

以下是上述算法的实现。

C++
#include 
using namespace std;
  
//adds the two-bit strings and return the result
string addBitStrings( string first, string second );
  
// Helper method: given two unequal sized bit strings, converts them to
// same length by adding leading 0s in the smaller string. Returns the
// the new length
int makeEqualLength(string &str1, string &str2)
{
    int len1 = str1.size();
    int len2 = str2.size();
    if (len1 < len2)
    {
        for (int i = 0 ; i < len2 - len1 ; i++)
            str1 = '0' + str1;
        return len2;
    }
    else if (len1 > len2)
    {
        for (int i = 0 ; i < len1 - len2 ; i++)
            str2 = '0' + str2;
    }
    return len1; // If len1 >= len2
}
  
// The main function that adds two-bit sequences and returns the addition
string addBitStrings( string first, string second )
{
    string result;  // To store the sum bits
  
    // make the lengths same before adding
    int length = makeEqualLength(first, second);
  
    int carry = 0;  // Initialize carry
  
    // Add all bits one by one
    for (int i = length-1 ; i >= 0 ; i--)
    {
        int firstBit = first.at(i) - '0';
        int secondBit = second.at(i) - '0';
  
        // boolean expression for sum of 3 bits
        int sum = (firstBit ^ secondBit ^ carry)+'0';
  
        result = (char)sum + result;
  
        // boolean expression for 3-bit addition
        carry = (firstBit & secondBit) | (secondBit & carry) | (firstBit & carry);
    }
  
    // if overflow, then add a leading 1
    if (carry)
        result = '1' + result;
  
    return result;
}
  
// Driver program to test above functions
int main()
{
    string str1 = "1100011";
    string str2 = "10";
  
    cout << "Sum is " << addBitStrings(str1, str2);
    return 0;
}


Java
// Java implementation of above algorithm
class GFG
{
  
    // Helper method: given two unequal sized bit strings, 
    // converts them to same length by adding leading 0s 
    // in the smaller string. Returns the the new length
    // Using StringBuilder as Java only uses call by value
    static int makeEqualLength(StringBuilder str1, 
                               StringBuilder str2) 
    {
        int len1 = str1.length();
        int len2 = str2.length();
        if (len1 < len2)
        {
            for (int i = 0; i < len2 - len1; i++)
                str1.insert(0, '0');
            return len2;
        } 
        else if (len1 > len2)
        {
            for (int i = 0; i < len1 - len2; i++)
                str2.insert(0, '0');
        }
  
        return len1; // If len1 >= len2
    }
  
    // The main function that adds two-bit sequences 
    // and returns the addition
    static String addBitStrings(StringBuilder str1,
                                StringBuilder str2) 
    {
        String result = ""; // To store the sum bits
  
        // make the lengths same before adding
        int length = makeEqualLength(str1, str2);
  
        // Convert StringBuilder to Strings
        String first = str1.toString();
        String second = str2.toString();
  
        int carry = 0; // Initialize carry
  
        // Add all bits one by one
        for (int i = length - 1; i >= 0; i--)
        {
            int firstBit = first.charAt(i) - '0';
            int secondBit = second.charAt(i) - '0';
  
            // boolean expression for sum of 3 bits
            int sum = (firstBit ^ secondBit ^ carry) + '0';
  
            result = String.valueOf((char) sum) + result;
  
            // boolean expression for 3-bit addition
            carry = (firstBit & secondBit) | 
                    (secondBit & carry) | 
                    (firstBit & carry);
        }
          
        // if overflow, then add a leading 1
        if (carry == 1)
            result = "1" + result;
        return result;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        String str1 = "1100011";
        String str2 = "10";
        System.out.println("Sum is " + 
        addBitStrings(new StringBuilder(str1), 
                      new StringBuilder(str2)));
    }
}
  
// This code is contributed by Vivek Kumar Singh


Python3
# Python3 program for above approach
  
# adds the two-bit strings and return the result
  
# Helper method: given two unequal sized bit strings, 
# converts them to same length by adding leading 0s 
# in the smaller string. Returns the the new length
def makeEqualLength(str1, str2):
  
    len1 = len(str1)     # Length of string 1
    len2 = len(str2)     # length of string 2
    if len1 < len2:
        str1 = (len2 - len1) * '0' + str1
        len1 = len2
    elif len2 < len1:
        str2 = (len1 - len2) * '0' + str2
        len2 = len1
    return len1, str1, str2
  
def addBitStrings( first, second ):
    result = '' # To store the sum bits
  
    # make the lengths same before adding
    length, first, second = makeEqualLength(first, second)
  
    carry = 0 # initialize carry as 0
  
    # Add all bits one by one
    for i in range(length - 1, -1, -1):
        firstBit = int(first[i])
        secondBit = int(second[i])
  
        # boolean expression for sum of 3 bits
        sum = (firstBit ^ secondBit ^ carry) + 48
        result = chr(sum) + result
  
        # boolean expression for 3 bits addition
        carry = (firstBit & secondBit) | \
                (secondBit & carry) | \
                (firstBit & carry)
  
        # if overflow, then add a leading 1
    if carry == 1:
        result = '1' + result
    return result
  
# Driver Code
if __name__ == '__main__':
    str1 = '1100011'
    str2 = '10'
    print('Sum is', addBitStrings(str1, str2))
      
# This code is contributed by
# chaudhary_19 (Mayank Chaudhary)


C#
// C# implementation of above algorithm
using System;
using System.Text;
  
class GFG
{
  
    // Helper method: given two unequal sized 
    // bit strings, converts them to same length 
    // by adding leading 0s in the smaller string. 
    // Returns the the new length Using StringBuilder 
    // as Java only uses call by value 
    static int makeEqualLength(StringBuilder str1, 
                               StringBuilder str2)
    {
        int len1 = str1.Length;
        int len2 = str2.Length;
        if (len1 < len2)
        {
            for (int i = 0; i < len2 - len1; i++)
            {
                str1.Insert(0, '0');
            }
            return len2;
        }
        else if (len1 > len2)
        {
            for (int i = 0; i < len1 - len2; i++)
            {
                str2.Insert(0, '0');
            }
        }
  
        return len1; // If len1 >= len2
    }
  
    // The main function that adds two-bit sequences 
    // and returns the addition 
    static string addBitStrings(StringBuilder str1, 
                                StringBuilder str2)
    {
        string result = ""; // To store the sum bits
  
        // make the lengths same before adding 
        int length = makeEqualLength(str1, str2);
  
        // Convert StringBuilder to Strings 
        string first = str1.ToString();
        string second = str2.ToString();
  
        int carry = 0; // Initialize carry
  
        // Add all bits one by one 
        for (int i = length - 1; i >= 0; i--)
        {
            int firstBit = first[i] - '0';
            int secondBit = second[i] - '0';
  
            // boolean expression for sum of 3 bits 
            int sum = (firstBit ^ secondBit ^ carry) + '0';
  
            result = ((char) sum).ToString() + result;
  
            // boolean expression for 3-bit addition 
            carry = (firstBit & secondBit) | 
                       (secondBit & carry) | 
                        (firstBit & carry);
        }
  
        // if overflow, then add a leading 1 
        if (carry == 1)
        {
            result = "1" + result;
        }
        return result;
    }
  
    // Driver Code 
    public static void Main(string[] args)
    {
        string str1 = "1100011";
        string str2 = "10";
        Console.WriteLine("Sum is " + 
                addBitStrings(new StringBuilder(str1), 
                              new StringBuilder(str2)));
    }
}
  
// This code is contributed by kumar65


输出:

Sum is 1100101