XOR of two numbers after making length of their binary representations equal | Function to count the number of bits in binary representation of an integer ; initialize count ; count till n is non zero ; right shift by 1 i . e , divide by 2 ; Function to calculate the xor of two numbers by adding trailing zeros to the number having less number of bits in its binary representation . ; stores the minimum and maximum ; left shift if the number of bits are less in binary representation ; Driver Code
def count ( n ) : NEW_LINE INDENT c = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT c += 1 NEW_LINE n = n >> 1 NEW_LINE DEDENT return c NEW_LINE DEDENT def XOR ( a , b ) : NEW_LINE INDENT c = min ( a , b ) NEW_LINE d = max ( a , b ) NEW_LINE if ( count ( c ) < count ( d ) ) : NEW_LINE INDENT c = c << ( count ( d ) - count ( c ) ) NEW_LINE DEDENT return ( c ^ d ) NEW_LINE DEDENT a = 13 ; b = 5 NEW_LINE print ( XOR ( a , b ) ) NEW_LINE