A Boolean Logic operations
We know that variables of type bool have only two values, true and false, and mathematically 1 and 0. A lot of time to judge when a condition is set up to continue to execute, so that the conditional expression is true-true;
Boolean operators are mainly: = = = (equal to),!= (unequal), >= (greater than equal), <= (less than equal),> (greater than),< (less than). The operation model is as follows:
Variable operations |
== |
!= |
>= |
<= |
> |
< |
var1=10 var2=16 |
False |
True |
False |
True |
False |
True |
BOOL Flag; Flag= (VAR1==VAR2), the above data to calculate, Flag=false, the other one analogy, in the operation process, you can now define the type bool variable, can also be directly practical, depends on the situation. Also note that the variable types on both sides of the operator are consistent.
Two, the following describes several logical operations: &,|,!, ^,&&,| |.
The introduction of the next:& is with the operation, | is OR operation,! Right, ^ xor or operation.
Their algorithms are as follows:
The & algorithms are as follows: VAR3=VAR1&VAR2
BOOL Var1 |
Var2 |
Var3 |
True |
True |
Ture |
True |
False |
False |
False |
Ture |
False |
False |
False |
False |
|, or the algorithms are as follows:
BOOL Var1 |
Var2 |
Var3 |
True |
True |
Ture |
True |
False |
True |
False |
Ture |
True |
False |
False |
False |
!, take the non operation, this is simpler, true! is the!ture of False,false;
bool Var1,var2;var1=true;var2=!var1; then VAR2 is equal to false;
^, the principle of the XOR is also relatively simple, two variables as long as their values are the same, they are different or the result is false, if a false, a true, then their different or the result is true;
At the same time, under the conditions of circulation and other common &&,| | Operation. These two algorithms are the same as the &,|, the previous two performance is better,&& operations, as long as the first variable is false, the result is false, the back of the pipe, the same, Operation as long as the first variable is true, the result is true, which is not the same as & and |, and the latter two operations are all operational variables. This article is published in the introductory web of programming: www.bianceng.cn
Three, bitwise operators
Bitwise logical operators include: With, or |, XOR or ^ and non!
With the operation is: the two operands of the bits corresponding to the number of digits and operations
For example: int p1=10;int p2=7;
P1 binary representation: 1010
The binary representation of P2:0111; both and the operation is: 0010: namely: 2;
Or operation: the binary corresponding bit of the operand is performed or calculated: 1111:15;
Different or: results: 1101:13;
P1 of non-: 0101:5.
Displacement operation: Right Shift >>, left shift <<
var1=var2<<var3; indicates var2 left var3 single-digit digits
var2=5;var3=2; The specific process is: 0101->010100: Get 20;
Assignment operation of four, bit and logical operations
Operand: op1,op2
Operator |
An expression |
Results |
&= |
Op1&=op2 |
Op1=op1&op2 |
|= |
Op1!=op2 |
Op1=op1|op2 |
^= |
Op1^=op2 |
Op1=op1^op2 |
>>= |
Op1>>=op2 |
Op1=op1>>op2 |
<<= |
Op1<<=op2 |
Op1=op1<<op2 |
The above operation please the reader's own programming experience.
In this case, the various operations of C # are basically covered, but if an expression has more than one operator, the precedence of the different operators determines who executes first, while the operation is performed from the left to the right. The precedence of various operators is as shown in the following table:
Priority level |
Operator |
7 |
^, |
1 |
++,--(prefix). (), +,-,! , ~ |
8 |
| |
2 |
*,/,% |
9 |
&& |
3 |
+,- |
10 |
|| |
4 |
>>,<< |
11 |
=,*=,+=,-+.<<=,>>=,&=,^=,|= |
5 |
==,!= |
12 |
+ +,--(suffix operation) |
6 |
& |
|
|
There's also a three-mesh operation: An expression? A:B;
Illustration: int x=4>5? 4:5;
That means: 4>5? Whether set up, if set up so x=4, otherwise x=5;
Since then, the formal entry into C # has been studied, and the following section begins learning conditional statements if and looping statements for each, for, and while.