c++异或运算符 | c++xor函数 26
c++异或运算符是C++中的按位运算符之一,它将两个运算符作为操作数,并对两个操作数的每一位进行运算。异或运算的意思是求两个运算分量相应位值是否相异,相异的为1,相同的为0。
C++中异或运算符的语法
variable1 ^ variable2
variable1是执行异或运算的第一个变量,
^:表示异或运算
Variable2是第二个变量
c++异或运算符的例子如下所述:
例#1
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;
}
输出:
在上面的程序中,定义了两个整数变量a和B用来保存执行异或运算的两个整数值。然后定义整数变量C来保存异或运算的结果,并将其作为输出显示在屏幕上。
例#2
代码:
//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;
}
输出:
在上面的程序中,定义了两个整数变量a和B用来保存执行异或运算的两个整数值。然后定义整数变量C来保存异或运算的结果,并将其作为输出显示在屏幕上。
例#3
代码:
//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;
}
输出:
这是c++异或运算符的指南。在这里,我们通过编程示例及其输出,通过XOR运算符的定义、语法和用法来讨论C++中XOR运算符的概念。您还可以查看以下文章以了解更多信息–
常见问题FAQ
- 程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
- 请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!