C++逻辑运算符的文字版本
在C++编程语言中,可以使用一些关键字来代替逻辑运算符。这些关键字最初在C语言中使用,当时键盘不支持`&&`、`!`、`||`等符号。现在,这里有一些C++逻辑运算符的文字版本。
运算符及其文字版本:
| 运算符 | 符号 | 文字版本 |
|---|---|---|
| 与运算符 | && | and |
| 或运算符 | || | or |
| 非运算符 | ! | not |
| 不等于运算符 | != | not_eq |
| 按位与运算符 | & | bitand |
| 按位或运算符 | | | bitor |
| 按位异或运算符 | ^ | |
| 与等于运算符 | &= | 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
使用文字运算符的优缺点:
优点 - 提高代码的可读性。
优点 - 在使用不支持`|`、`&`、`!`等字符的键盘时很有用。
缺点 - 在语句中使用文字关键字时,运算符和操作数之间需要空格,否则可能会发生错误。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP