Home [C# 筆記] 邏輯運算子(Logical Operators)
Post
Cancel

[C# 筆記] 邏輯運算子(Logical Operators)

「邏輯運算子(Logical Operators)」(!, ~, &&, ||)用來對運算式中的運算元進行邏輯運算,並傳回Boolean或位元結果。

  • ! 反相運算
  • ~位元補數運算
  • && AND 運算
  • || OR 運算

!

! 反相運算

1
2
bool a = false;
bool b = !a; // true

~

~位元補數運算

1
2
int a = 15;
int b = ~a; // -16

&&

&& AND運算

1
2
3
bool a = true;
bool b = false;
bool c = a && b; // false

||

|| OR 運算

1
2
3
bool a = true;
bool b = false;
bool c = a && b; // true

Book: Visual C# 2005 建構資訊系統實戰經典教本

This post is licensed under CC BY 4.0 by the author.