# Find all possible pairs with given Bitwise OR and Bitwise XOR values

• Last Updated : 28 Jul, 2021

Given two positive integers A and B representing Bitwise XOR and Bitwise OR of two positive integers, the task is to find all possible pairs (x, y) such that x ^ y is equal to A and x | y is equal to B.

Examples:

Input: A = 5, B = 7
Output:
2 7
3 6
6 3
7 2
Explanation:
7( XOR )2 = 5 and 7( OR )2 = 7
3( XOR )6 = 5 and 3( OR )6 = 7

Input: A = 8, B = 10
Output:
2 10
10 2

Naive Approach: The simplest approach to solve the problem is to generate all possible pairs and for each pair, check if their Bitwise XOR and Bitwise OR are equal to A and B respectively.

Time Complexity: O(B2)
Auxiliary Space: O(1)

Efficient Approach: The idea is to traverse through all possible values of x and use the property of XOR that if x ^ y = A, then x ^ A = y to find all possible values of y. Follow the steps below to solve the problem:

• Iterate from 1 to B using a variable, say i, and perform the following operations:
• Initialize a variable y as i ^ A.
• Check if the value of y is greater than 0 and (i | y) is equal to B or not.
• If found to be true, then print the values of i and y.

Below is the implementation of the above approach:

## C++

 `// C++ code for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find pairs with` `// XOR equal to A and OR equal to B` `void` `findPairs(``int` `A, ``int` `B)` `{` `    ``// Iterate from 1 to B` `    ``for` `(``int` `i = 1; i <= B; i++) {` `        ``int` `y = A ^ i;`   `        ``// Check if (i OR y) is B` `        ``if` `(y > 0 and (i | y) == B) {` `            ``cout << i << ``" "` `<< y << endl;` `        ``}` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `A = 8, B = 10;`   `    ``findPairs(A, B);` `    ``return` `0;` `}`

## Java

 `// Java code for the above approach` `import` `java.util.*;`   `class` `GFG{`   `// Function to find pairs with` `// XOR equal to A and OR equal to B` `static` `void` `findPairs(``int` `A, ``int` `B)` `{` `    `  `    ``// Iterate from 1 to B` `    ``for``(``int` `i = ``1``; i <= B; i++) ` `    ``{` `        ``int` `y = A ^ i;`   `        ``// Check if (i OR y) is B` `        ``if` `(y > ``0` `&& (i | y) == B) ` `        ``{` `            ``System.out.println(i + ``" "` `+ y);` `        ``}` `    ``}` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `A = ``8``, B = ``10``;`   `    ``findPairs(A, B);` `}` `}`   `// This code is contributed by Hritik`

## Python3

 `# Python3 code for the above approach`   `# Function to find pairs with` `# XOR equal to A and OR equal to B` `def` `findPairs(A, B):` `    `  `    ``# Iterate from 1 to B` `    ``for` `i ``in` `range``(``1``, B ``+` `1``):` `        `  `        ``y ``=` `A ^ i` `        `  `        ``# Check if (i OR y) is B` `        ``if` `(y > ``0` `and` `(i | y) ``=``=` `B):` `            ``print``(i, ``" "``, y)`   `# Driver Code` `A ``=` `8` `B ``=` `10`   `findPairs(A, B)`   `# This code is contributed by amreshkumar3`

## C#

 `// C# code for the above approach` `using` `System;` `class` `GFG` `{` `    `  `    ``// Function to find pairs with` `    ``// XOR equal to A and OR equal to B` `    ``static` `void` `findPairs(``int` `A, ``int` `B)` `    ``{` `         `  `        ``// Iterate from 1 to B` `        ``for``(``int` `i = 1; i <= B; i++)` `        ``{` `            ``int` `y = A ^ i;` `     `  `            ``// Check if (i OR y) is B` `            ``if` `(y > 0 && (i | y) == B)` `            ``{` `                ``Console.WriteLine(i + ``" "` `+ y);` `            ``}` `        ``}` `    ``}`   `  ``// Driver code` `  ``static` `void` `Main ()` `  ``{` `    ``int` `A = 8, B = 10;` ` `  `    ``findPairs(A, B);` `  ``}` `}`   `// This code is contributed by suresh07.`

## Javascript

 ``

Output:

```2 10
10 2```

Time Complexity: O(B)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :