# 布林值邏輯運算子 (C# 參考)Boolean logical operators (C# reference)

## 邏輯否定運算子 !Logical negation operator !

! 運算子會計算其運算元的邏輯否定。The ! operator computes logical negation of its operand. 也就是說，它會在運算元評估為 false 時產生 true，並在運算元評估為 true 時產生 falseThat is, it produces true, if the operand evaluates to false, and false, if the operand evaluates to true:

bool passed = false;
Console.WriteLine(!passed);  // output: True
Console.WriteLine(!true);    // output: False


## 邏輯 AND 運算子 &Logical AND operator &

& 運算子會計算其運算元的邏輯 AND。The & operator computes the logical AND of its operands. xy 皆求出 true，那麼 x & y 的結果會是 trueThe result of x & y is true if both x and y evaluate to true. 否則，結果為 falseOtherwise, the result is false.

bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = false & SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// False

bool b = true & SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True


## 邏輯互斥 OR 運算子 ^Logical exclusive OR operator ^

^ 運算子會計算其運算元的邏輯互斥 OR，其也稱為邏輯 XOR。The ^ operator computes the logical exclusive OR, also known as the logical XOR, of its operands. 如果 x 評估為 truey 評估為 false，或是 x 評估為 falsey 評估為 true 時，x ^ y 的結果將會為 trueThe result of x ^ y is true if x evaluates to true and y evaluates to false, or x evaluates to false and y evaluates to true. 否則，結果為 falseOtherwise, the result is false. 也就是說，針對 bool 運算元，^ 運算子的計算結果會與不等比較運算子 != 相同。That is, for the bool operands, the ^ operator computes the same result as the inequality operator !=.

Console.WriteLine(true ^ true);    // output: False
Console.WriteLine(true ^ false);   // output: True
Console.WriteLine(false ^ true);   // output: True
Console.WriteLine(false ^ false);  // output: False


## 邏輯 OR 運算子 |Logical OR operator |

| 運算子會計算其運算元的邏輯 OR。The | operator computes the logical OR of its operands. xy 其中一項的值為 truex | y 的結果會是 trueThe result of x | y is true if either x or y evaluates to true. 否則，結果為 falseOtherwise, the result is false.

bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = true | SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// True

bool b = false | SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True


## 條件式邏輯 AND 運算子 &&Conditional logical AND operator &&

bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = false && SecondOperand();
Console.WriteLine(a);
// Output:
// False

bool b = true && SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True


## 條件邏輯 OR 運算子 ||Conditional logical OR operator ||

bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}

bool a = true || SecondOperand();
Console.WriteLine(a);
// Output:
// True

bool b = false || SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True


## 可為 Null 的布林值邏輯運算子Nullable Boolean logical operators

xx yy x&yx&y x|yx|y
truetrue truetrue truetrue truetrue
truetrue Falsefalse falsefalse truetrue
truetrue nullnull nullnull truetrue
Falsefalse truetrue Falsefalse truetrue
Falsefalse Falsefalse Falsefalse Falsefalse
Falsefalse nullnull Falsefalse nullnull
nullnull truetrue nullnull truetrue
nullnull Falsefalse Falsefalse nullnull
nullnull nullnull nullnull nullnull

bool? test = null;
Display(!test);         // output: null
Display(test ^ false);  // output: null
Display(test ^ null);   // output: null
Display(true ^ null);   // output: null

void Display(bool? b) => Console.WriteLine(b is null ? "null" : b.Value.ToString());


## 複合指派Compound assignment

x op= y


x = x op y


&|^ 運算子支援複合指派，如下列範例所示：The &, |, and ^ operators support compound assignment, as the following example shows:

bool test = true;
test &= false;
Console.WriteLine(test);  // output: False

test |= true;
Console.WriteLine(test);  // output: True

test ^= false;
Console.WriteLine(test);  // output: True


## 運算子優先順序Operator precedence

• 邏輯否定運算子 !Logical negation operator !
• 邏輯 AND 運算子 &Logical AND operator &
• 邏輯互斥 OR 運算子 ^Logical exclusive OR operator ^
• 邏輯 OR 運算子 |Logical OR operator |
• 條件邏輯 AND 運算子 &&Conditional logical AND operator &&
• 條件邏輯 OR 運算子 ||Conditional logical OR operator ||

Console.WriteLine(true | true & false);   // output: True
Console.WriteLine((true | true) & false); // output: False

bool Operand(string name, bool value)
{
Console.WriteLine(\$"Operand {name} is evaluated.");
return value;
}

var byDefaultPrecedence = Operand("A", true) || Operand("B", true) && Operand("C", false);
Console.WriteLine(byDefaultPrecedence);
// Output:
// Operand A is evaluated.
// True

var changedOrder = (Operand("A", true) || Operand("B", true)) && Operand("C", false);
Console.WriteLine(changedOrder);
// Output:
// Operand A is evaluated.
// Operand C is evaluated.
// False