Find two numbers from their sum and XOR | Java program to find two numbers with given Sum and XOR such that value of first number is minimum . ; Function that takes in the sum and XOR of two numbers and generates the two numbers such that the value of X is minimized ; Traverse through all bits ; Let us leave bits as 0. ; We leave i - th bit of b as 0. ; else ( Xi == 1 && Ai == 1 ) ; Driver function
class GFG { static void compute ( long S , long X ) { long A = ( S - X ) / 2 ; int a = 0 , b = 0 ; final int LONG_FIELD_SIZE = 8 ; for ( int i = 0 ; i < 8 * LONG_FIELD_SIZE ; i ++ ) { long Xi = ( X & ( 1 << i ) ) ; long Ai = ( A & ( 1 << i ) ) ; if ( Xi == 0 && Ai == 0 ) { } else if ( Xi == 0 && Ai > 0 ) { a = ( ( 1 << i ) a ) ; b = ( ( 1 << i ) b ) ; } else if ( Xi > 0 && Ai == 0 ) { a = ( ( 1 << i ) a ) ; } { System . out . println ( " Not ▁ Possible " ) ; return ; } } System . out . println ( " a ▁ = ▁ " + a + " b = " + b);  } public static void main ( String [ ] args ) { long S = 17 , X = 13 ; compute ( S , X ) ; } }