Choose X such that ( A xor X ) + ( B xor X ) is minimized | Function to return the integer X such that ( A xor X ) + ( B ^ X ) is minimized ; While either A or B is non - zero ; Position at which both A and B have a set bit ; Inserting a set bit in x ; Right shifting both numbers to traverse all the bits ; Driver code
< ? php function findX ( $ A , $ B ) { $ j = 0 ; $ x = 0 ; while ( $ A $ B ) { if ( ( $ A & 1 ) && ( $ B & 1 ) ) { $ x += ( 1 << $ j ) ; } $ A >>= 1 ; $ B >>= 1 ; $ j += 1 ; } return $ x ; } $ A = 2 ; $ B = 3 ; $ X = findX ( $ A , $ B ) ; echo " X = " ▁ , ▁ $ X ▁ , ▁ " , Sum = " ( $ A ^ $ X ) + ( $ B ^ $ X ) ; ? >