The preceding chapters briefly describe the arithmetic operators commonly used in C #. This chapter says a Boolean value with its Boolean operator.
Boolean values are represented in C # as BOOL types, which can store two values, true or false, or true or false, which can be said to be 0 or 1.
The main use of Boolean operations in C # is to compare. So let's start with the Boolean comparison operator, which is also a relational operator, whose name is more commonly used.
Assuming that the value of variable A is 10 and the value of variable B is 20, then:
operator |
category |
Description |
Example |
== |
Two Yuan |
Check that the values of the two operands are equal, and if they are equal, the condition is true. |
(A = = B) is not true. |
!= |
Two Yuan |
Check that the values of the two operands are equal, and the condition is true if they are not equal. |
(A! = B) is true. |
> |
Two Yuan |
Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. |
(A > B) is not true. |
< |
Two Yuan |
Checks if the value of the left operand is less than the value of the right operand, or if the condition is true. |
(A < B) is true. |
>= |
Two Yuan |
Checks whether the value of the left operand is greater than or equal to the value of the right operand, or if the condition is true. |
(A >= B) is not true. |
<= |
Two Yuan |
Checks whether the value of the left operand is less than or equal to the value of the right operand, or if the condition is true. |
(a <= B) is true. |
When dealing with Boolean operations that are commonly used, logical operations are used in addition to the relational operators. The following table
logical operators
The following table shows all the logical operators supported by C #. Assume that the variable A is a Boolean true and that the variable B is a Boolean value of false:
operator |
|
description |
instance |
& |
two $ |
is called logic and operator. If the two operands are nonzero, the condition is true. |
(A &b) is false. |
| |
two USD | The
is called a logical OR operator. If any of the two operands is nonzero, the condition is true. |
(A | B) is true. |
! |
unary | The
is called a logical non-operator. The logical state used to reverse the operand. If the condition is true, the logical non-operator will make it false. |
! ( A & B) is true. |
^ |
two USD |
is called logical XOR. Two operands either 0 or FALSE, the condition is true |
a^b is true |
&& |
two USD |
is called logical AND operator. If the two operands are nonzero, the condition is true. |
(A && B) is false. |
| | |
two USD | The
is called a logical OR operator. If any of the two operands is nonzero, the condition is true. |
(A | | B) is true. |
Tables in,& and &&,| and | | The result is the same, the name is the same, but it is listed separately, as to what is different, the latter is better than the former effect of the,&& effect is better than the effect of &
There are two ways to do this list, one is when the operand is a Boolean, and the other is the displacement operation. First, the Boolean value is the Operation A=true B=false
operator |
Description |
Example |
&= |
A&=b equivalent to A=a&b |
A=fasle |
^= |
A^=b equivalent to A=a^b |
A=true |
|= |
A|=b equivalent to a=a| B |
A=true |
As for how to understand this, a&=b, the equivalent of A=a&b, is explained, when the operand is a Boolean value, the operation is in the logical and the way, and return the value of the operation "Boolean operation only two, true or false" the remaining two can be calculated in this way, When the operand is another time, it follows the displacement operation.
Canonical C#_ Boolean operations [Boolean and Boolean operators]: "C # Getting Started classic"