Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | C ++ program for the above approach ; Function to return the count of possible pairs of A and B whose Bitwise XOR is X and Y respectively ; Stores the count of pairs ; Iterate till any bit are set ; Extract i - th bit of X and Y ; Divide X and Y by 2 ; If Xi = 1 and Yi = 2 , multiply counter by 2 ; Increase required count ; If Xi = 1 and Yi = 1 ; No answer exists ; Return the final count ; Driver Code ; Given X and Y ; Function Call
#include <iostream> NEW_LINE using namespace std ; int countOfPairs ( int x , int y ) { int counter = 1 ; while ( x y ) { int bit1 = x % 2 ; int bit2 = y % 2 ; x >>= 1 ; y >>= 1 ; if ( bit1 == 1 and bit2 == 0 ) { counter *= 2 ; continue ; } if ( bit1 & bit2 ) { counter = 0 ; break ; } } return counter ; } int main ( ) { int X = 2 , Y = 5 ; cout << countOfPairs ( X , Y ) ; return 0 ; }