
C++ XOR运算符简介
XOR运算符是C++中的位运算符之一,它以两个操作数为操作数,对两个操作数的每一个位。进行XOR操作,对给定的两个操作数的两个比特进行XOR操作的结果是0。如果给定的两个操作数的两个位是零,对给定的两个操作数的两个位进行XOR操作的结果是零,如果给定的两个操作数的两个位是一,对给定的两个操作数的两个位进行XOR操作的结果是一,如果给定的两个操作数的两个位之一是零或一,另一个位是一或零,也就是说如果给定的两个操作数的两个位不同,结果是一。
C++中XOR运算符的语法
variable1 ^ variable2
其中variable1是包含第一个操作数的第一个变量,必须对其进行XOR操作。
^符号代表XOR操作
variable2是第二个变量,包含第二个操作数,必须对其进行XOR操作。
XOR运算符在C++中的工作
- XOR运算符是C++中的位运算符之一,它以两个操作数为操作数,对两个操作数的每一个位进行XOR运算。
- 如果给定的两个操作数的两个比特为零,那么对这两个操作数的XOR操作的结果就是零。
- 如果给定的两个操作数的两个比特为1,则对这两个操作数的两个比特进行XOR操作的结果为0。
- 如果给定的两个操作数的两个位中的一个是0或1,另一个是1或0,那么对两个操作数的两个位进行XOR操作的结果是1,也就是说,如果给定的两个操作数的两个位不同,结果是1。
C++ XOR运算符的例子
下面提到不同的例子。
例子#1
C++程序演示了C++中的XOR操作符,对给定的两个操作数进行XOR操作并显示结果。
代码
//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;
}
输出

在上述程序中,我们在程序中使用cin和cout语句包含了头文件iostream。然后调用main方法,在该方法中定义了两个整数变量a和b来存储两个整数值,并对其进行XOR操作。然后定义整数变量c来存储XOR操作的结果,该结果在屏幕上显示为输出。
例子#2
在C++中演示XOR运算符的程序,对给定的两个操作数进行XOR运算并显示结果。
代码
//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;
}
输出

在上述程序中,我们在程序中使用cin和cout语句包含了头文件iostream。然后调用main方法,在该方法中定义了两个整数变量a和b来存储两个整数值,并对其进行XOR操作。然后定义整数变量c来存储XOR操作的结果,该结果在屏幕上显示为输出。
例子#3
在C++中演示XOR运算符的程序,对给定的两个操作数进行XOR运算并显示结果。
代码
//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;
}
输出

在上述程序中,我们在程序中使用cin和cout语句包含了头文件iostream。然后调用main方法,在该方法中定义了两个整数变量a和b来存储两个整数值,并对其进行XOR操作。然后定义整数变量c来存储XOR操作的结果,该结果在屏幕上显示为输出。