Bitwise XOR of a Binary array | Function to return the bitwise XOR of all the binary strings ; Get max size and reverse each string Since we have to perform XOR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform XOR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code
import sys NEW_LINE def strBitwiseXOR ( arr , n ) : NEW_LINE INDENT result = " " NEW_LINE max_len = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_len = max ( max_len , len ( arr [ i ] ) ) NEW_LINE arr [ i ] = arr [ i ] [ : : - 1 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT s = " " NEW_LINE for j in range ( max_len - len ( arr [ i ] ) ) : NEW_LINE INDENT s += "0" NEW_LINE DEDENT arr [ i ] = arr [ i ] + s NEW_LINE DEDENT for i in range ( max_len ) : NEW_LINE INDENT pres_bit = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT pres_bit = pres_bit ^ ( ord ( arr [ j ] [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT result += chr ( ( pres_bit ) + ord ( '0' ) ) NEW_LINE DEDENT result = result [ : : - 1 ] NEW_LINE print ( result ) NEW_LINE DEDENT if ( __name__ == " _ _ main _ _ " ) : NEW_LINE INDENT arr = [ "1000" , "10001" , "0011" ] NEW_LINE n = len ( arr ) NEW_LINE strBitwiseXOR ( arr , n ) NEW_LINE DEDENT