| MATLAB Function Reference | ![]() |
Syntax
A & B A | B ~A
Description
符號 &, |, 和 ~
為邏輯運算元 and, or, 及 not。若結果為 0 表示邏輯的假 (F),但若有任何非零的數值則代表邏輯的真 (T)。&
運算元為邏輯的 and,| 為邏輯的 or,而 ~A 表示 A 元素的補數。函式 xor(A,B)
表示排外的 or 運算元。以下列出幾種函式及運算元的真值表:
| Inputs | and |
or |
xor |
not |
|
A |
B |
A&B |
A|B |
xor(A,B) |
~A |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
0 |
0 |
Remarks
| and |
|
and(A,B) |
| or |
|
or(A,B) |
| not |
|
not(A) |
Precedence of & and |
依 MATLAB 由左至右的計算優先次序, a|b&c 等同於 (a|b)&c。然而,在大多數的程式語言中,a|b&c
等同於 a|(b&c),因為 & 比 | 還有較高的優先次序。為了確定未來 MATLAB
版本的兼容性,您應該使用括號來確定運算元 & 及 | 的優先次序。
Examples
1 | 0 & 0 = 0 0 & 0 | 1 = 1
See Also
The relational operators: <, <=, >, >=, ==, ~=ì
| Relational Operators < > <= >= == ~= | Special Characters [ ] ( ) {} = ' . ... , ; % ! | ![]() |