Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
Last Updated :
04 Dec, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findBitwiseORGivenXORAND( int X, int Y)
{
int range = X + Y;
int ans = 0;
for ( int i = 1; i <= range; i++) {
for ( int j = 1; j <= range; j++) {
if ((i ^ j) == X && (i & j) == Y) {
ans = (i | j);
break ;
}
}
}
return ans;
}
int main()
{
int X = 5, Y = 2;
cout << findBitwiseORGivenXORAND(X, Y);
}
|
Java
import java.util.*;
public class GFG {
static int findBitwiseORGivenXORAND( int X, int Y)
{
int range = X + Y;
int ans = 0 ;
for ( int i = 1 ; i <= range; i++) {
for ( int j = 1 ; j <= range; j++) {
if ((i ^ j) == X && (i & j) == Y) {
ans = (i | j);
break ;
}
}
}
return ans;
}
public static void main(String[] args)
{
int X = 5 , Y = 2 ;
System.out.println(findBitwiseORGivenXORAND(X, Y));
}
}
|
Python3
def findBitwiseORGivenXORAND(X, Y):
range_val = X + Y
ans = 0
for i in range ( 1 , range_val + 1 ):
for j in range ( 1 , range_val + 1 ):
if (i ^ j) = = X and (i & j) = = Y:
ans = (i | j)
break
return ans
def main():
X = 5
Y = 2
print (findBitwiseORGivenXORAND(X, Y))
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG {
static int FindBitwiseORGivenXORAND( int X, int Y)
{
int range = X + Y;
int ans = 0;
for ( int i = 1; i <= range; i++) {
for ( int j = 1; j <= range; j++) {
if ((i ^ j) == X && (i & j) == Y) {
ans = (i | j);
break ;
}
}
}
return ans;
}
static void Main()
{
int X = 5, Y = 2;
Console.WriteLine(FindBitwiseORGivenXORAND(X, Y));
}
}
|
Javascript
function findBitwiseORGivenXORAND(X, Y) {
let range = X + Y;
let ans = 0;
for (let i = 1; i <= range; i++) {
for (let j = 1; j <= range; j++) {
if ((i ^ j) === X && (i & j) === Y) {
ans = (i | j);
break ;
}
}
}
return ans;
}
let X = 5, Y = 2;
console.log(findBitwiseORGivenXORAND(X, Y));
|
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++
#include <bits/stdc++.h>
using namespace std;
int findBitwiseORGivenXORAND( int X, int Y) { return X + Y; }
int main()
{
int X = 5, Y = 2;
cout << findBitwiseORGivenXORAND(X, Y);
}
|
C
#include <stdio.h>
int findBitwiseORGivenXORAND( int X, int Y)
{
return X | Y;
}
int main()
{
int X = 5, Y = 2;
printf ( "%d\n" , findBitwiseORGivenXORAND(X, Y));
}
|
Java
class GFG {
static int findBitwiseORGivenXORAND( int X, int Y)
{
return X + Y;
}
public static void main(String[] args)
{
int X = 5 , Y = 2 ;
System.out.print(findBitwiseORGivenXORAND(X, Y));
}
}
|
Python3
def findBitwiseORGivenXORAND(X, Y):
return X + Y
if __name__ = = "__main__" :
X = 5
Y = 2
print (findBitwiseORGivenXORAND(X, Y))
|
C#
using System;
class GFG {
static int findBitwiseORGivenXORAND( int X, int Y)
{
return X + Y;
}
public static void Main( string [] args)
{
int X = 5, Y = 2;
Console.Write(findBitwiseORGivenXORAND(X, Y));
}
}
|
Javascript
<script>
function findBitwiseORGivenXORAND(X, Y)
{
return X + Y;
}
let X = 5, Y = 2;
document.write(findBitwiseORGivenXORAND(X, Y));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...