在c ++编程语言中,有些关键字可以代替逻辑运算符使用。当键盘不支持&&,!,||等符号时,关键字最初在c中使用。现在,这是c ++中逻辑运算符的一些书面版本。
运算符及其书面版本为-
| 运算符 | 符号 | 书面版本 |
|---|---|---|
| 和运算符 | && | and |
| 或运算符 | || | or |
| 不是运算符 | ! | not |
| 不等于运算符 | != | not_eq |
| 按位与运算符 | & | bitand |
| 按位或运算符 | | | bitor |
| 按位XOR运算符 | ^ | |
| 等于运算符 | &= | and_eq |
| 或等于运算符 | | = | or_eq |
| 异或等于运算符 | ^ = |
程序显示我们程序的执行情况
#include<iostream>
using namespace std;
int main(){
int x=1, y=0;
cout<<"Written logical operators are :\n";
cout<<x<<" and "<<y<<" = "<<(x and y)<<endl;
cout<<x<<" or "<<y<<" = "<<(x or y)<<endl;
cout<<x<<" bitwise and "<<y<<" = "<<(x bitand y)<<endl;
cout<<x<<" not equal to "<<y<<" = "<<(x not_eq y)<<endl;
return 0;
}输出结果
Written logical operators are : 1 and 0 = 0 1 or 0 = 1 1 bitwise and 0 = 0 1 not equal to 0 = 1
使用书面运算符的利弊-
Pro-提高了代码的可读性。
Pro-与不支持|,&、!等字符的键盘一起使用时,此功能很有用。
缺点-在语句中使用书面关键字,操作符和操作数之间需要空格,否则可能会发生错误。