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