Choose X such that ( A xor X ) + ( B xor X ) is minimized | Java implementation of the approach ; 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
class GFG { static int findX ( int A , int B ) { int j = 0 , x = 0 ; while ( A != 0 B != 0 ) { if ( ( A % 2 == 1 ) && ( B % 2 == 1 ) ) { x += ( 1 << j ) ; } A >>= 1 ; B >>= 1 ; j += 1 ; } return x ; } public static void main ( String [ ] args ) { int A = 2 , B = 3 ; int X = findX ( A , B ) ; System . out . println ( " X ▁ = ▁ " + X + " , ▁ Sum ▁ = ▁ " + ( ( A ^ X ) + ( B ^ X ) ) ) ; } }