Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values

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 = 2Â
Output: 7Â
Explanation:Â
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 = 1Â
Output: 15Â
Explanation:Â
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.
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 valuesint findBitwiseORGivenXORAND(int X, int Y){    int range = X + Y;    // Find the max range    int ans = 0;    // Traversing all the number from 0 to rangr    for (int i = 1; i <= range; i++) {        for (int j = 1; j <= range; j++) {            // If X and Y satisfie            if ((i ^ j) == X && (i & j) == Y) {                ans = (i | j);                // Calculate the OR                break;            }        }    }    return ans;}Â
// Driver Codeint main(){Â Â Â Â int X = 5, Y = 2;Â Â Â Â cout << findBitwiseORGivenXORAND(X, Y);} |
Java
// Java program to implement the above approachimport java.util.*;Â
public class GFG {Â
    // Function to calculate Bitwise OR from given    // bitwise XOR and bitwise AND values    static int findBitwiseORGivenXORAND(int X, int Y)    {        int range = X + Y;        // Find the max range        int ans = 0;        // Traversing all the numbers from 0 to range        for (int i = 1; i <= range; i++) {            for (int j = 1; j <= range; j++) {                // If X and Y satisfy the conditions                if ((i ^ j) == X && (i & j) == Y) {                    ans = (i | j);                    // Calculate the OR                    break;                }            }        }        return ans;    }Â
    // Driver Code    public static void main(String[] args)    {        int X = 5, Y = 2;        System.out.println(findBitwiseORGivenXORAND(X, Y));    }}Â
// This code is contributed by Susobhan Akhuli |
Python3
# Python program to implement the above approach# Function to calculate Bitwise OR from given# bitwise XOR and bitwise AND valuesdef findBitwiseORGivenXORAND(X, Y):    range_val = X + Y    # Find the max range    ans = 0    # Traversing all the numbers from 0 to range_val    for i in range(1, range_val + 1):        for j in range(1, range_val + 1):            # If X and Y satisfy the conditions            if (i ^ j) == X and (i & j) == Y:                ans = (i | j)                # Calculate the OR                break    return ansÂ
# Driver Codedef main():Â Â Â Â X = 5Â Â Â Â Y = 2Â Â Â Â print(findBitwiseORGivenXORAND(X, Y))Â
if __name__ == "__main__":Â Â Â Â main()Â
# This code is contributed by Susobhan Akhuli |
C#
// C# program to implement the above approachusing System;Â
public class GFG {    // Function to calculate Bitwise OR from given    // bitwise XOR and bitwise AND values    static int FindBitwiseORGivenXORAND(int X, int Y)    {        int range = X + Y;        int ans = 0;Â
        // Traversing all the numbers from 0 to range        for (int i = 1; i <= range; i++) {            for (int j = 1; j <= range; j++) {                // If X and Y satisfy the conditions                if ((i ^ j) == X && (i & j) == Y) {                    ans = (i | j); // Calculate the OR                    break;                }            }        }        return ans;    }Â
    static void Main()    {        int X = 5, Y = 2;        Console.WriteLine(FindBitwiseORGivenXORAND(X, Y));    }}Â
// This code is contributed by Susobhan Akhuli |
Javascript
// Javascript program to implement the above approachÂ
// Function to calculate Bitwise OR from given// bitwise XOR and bitwise AND valuesfunction findBitwiseORGivenXORAND(X, Y) {    let range = X + Y;    // Find the max range    let ans = 0;    // Traversing all the number from 0 to range    for (let i = 1; i <= range; i++) {        for (let j = 1; j <= range; j++) {            // If X and Y satisfy            if ((i ^ j) === X && (i & j) === Y) {                ans = (i | j);                // Calculate the OR                break;            }        }    }    return ans;}Â
// Driver Codelet X = 5, Y = 2;console.log(findBitwiseORGivenXORAND(X, Y)); |
7
Time Complexity: O(N2), where N = (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 valuesint findBitwiseORGivenXORAND(int X, int Y) { return X + Y; }Â
// Driver Codeint main(){Â Â Â Â int X = 5, Y = 2;Â Â Â Â cout << findBitwiseORGivenXORAND(X, Y);} |
C
// C program to implement// the above approach#include <stdio.h>Â
// Function to calculate Bitwise OR from given// bitwise XOR and bitwise AND valuesint findBitwiseORGivenXORAND(int X, int Y) { Â Â return X + Y; }Â
// Driver Codeint main(){Â Â Â Â int X = 5, Y = 2;Â Â Â Â printf("%d\n", findBitwiseORGivenXORAND(X, Y));}Â
// This code is contributed by phalashi. |
Java
// Java program to implement// the above approachclass 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 |
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 Codeif __name__ == "__main__":Â
    X = 5    Y = 2Â
    print(findBitwiseORGivenXORAND(X, Y))Â
# This code is contributed by AnkitRai01 |
C#
// C# program to implement// the above approachusing 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 |
Javascript
<script>// JavaScript program to implement// the above approachÂ
// Function to calculate Bitwise OR from given// bitwise XOR and bitwise AND valuesfunction findBitwiseORGivenXORAND(X, Y){Â Â Â Â return X + Y;}Â
// Driver CodeÂ
    let X = 5, Y = 2;    document.write(findBitwiseORGivenXORAND(X, Y));Â
// This code is contributed by Surbhi Tyagi.Â
</script> |
7
Time Complexity: O(1)Â
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



