XOR two binary strings of unequal lengths | Function to insert n 0 s in the beginning of the given string ; Function to return the XOR of the given strings ; Lengths of the given strings ; Make both the strings of equal lengths by inserting 0 s in the beginning ; Updated length ; To store the resultant XOR ; Driver code
def addZeros ( strr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT strr = "0" + strr NEW_LINE DEDENT return strr NEW_LINE DEDENT def getXOR ( a , b ) : NEW_LINE INDENT aLen = len ( a ) NEW_LINE bLen = len ( b ) NEW_LINE if ( aLen > bLen ) : NEW_LINE INDENT b = addZeros ( b , aLen - bLen ) NEW_LINE DEDENT elif ( bLen > aLen ) : NEW_LINE INDENT a = addZeros ( a , bLen - aLen ) NEW_LINE DEDENT lenn = max ( aLen , bLen ) ; NEW_LINE res = " " NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( a [ i ] == b [ i ] ) : NEW_LINE INDENT res += "0" NEW_LINE DEDENT else : NEW_LINE INDENT res += "1" NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT a = "11001" NEW_LINE b = "111111" NEW_LINE print ( getXOR ( a , b ) ) NEW_LINE