Xor of two binary numbers in python

I'm trying to xor 2 binaries using python like this but my output is not in binary any help?

a = "11011111101100110110011001011101000"
b = "11001011101100111000011100001100001"
y = int[a] ^ int[b]
print y

karthikr

93.9k25 gold badges191 silver badges186 bronze badges

asked Oct 16, 2013 at 21:19

1

a="11011111101100110110011001011101000"
b="11001011101100111000011100001100001"
y=int[a,2] ^ int[b,2]
print['{0:b}'.format[y]]

answered Oct 16, 2013 at 21:20

RobᵩRobᵩ

157k17 gold badges224 silver badges300 bronze badges

4

To get the Xor'd binary to the same length, as per the OP's request, do the following:

a = "11011111101100110110011001011101000"
b = "11001011101100111000011100001100001"
y = int[a, 2]^int[b,2]
print bin[y][2:].zfill[len[a]]

[output: 00010100000000001110000101010001001]

Convert the binary strings to an integer base 2, then XOR, then bin[] and then skip the first two characters, 0b, hence the bin[y0][2:].
After that, just zfill to the length - len[a], for this case.

Cheers

Matan Itzhak

2,2102 gold badges18 silver badges33 bronze badges

answered Mar 19, 2017 at 16:21

BigHBigH

3224 silver badges5 bronze badges

Since you are trying to carryout XOR on the same length binaries, the following should work just fine:

c=[str[int[a[i]]^int[b[i]]] for i in range[len[a]]]
c=''.join[c]

You can avoid the formatting altogether.

answered Nov 27, 2020 at 17:26

Since you are starting with strings and want a string result, you may find this interesting but it only works if they are the same length.

y = ''.join['0' if i == j else '1' for i, j in zip[a,b]]

If they might be different lengths you can do:

y = ''.join['0' if i == j else '1' for i, j in zip[a[::-1],b[::-1]][::-1]]
y = a[len[y]:] + b[len[y]:] + y

answered Oct 16, 2013 at 23:29

dansalmodansalmo

11.1k5 gold badges55 silver badges50 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two binary strings A and B of equal lengths, the task is to print a string which is the XOR of Binary Strings A and B.
    Examples: 
     

    Input: A = “0001”, B = “0010” 
    Output: 0011
    Input: A = “1010”, B = “0101” 
    Output: 1111 
     

    Approach: The idea is to iterate over both the string character by character and if the character mismatched then add “1” as the character in the answer string otherwise add “0” to the answer string to generate the XOR string.
     

    Below is the implementation of the above approach:
     

    C++

    #include

    using namespace std;

    string xoring[string a, string b, int n]{

    string ans = "";

        for [int i = 0; i < n; i++]

        {

            if [a[i] == b[i]]

                ans += "0";

            else

                ans += "1";

        }

        return ans;

    }

    int main[]

    {

        string a = "1010";

        string b = "1101";

        int n = a.length[];

        string c = xoring[a, b, n];

        cout

    Chủ Đề