Calcular Bitwise OR de dos enteros a partir de sus valores Bitwise AND y Bitwise XOR

Dados dos enteros X e Y , que representan Bitwise XOR y Bitwise AND de dos enteros positivos, la tarea es calcular el valor Bitwise OR de esos dos enteros positivos.

Ejemplos:

Entrada: X = 5, Y = 2 
Salida:
Explicación: 
Si A y B son dos enteros positivos tales que A ^ B = 5, A & B = 2, entonces el valor posible de A y B es 3 y 6 respectivamente. 
Por lo tanto, (A | B) = (3 | 6) = 7.

Entrada: X = 14, Y = 1 
Salida: 15 
Explicación: 
Si A y B son dos enteros positivos tales que A ^ B = 14, A & B = 1, entonces el valor posible de A y B es 7 y 9 respectivamente. 
Por lo tanto, (A | B) = (7 | 9) = 15.

Enfoque ingenuo: el enfoque más simple para resolver este problema es iterar hasta el máximo de X e Y , digamos N , y generar todos los pares posibles de los primeros N números naturales. Para cada par, verifique si Bitwise XOR y Bitwise AND del par son X e Y , respectivamente, o no. Si se encuentra que es cierto, imprima el Bitwise OR de ese par.

Complejidad de tiempo: O(N 2 ), donde N = max(X, Y)  
Espacio auxiliar: O(1)

Enfoque eficiente: Para optimizar el enfoque anterior, la idea se basa en las siguientes observaciones:

(A ^ B) = (A | B) – (A y B) 
=> (A | B) = (A ^ B) + (A y B) = X + Y 
 

A continuación se muestra la implementación del enfoque anterior:

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

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

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>
Producción: 

7

 

Tiempo Complejidad: O(1)  
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por reapedjuggler y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *