Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
Last Updated :
11 Sep, 2024
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 values
int 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 Code
int main()
{
int X = 5, Y = 2;
cout << findBitwiseORGivenXORAND(X, Y);
}
Java
// Java program to implement the above approach
import 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
Python
# Python program to implement the above approach
# Function to calculate Bitwise OR from given
# bitwise XOR and bitwise AND values
def 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 Code
def 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 approach
using 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 values
function 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 Code
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++
// 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);
}
C
// C program to implement
// the above approach
#include <stdio.h>
// 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;
printf("%d\n", findBitwiseORGivenXORAND(X, Y));
}
// This code is contributed by phalashi.
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
Python
# 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
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
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function to calculate Bitwise OR from given
// bitwise XOR and bitwise AND values
function 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>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Bit Manipulation for Competitive Programming Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. It is a valuable technique in competitive programming because it allows you to solve problems efficiently, often reducing time complexity and memory usag
15+ min read
Count set bits in an integer Write an efficient program to count the number of 1s in the binary representation of an integer.Examples : Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 13 is 1101 and has 3 set bits[Naive Approach] - One by One CountingTh
15+ min read
Count total set bits in first N Natural Numbers (all numbers from 1 to N) Given a positive integer n, the task is to count the total number of set bits in binary representation of all natural numbers from 1 to n. Examples: Input: n= 3Output: 4Explanation: Numbers from 1 to 3: {1, 2, 3}Binary Representation of 1: 01 -> Set bits = 1Binary Representation of 2: 10 -> Se
9 min read
Check whether the number has only first and last bits set Given a positive integer n. The problem is to check whether only the first and last bits are set in the binary representation of n.Examples: Input : 9 Output : Yes (9)10 = (1001)2, only the first and last bits are set. Input : 15 Output : No (15)10 = (1111)2, except first and last there are other bi
4 min read
Shortest path length between two given nodes such that adjacent nodes are at bit difference 2 Given an unweighted and undirected graph consisting of N nodes and two integers a and b. The edge between any two nodes exists only if the bit difference between them is 2, the task is to find the length of the shortest path between the nodes a and b. If a path does not exist between the nodes a and
7 min read
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, the
7 min read
Unset least significant K bits of a given number Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N. Examples: Input: N = 200, K=5Output: 192Explanation: (200)10 = (11001000)2 Unsetting least significant K(= 5) bits from the above binary representation, the new number obtained is (11000000
4 min read
Find all powers of 2 less than or equal to a given number Given a positive number N, the task is to find out all the perfect powers of two which are less than or equal to the given number N. Examples: Input: N = 63 Output: 32 16 8 4 2 1 Explanation: There are total of 6 powers of 2, which are less than or equal to the given number N. Input: N = 193 Output:
6 min read
Powers of 2 to required sum Given an integer N, task is to find the numbers which when raised to the power of 2 and added finally, gives the integer N. Example : Input : 71307 Output : 0, 1, 3, 7, 9, 10, 12, 16 Explanation : 71307 = 2^0 + 2^1 + 2^3 + 2^7 + 2^9 + 2^10 + 2^12 + 2^16 Input : 1213 Output : 0, 2, 3, 4, 5, 7, 10 Exp
10 min read
Print bitwise AND set of a number N Given a number N, print all the numbers which are a bitwise AND set of the binary representation of N. Bitwise AND set of a number N is all possible numbers x smaller than or equal N such that N & i is equal to x for some number i. Examples : Input : N = 5Output : 0, 1, 4, 5 Explanation: 0 &
8 min read