Find two numbers from their sum and OR | C ++ program for the above approach ; Function to find the two integers from the given sum and Bitwise OR value ; Check if Z is non negative ; Iterate through all the bits ; Find the kth bit of A & B ; Find the kth bit of A | B ; If bit1 = 1 and bit2 = 0 , then there will be no possible pairs ; Print the possible pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define MaxBit  32 NEW_LINE using namespace std ; int possiblePair ( int X , int Y ) { int Z = Y - X ; if ( Z < 0 ) { cout << " - 1" ; return 0 ; } for ( int k = 0 ; k < MaxBit ; k ++ ) { int bit1 = ( Z >> k ) & 1 ; int bit2 = ( Z >> k ) & 1 ; if ( bit1 && ! bit2 ) { cout << " - 1" ; return 0 ; } } cout << Z << ' ▁ ' << X ; return 0 ; } int main ( ) { int X = 7 , Y = 11 ; possiblePair ( X , Y ) ; return 0 ; }