The logical (Boolean) operator is used to perform an operation on an expression of the result of a Boolean, and the result of the operation is Boolean. The results of the operation are as follows:
Operator Operation Example Results
& and (with) False&true false
| or (or) false|true true
^ XOR (exclusive OR) False^true true
! Not (non)!false true
&& and (short circuit) false&&true false
|| OR (Short circuit) false| | True True
Here are some simple explanations for some of the more likely problems:
1. "^" Computes the logical "XOR" of the operand, that is, the result is true if and only if only one operand is true.
2. The difference between "&" and "&&" is that if you use the former connection, then the expressions on both sides of "&" will participate in the calculation in any case. If the latter connection is used, the expression to the right of "&&" will not be evaluated if the left side is false.
3, "|" and "| |" The difference is that "|" Indicates that either of the Boolean expressions on either side are true, the combination returns a true value, and for "| |", the same as the second, and true if the left is true, or to the right if the left is Falsh, or falsh if the right is true.
Cases
The code is as follows |
Copy Code |
Using System; Using System.Data; Using System.Configuration; Using System.Web; Using System.Web.Security; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Web.UI.WebControls.WebParts; Using System.Web.UI.HtmlControls; public partial class _default:system.web.ui.page { protected void Page_Load (object sender, EventArgs e) { int x = 0; String name = "Search Bar"; & operation, two of any one of the false is False Response.Write ("x!= 0 & name =/Search bar/" result is: "+ (x!= 0 & name = =" Search bar ")); Response.Write ("<br>"); | operation, when either of the two is true, the result of the operation is true, otherwise the Response.Write ("x!= 0 | The result of the name =/"Search bar/" operation is: "+ (x!= 0 | name = =" Search bar ")); Response.Write ("<br>"); ^ operation, true if and only if one is true, otherwise false Response.Write ("x!= 0 ^ name =/" Search bar/"result is:" + (x!= 0 ^ Name = = "Search bar")); Response.Write ("<br>"); The result of the Response.Write ("x = = 0 ^ name =/Search Bar/" is: "+ (x = = 0 ^ Name = =" Search bar ")); Response.Write ("<br>"); Operation, if True false, if False is true Response.Write ("x!= 0 of the operation result is:" +! ( X!= 0)); Response.Write ("<br>"); && short-circuit operation, if left is false then exit, if the left is true, then look to the right Response.Write ("x!= 0 && name =/Search bar/" result is: "+ (x!= 0 && name = =" Search bar ")); Response.Write ("<br>"); //|| Short-circuit operation, if the left is true, then the true exit; If the left is false, look to the right, the right is true, it is true, otherwise false Response.Write ("x!= 0 | | The result of the name =/"Search bar/" operation is: "+ (x!= 0 | | | | name = =" Search bar ")); } } |
Operator
C # provides a number of operators, which are symbols that specify what operations are performed in an expression. Enumeration is generally allowed for integer operations, such as = =,!=, <, >, <=, >=, binary +, binary-, ^, &, |, ~, + + 、--, and sizeof (). In addition, many operators can be overloaded by the user, thereby changing the meaning of these operators when applied to user-defined types.
operator Category |
operator |
Basic
|
x.y
F (x)
A[X]
X + +
x--
New
typeof
Checked
Unchecked
->
|
One dollar
|
+
-
!
~
++x
--x
(T) x
True
False
&
sizeof
|
Multiplication
|
*
/
%
|
Addition
|
+
-
|
Transform
|
<<
>>
|
Relationship and type detection
|
<
>
<=
>=
Is
As
|
Equal
|
==
!=
|
Logic "and"
|
&
|
Logical XOR
|
^
|
Logical "or"
|
|
|
Conditions and
|
&&
|
Condition OR
|
||
|
Conditional operations
|
?:
|
assigning values
|
=
+=
-=
*=
/=
%=
&=
|=
^=
<<=
>>=
??
|
- Operator Precedence
- Front + + + (plus)-(minus sign)! ~
- * / %
- + -
- << >>
- < > <= >=
- == !=
- &
- ^
- |
- &&
- ||
- Assignment operations
- After + +--
C # allows user-defined types to overload operators by defining static member functions using the operator keyword. However, not all operators can be overloaded, and the following table lists the operators that cannot be overloaded:
operator |
can be overloaded |
+ 、-、!、 ~, + + 、--、 true and False
|
These unary operators can be overloaded.
|
+,-, *,/,%, >>, |, ^, <<
|
These binary operators can be overloaded.
|
= =,!=, <=, >=
|
Comparison operators can be overloaded (but see the description later in this table).
|
&&, | |
|
Conditional logical operators cannot be overloaded, but can be computed using
& and
| overloads that can be overloaded.
|
[]
|
The array index operator cannot be overloaded, but the indexer can be defined.
|
()
|
You cannot overload a conversion operator, but you can define a new conversion operator (see explicit and implicit).
|
+ =, = =, *=,/=,%=, &=, |=, ^=, <<=, >>=
|
The assignment operator cannot be overloaded, but
= = can use the
+ calculation, and so on.
|
= 、.、?:,->, New, is, sizeof, and typeof
|
These operators cannot be overloaded.
|
public static Complex operator + (Complex C1, Complex C2)
|