# c++异或运算符 | c++xor函数 26

c++异或运算符是C++中的按位运算符之一，它将两个运算符作为操作数，并对两个操作数的每一位进行运算。异或运算的意思是求两个运算分量相应位值是否相异，相异的为1，相同的为0。

C++中异或运算符的语法

``variable1 ^ variable2``

variable1是执行异或运算的第一个变量，

^：表示异或运算

Variable2是第二个变量

### c++异或运算符的例子如下所述：

C++程序演示C++中的异或运算符对两个运算值执行异或运算并显示结果：代码:

``````//The header iostream is included to be able to make use of cin and cout statements
#include <iostream>
using namespace std;
//main method is called
int main()
{
//an integer variable called a is defined to store the first integer value
int a = 5;
//an integer variable called b is defined to store the second integer value
int b = 4;
//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b
int c = a ^ b;
cout << "The result after performing XOR operation on a and b is " << c;
return 0;
}``````

``````//The header iostream is included to be able to make use of cin and cout statements
#include <iostream>
using namespace std;
//main method is called
int main()
{
//an integer variable called a is defined to store the first integer value
int a = 20;
//an integer variable called b is defined to store the second integer value
int b = 35;
//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b
int c = a ^ b;
cout << "The result after performing XOR operation on a and b is " << c;
return 0;
}``````

``````//The header iostream is included to be able to make use of cin and cout statements
#include <iostream>
using namespace std;
//main method is called
int main()
{
//an integer variable called a is defined to store the first integer value
int a = 100;
//an integer variable called b is defined to store the second integer value
int b = 35;
//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b
int c = a ^ b;
cout << "The result after performing XOR operation on a and b is " << c;
return 0;
}``````