Given two integers **X** and **Y**, representing **Bitwise XOR** and **Bitwise AND** of two positive integers, the task is to calculate the **Bitwise OR** value of those two positive integers.

**Examples:**

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

If A and B are two positive integers such that A ^ B = 5, A & B = 2, then the possible value of A and B is 3 and 6 respectively.

Therefore, (A | B) = (3 | 6) = 7.

Input:X = 14, Y = 1Output:15Explanation:

If A and B are two positive integers such that A ^ B = 14, A & B = 1, then the possible value of A and B is 7 and 9 respectively.

Therefore, (A | B) = (7 | 9) = 15.

**Naive Approach:** The simplest approach to solve this problem is to iterate up to the maximum of **X** and **Y**, say **N**, and generate all possible pairs of the first **N** natural numbers. For each pair, check if **Bitwise XOR** and the Bitwise AND of the pair is **X** and **Y**, respectively, or not. If found to be true, then print the **Bitwise OR** of that pair.

**Time Complexity:** O(N^{2}), where N = max(X, Y)**Auxiliary Space:** O(1)

**Efficient Approach:** To optimize the above approach, the idea is based on the following observations:

(A ^ B) = (A | B) – (A & B)

=> (A | B) = (A ^ B) + (A & B) = X + Y

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to calculate Bitwise OR from given` `// bitwise XOR and bitwise AND values` `int` `findBitwiseORGivenXORAND(` `int` `X, ` `int` `Y)` `{` ` ` `return` `X + Y;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `X = 5, Y = 2;` ` ` `cout << findBitwiseORGivenXORAND(X, Y);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// the above approach` `class` `GFG {` ` ` `// Function to calculate Bitwise OR from given` ` ` `// bitwise XOR and bitwise AND values` ` ` `static` `int` `findBitwiseORGivenXORAND(` `int` `X, ` `int` `Y)` ` ` `{` ` ` `return` `X + Y;` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `X = ` `5` `, Y = ` `2` `;` ` ` `System.out.print(findBitwiseORGivenXORAND(X, Y));` ` ` `}` `}` `// This code is contributed by AnkitRai01` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to calculate Bitwise OR from` `# given bitwise XOR and bitwise AND values` `def` `findBitwiseORGivenXORAND(X, Y):` ` ` `return` `X ` `+` `Y` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `X ` `=` `5` ` ` `Y ` `=` `2` ` ` ` ` `print` `(findBitwiseORGivenXORAND(X, Y))` `# This code is contributed by AnkitRai01` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to calculate Bitwise OR from given` `// bitwise XOR and bitwise AND values` `static` `int` `findBitwiseORGivenXORAND(` `int` `X, ` `int` `Y)` `{` ` ` `return` `X + Y;` `}` `// Driver Code` `public` `static` `void` `Main(` `string` `[]args)` `{` ` ` `int` `X = 5, Y = 2;` ` ` ` ` `Console.Write(findBitwiseORGivenXORAND(X, Y));` `}` `}` `// This code is contributed by ipg2016107` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** O(1)**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.