Given two integers **X** and **Y**, the task is to find the total number of ways to generate a pair of integers **A** and **B** such that Bitwise XOR and Bitwise AND between **A** and **B** is **X **and **Y** respectively

**Examples:**

Input:X = 2, Y = 5Output:2Explanation:

The two possible pairs are (5, 7) and (7, 5).Pair 1:(5, 7)

Bitwise AND = 5 & 7 = 2

Bitwise XOR = 5 ^ 7 = 5Pair 2:(7, 5)

Bitwise AND = 7 & 5 = 2

Bitwise XOR = 7 ^ 5 = 5

Input:X = 7, Y = 5Output:0

**Naive Approach:** The simplest approach to solve the problem is to choose the maximum among **X** and **Y **and set all its bits and then check all possible pairs from **0** to that maximum number, say **M**. If for any pair of **A** and **B**, **A & B** and **A⊕B **becomes equal to **X** and **Y** respectively, then increment the **count**. Print the final value of **count** after checking for all possible pairs.**Time Complexity:** O(M^{2})**Auxiliary Space:** O(1)

**Efficient Approach:** The idea is to generate all possible combinations of bits at each position. There are 4 possibilities for the **i ^{th}** bit in

**X**and

**Y**which are as follows:

- If X
_{i}= 0 and Y_{i}= 1, then A_{i}= 1 and B_{i}= 1. - If X
_{i}= 1 and Y_{i}= 1, then no possible bit assignment exist. - If X
_{i}= 0 and Y_{i}= 0 then A_{i}= 0 and B_{i}= 0. - If X
_{i}= 1 and Y_{i}= 0 then A_{i}= 0 and B_{i}= 1 or A_{i}= 1 and B_{i}= 0, where**A**,_{i}**B**,_{i}**X**, and_{i}**Y**represent_{i}**i**bit in each of them.^{th}

Follow the steps below to solve the problem:

- Initialize the counter as
**1**. - For the
**i**, if^{th}bit**X**and_{i}**Y**are equal to_{i}**1**, then print**0**. - If at
**i**,^{th}bit**X**is_{i}**1**and**Y**is_{i}**0**then multiply the counter by**2**as there are 2 options. - After that, divide
**X**and**Y**each time by**2**. - Repeat the above steps for every
**i**until both of them become^{th}bit**0**and print the value of the**counter**.

Below is the implementation of the above approach:

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

*chevron_right*

*filter_none*

`// Java program for ` `// the above approach` `import` `java.util.*;`
`class` `GFG{`
`// Function to return the count of` `// possible pairs of A and B whose` `// Bitwise XOR is X and Y respectively` `static` `int` `countOfPairs(` `int` `x, ` `int` `y)`
`{` ` ` `// Stores the count of pairs`
` ` `int` `counter = ` `1` `;`
` ` `// Iterate till any bit are set`
` ` `while` `(x > ` `0` `|| y > ` `0` `) `
` ` `{`
` ` `// Extract i-th bit`
` ` `// of X and Y`
` ` `int` `bit1 = x % ` `2` `;`
` ` `int` `bit2 = y % ` `2` `;`
` ` `// Divide X and Y by 2`
` ` `x >>= ` `1` `;`
` ` `y >>= ` `1` `;`
` ` `// If Xi = 1 and Yi = 2,`
` ` `// multiply counter by 2`
` ` `if` `(bit1 == ` `1` `&& bit2 == ` `0` `) `
` ` `{`
` ` `// Increase required count`
` ` `counter *= ` `2` `;`
` ` `continue` `;`
` ` `}`
` ` `// If Xi =1 and Yi = 1`
` ` `if` `((bit1 & bit2) > ` `0` `) `
` ` `{`
` ` `// No answer exists`
` ` `counter = ` `0` `;`
` ` `break` `;`
` ` `}`
` ` `}`
` ` `// Return the final count`
` ` `return` `counter;`
`}` `// Driver Code` `public` `static` `void` `main(String[] args)`
`{` ` ` `// Given X and Y`
` ` `int` `X = ` `2` `, Y = ` `5` `;`
` ` `// Function Call`
` ` `System.out.print(countOfPairs(X, Y));`
`}` `}` `// This code is contributed by Princi Singh` |

*chevron_right*

*filter_none*

`# Python3 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` `def` `countOfPairs(x, y):`
` ` `# Stores the count of pairs`
` ` `counter ` `=` `1`
` ` `# Iterate till any bit are set`
` ` `while` `(x ` `or` `y):`
` ` `# Extract i-th bit`
` ` `# of X and Y`
` ` `bit1 ` `=` `x ` `%` `2`
` ` `bit2 ` `=` `y ` `%` `2`
` ` `# Divide X and Y by 2`
` ` `x >>` `=` `1`
` ` `y >>` `=` `1`
` ` `# If Xi = 1 and Yi = 2,`
` ` `# multiply counter by 2`
` ` `if` `(bit1 ` `=` `=` `1` `and` `bit2 ` `=` `=` `0` `):`
` ` ` ` `# Increase required count`
` ` `counter ` `*` `=` `2`
` ` `continue`
` ` `# If Xi =1 and Yi = 1`
` ` `if` `(bit1 & bit2):`
` ` `# No answer exists`
` ` `counter ` `=` `0`
` ` `break`
` ` `# Return the final count`
` ` `return` `counter`
`# Driver Code` `# Given X and Y ` `X ` `=` `2`
`Y ` `=` `5`
`# Function call` `print` `(countOfPairs(X, Y))`
`# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

`// C# program for ` `// the above approach` `using` `System;`
`class` `GFG{`
`// Function to return the count of` `// possible pairs of A and B whose` `// Bitwise XOR is X and Y respectively` `static` `int` `countOfPairs(` `int` `x, ` `int` `y)`
`{` ` ` `// Stores the count of pairs`
` ` `int` `counter = 1;`
` ` `// Iterate till any bit are set`
` ` `while` `(x > 0 || y > 0) `
` ` `{`
` ` `// Extract i-th bit`
` ` `// of X and Y`
` ` `int` `bit1 = x % 2;`
` ` `int` `bit2 = y % 2;`
` ` `// Divide X and Y by 2`
` ` `x >>= 1;`
` ` `y >>= 1;`
` ` `// If Xi = 1 and Yi = 2,`
` ` `// multiply counter by 2`
` ` `if` `(bit1 == 1 && bit2 == 0) `
` ` `{`
` ` `// Increase required count`
` ` `counter *= 2;`
` ` `continue` `;`
` ` `}`
` ` `// If Xi =1 and Yi = 1`
` ` `if` `((bit1 & bit2) > 0) `
` ` `{`
` ` `// No answer exists`
` ` `counter = 0;`
` ` `break` `;`
` ` `}`
` ` `}`
` ` `// Return the readonly count`
` ` `return` `counter;`
`}` `// Driver Code` `public` `static` `void` `Main(String[] args)`
`{` ` ` `// Given X and Y`
` ` `int` `X = 2, Y = 5;`
` ` `// Function Call`
` ` `Console.Write(countOfPairs(X, Y));`
`}` `}` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(log M)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Count even length subarrays having bitwise XOR equal to 0
- Subsequence pair from given Array having all unique and all same elements respectively
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Highest and Smallest power of K less than and greater than equal to N respectively
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Count squares possible from M and N straight lines parallel to X and Y axis respectively
- Generate an array having Bitwise AND of the previous and the next element
- Count pairs with Bitwise XOR as ODD number
- Count pairs with Bitwise XOR as EVEN number
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Is it possible to reach N and M from 1 and 0 respectively as per given conditio
- Bitwise XOR of elements having odd frequency

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.