📌  相关文章
📜  两个二进制字符串的异或

📅  最后修改于: 2021-09-06 06:23:49             🧑  作者: Mango

给定两个相等长度的二进制字符串AB ,任务是打印一个字符串,它是二进制字符串AB的异或。
例子:

方法:我们的想法是遍历由字符字符串的字符都和如果字符不匹配然后加“1”在回答字符串的字符,否则加“0”的答案字符串生成XOR字符串。

下面是上述方法的实现:

C++
// C++ Implementation to find the
// XOR of the two Binary Strings
#include
using namespace std;
 
// Function to find the
// XOR of the two Binary Strings
string xoring(string a, string b, int n){
string ans = "";
     
    // Loop to iterate over the
    // Binary Strings
    for (int i = 0; i < n; i++)
    {
        // If the Character matches
        if (a[i] == b[i])
            ans += "0";
        else
            ans += "1";
    }
    return ans;
}
 
// Driver Code
int main()
{
    string a = "1010";
    string b = "1101";
    int n = a.length();
    string c = xoring(a, b, n);
    cout << c << endl;
}
 
// This code is contributed by Surendra_Gangwar


Java
// Java Implementation to find the
// XOR of the two Binary Strings
import java.io.*;
 
class GFG {
    // Function to find the
    // XOR of the two Binary Strings
    static String  xoring(String a, String b, int n){
    String ans = "";
         
        // Loop to iterate over the
        // Binary Strings
        for (int i = 0; i < n; i++)
        {
            // If the Character matches
            if (a.charAt(i) == b.charAt(i))
                ans += "0";
            else
                ans += "1";
        }
        return ans;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        String a = "1010";
        String b = "1101";
        int n = a.length();
        String c = xoring(a, b, n);
        System.out.println(c);
    }
}
 
// This code is contributed by shubhamsingh10


Python3
# Python Implementation to find the
# XOR of the two Binary Strings
 
# Function to find the
# XOR of the two Binary Strings
def xor(a, b, n):
    ans = ""
     
    # Loop to iterate over the
    # Binary Strings
    for i in range(n):
         
        # If the Character matches
        if (a[i] == b[i]):
            ans += "0"
        else:
            ans += "1"
    return ans
 
# Driver Code
if __name__ == "__main__":
    a = "1010"
    b = "1101"
    n = len(a)
    c = xor(a, b, n)
    print(c)


C#
// C# Implementation to find the
// XOR of the two Binary Strings
using System;
 
class GFG{
    // Function to find the
    // XOR of the two Binary Strings
    static string xoring(string a, string b, int n){
    string ans = "";
         
        // Loop to iterate over the
        // Binary Strings
        for (int i = 0; i < n; i++)
        {
            // If the Character matches
            if (a[i] == b[i])
                ans += "0";
            else
                ans += "1";
        }
        return ans;
    }
     
    // Driver Code
    static public void Main ()
    {
        string a = "1010";
        string b = "1101";
        int n = a.Length;
        string c = xoring(a, b, n);
        Console.WriteLine(c);
    }
}
 
// This code is contributed by shubhamsingh10


Javascript


输出:

0111