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
function count ( n ) { let c = 0 ; while ( n != 0 ) { c ++ ; n = n >> 1 ; } return c ; } function XOR ( a , b ) { let c = Math . min ( a , b ) ; let d = Math . max ( a , b ) ; if ( count ( c ) < count ( d ) ) c = c << ( count ( d ) - count ( c ) ) ; return ( c ^ d ) ; } let a = 13 , b = 5 ; document . write ( XOR ( a , b ) ) ;