how to xor binary with python how to xor binary with python python python

how to xor binary with python


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


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


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.