Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | 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 ; Given X and Y ; Function call
def countOfPairs ( x , y ) : NEW_LINE INDENT counter = 1 NEW_LINE while ( x or y ) : NEW_LINE INDENT bit1 = x % 2 NEW_LINE bit2 = y % 2 NEW_LINE x >>= 1 NEW_LINE y >>= 1 NEW_LINE if ( bit1 == 1 and bit2 == 0 ) : NEW_LINE INDENT counter *= 2 NEW_LINE continue NEW_LINE DEDENT if ( bit1 & bit2 ) : NEW_LINE INDENT counter = 0 NEW_LINE break NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT X = 2 NEW_LINE Y = 5 NEW_LINE print ( countOfPairs ( X , Y ) ) NEW_LINE