📜  在不使用XOR运算符的情况下找到两个数字的XOR

📅  最后修改于: 2021-05-07 00:48:08             🧑  作者: Mango

给定两个整数,无需使用XOR运算符即可找到它们的XOR,即在C / C++中不使用^。

例子 :

Input:  x = 1, y = 2
Output: 3

Input:  x = 3, y = 5
Output: 6

一个简单的解决方案是一次遍历所有位。对于每对位,检查两者是否相同,在输出中将相应的位设置为0,否则设置为1。

C++
// C++ program to find XOR without using ^
#include 
using namespace std;
 
// Returns XOR of x and y
int myXOR(int x, int y)
{
    int res = 0; // Initialize result
     
    // Assuming 32-bit Integer
    for (int i = 31; i >= 0; i--)                    
    {
       // Find current bits in x and y
       bool b1 = x & (1 << i);
       bool b2 = y & (1 << i);
        
        // If both are 1 then 0 else xor is same as OR
        bool xoredBit = (b1 & b2) ? 0 : (b1 | b2);         
 
        // Update result
        res <<= 1;
        res |= xoredBit;
    }
    return res;
}
 
// Driver program to test above function
int main()
{
   int x = 3, y = 5;
   cout << "XOR is " << myXOR(x, y);
   return 0;
}


Java
// Java program to find XOR without using ^
import java.io.*;
 
class GFG{
   
// Returns XOR of x and y
static int myXOR(int x, int y)
{
     
    // Initialize result
    int res = 0;
 
    // Assuming 32-bit Integer
    for(int i = 31; i >= 0; i--)                    
    {
         
        // Find current bits in x and y
        int b1 = ((x & (1 << i)) == 0 ) ? 0 : 1; 
        int b2 = ((y & (1 << i)) == 0 ) ? 0 : 1; 
 
        // If both are 1 then 0 else xor is same as OR
        int xoredBit = ((b1 & b2) != 0) ? 0 : (b1 | b2);         
 
        // Update result
        res <<= 1;
        res |= xoredBit;
    }
    return res;
}
 
// Driver Code
public static void main (String[] args)
{
    int x = 3, y = 5;
     
    System.out.println("XOR is " + myXOR(x, y));
}
}
 
// This code is contributed by math_lover


Python3
# Python3 program to find XOR without using ^
 
# Returns XOR of x and y
def myXOR(x, y):
    res = 0 # Initialize result
 
    # Assuming 32-bit Integer
    for i in range(31, -1, -1):
         
        # Find current bits in x and y
        b1 = x & (1 << i)
        b2 = y & (1 << i)
        b1 = min(b1, 1)
        b2 = min(b2, 1)
 
        # If both are 1 then 0
        # else xor is same as OR
        xoredBit = 0
        if (b1 & b2):
            xoredBit = 0
        else:
            xoredBit = (b1 | b2)
 
        # Update result
        res <<= 1;
        res |= xoredBit
    return res
 
# Driver Code
x = 3
y = 5
print("XOR is", myXOR(x, y))
 
# This code is contributed by Mohit Kumar


C#
// C# program to find XOR
// without using ^
using System;
class GFG{
   
// Returns XOR of x and y
static int myXOR(int x,
                 int y)
{   
  // Initialize result
  int res = 0;
 
  // Assuming 32-bit int
  for(int i = 31; i >= 0; i--)                    
  {
    // Find current bits in x and y
    int b1 = ((x & (1 << i)) == 0 ) ?
               0 : 1; 
    int b2 = ((y & (1 << i)) == 0 ) ?
               0 : 1; 
 
    // If both are 1 then 0 else
    // xor is same as OR
    int xoredBit = ((b1 & b2) != 0) ?
                     0 : (b1 | b2);         
 
    // Update result
    res <<= 1;
    res |= xoredBit;
  }
  return res;
}
 
// Driver Code
public static void Main(String[] args)
{
  int x = 3, y = 5;
  Console.WriteLine("XOR is " +
                     myXOR(x, y));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program to find XOR without using ^
#include 
using namespace std;
 
// Returns XOR of x and y
int myXOR(int x, int y)
{
   return (x | y) & (~x | ~y);
}
 
// Driver program to test above function
int main()
{
   int x = 3, y = 5;
   cout << "XOR is " << myXOR(x, y);
   return 0;
}


Java
// Java program to find
// XOR without using ^
import java.io.*;
 
class GFG
{
 
// Returns XOR of x and y
static int myXOR(int x, int y)
{
    return (x | y) &
           (~x | ~y);
}
 
// Driver Code
public static void main (String[] args)
{
    int x = 3, y = 5;
    System.out.println("XOR is "+
                      (myXOR(x, y)));
}
}
 
// This code is contibuted by ajit


Python3
# Python 3 program to
# find XOR without using ^
 
# Returns XOR of x and y
def myXOR(x, y):
    return ((x | y) &
            (~x | ~y))
 
# Driver Code
x = 3
y = 5
print("XOR is" ,
       myXOR(x, y))
 
# This code is contributed
# by Smitha


C#
// C# program to find
// XOR without using ^
using System;
 
class GFG
{
     
// Returns XOR of x and y
static int myXOR(int x, int y)
{
    return (x | y) &
           (~x | ~y);
}
 
// Driver Code
static public void Main ()
{
    int x = 3, y = 5;
    Console.WriteLine("XOR is "+
                     (myXOR(x, y)));
}
}
 
// This code is contibuted by m_kit


PHP


Javascript


C++
// C++ program to find XOR without using ^
#include 
using namespace std;
 
// Returns XOR of x and y
int myXOR(int x, int y)
{
   return (x & (~y)) | ((~x )& y);
}
 
// Driver program to test above function
int main()
{
   int x = 3, y = 5;
   cout << "XOR is " << myXOR(x, y);
   return 0;
}


Java
// Java program to find XOR without using ^
import java.io.*;
  
class GFG
{
 
// Returns XOR of x and y
static int myXOR(int x, int y)
{
return (x & (~y)) | ((~x )& y);
}
 
// Driver Code
public static void main (String[] args)
{
  
int x = 3, y = 5;
System.out.println("XOR is "+
                      (myXOR(x, y)));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program to
# Returns XOR of x and y
def myXOR(x, y):
    return (x & (~y)) | ((~x )& y)
 
# Driver Code
x = 3
y = 5
print("XOR is" ,
    myXOR(x, y))
 
# This code is contributed by shivanisinghss2110


C#
// C# program to find XOR without using ^
using System;
 
class GFG{
 
// Returns XOR of x and y
static int myXOR(int x, int y)
{
    return (x & (~y)) | ((~x )& y);
}
 
// Driver program to test above function
public static void Main()
{
    int x = 3, y = 5;
    Console.WriteLine("XOR is " +myXOR(x, y));
}
}
 
// This code is contributed by shivansinghss2110


Javascript


输出 :

XOR is 6

感谢Utkarsh Trivedi提出了此解决方案。一个更好的解决方案可以在不使用循环的情况下找到XOR。
1)查找x和y的按位“或”(结果已设置位,其中x已设置或y已设置位)。 x = 3(011)和y = 5(101)的OR为7(111)
2)要删除多余的设置位,请找到x和y都设置了位的地方。表达式“〜x | |”的值“ x”和“ y”都设置了位,则“ y”具有0位。
3)“(x | y)”和“〜x |的按位与” 〜y”产生所需的结果。

下面是实现。

C++

// C++ program to find XOR without using ^
#include 
using namespace std;
 
// Returns XOR of x and y
int myXOR(int x, int y)
{
   return (x | y) & (~x | ~y);
}
 
// Driver program to test above function
int main()
{
   int x = 3, y = 5;
   cout << "XOR is " << myXOR(x, y);
   return 0;
}

Java

// Java program to find
// XOR without using ^
import java.io.*;
 
class GFG
{
 
// Returns XOR of x and y
static int myXOR(int x, int y)
{
    return (x | y) &
           (~x | ~y);
}
 
// Driver Code
public static void main (String[] args)
{
    int x = 3, y = 5;
    System.out.println("XOR is "+
                      (myXOR(x, y)));
}
}
 
// This code is contibuted by ajit

Python3

# Python 3 program to
# find XOR without using ^
 
# Returns XOR of x and y
def myXOR(x, y):
    return ((x | y) &
            (~x | ~y))
 
# Driver Code
x = 3
y = 5
print("XOR is" ,
       myXOR(x, y))
 
# This code is contributed
# by Smitha

C#

// C# program to find
// XOR without using ^
using System;
 
class GFG
{
     
// Returns XOR of x and y
static int myXOR(int x, int y)
{
    return (x | y) &
           (~x | ~y);
}
 
// Driver Code
static public void Main ()
{
    int x = 3, y = 5;
    Console.WriteLine("XOR is "+
                     (myXOR(x, y)));
}
}
 
// This code is contibuted by m_kit

的PHP


Java脚本


输出 :

XOR is 6

感谢jitu_the_best建议此解决方案。

替代解决方案:

C++

// C++ program to find XOR without using ^
#include 
using namespace std;
 
// Returns XOR of x and y
int myXOR(int x, int y)
{
   return (x & (~y)) | ((~x )& y);
}
 
// Driver program to test above function
int main()
{
   int x = 3, y = 5;
   cout << "XOR is " << myXOR(x, y);
   return 0;
}

Java

// Java program to find XOR without using ^
import java.io.*;
  
class GFG
{
 
// Returns XOR of x and y
static int myXOR(int x, int y)
{
return (x & (~y)) | ((~x )& y);
}
 
// Driver Code
public static void main (String[] args)
{
  
int x = 3, y = 5;
System.out.println("XOR is "+
                      (myXOR(x, y)));
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python3 program to
# Returns XOR of x and y
def myXOR(x, y):
    return (x & (~y)) | ((~x )& y)
 
# Driver Code
x = 3
y = 5
print("XOR is" ,
    myXOR(x, y))
 
# This code is contributed by shivanisinghss2110

C#

// C# program to find XOR without using ^
using System;
 
class GFG{
 
// Returns XOR of x and y
static int myXOR(int x, int y)
{
    return (x & (~y)) | ((~x )& y);
}
 
// Driver program to test above function
public static void Main()
{
    int x = 3, y = 5;
    Console.WriteLine("XOR is " +myXOR(x, y));
}
}
 
// This code is contributed by shivansinghss2110

Java脚本