Find two numbers from their sum and XOR | Set 2 | Function to find the value of A and B whose sum is X and xor is Y ; Initialize the two numbers ; Case 1 : X < Y ; Case 2 : X - Y is odd ; Case 3 : If both Sum and XOR are equal ; Case 4 : If above cases fails ; Update the value of A ; Check if A & Y value is 0 ; If true , update B ; Otherwise assign - 1 to A , - 1 to B ; Print the numbers A and B ; Given Sum and XOR of 2 numbers ; Function Call
function findNums ( X , Y ) { let A , B ; if ( X < Y ) { A = - 1 ; B = - 1 ; } else if ( Math . abs ( X - Y ) & 1 ) { A = - 1 ; B = - 1 ; } else if ( X == Y ) { A = 0 ; B = Y ; } else { A = ( X - Y ) / 2 ; if ( ( A & Y ) == 0 ) { B = ( A + Y ) ; } else { A = - 1 ; B = - 1 ; } } document . write ( A + " " + B ) ; } let X = 17 , Y = 13 ; findNums ( X , Y ) ;