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

25 Mar, 2022
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.
Time Complexity: O(N2), 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
Java
Python3
C#
Javascript
// 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 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 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 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# 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
<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)
ankthon
ipg2016107
khushboogoyal499
surbhityagi15
phasing17
Bit Algorithms
Bitwise-AND
Bitwise-OR
Bitwise-XOR
Bit Magic
Greedy
Mathematical
Greedy
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Program to find whether a given number is power of 2
Binary representation of a given number
Set, Clear and Toggle a given bit of a number in C
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Program for array rotation
Huffman Coding | Greedy Algo-3