[. NET object-oriented programming fundamentals] (6) Foundations--operators and expressions

Source: Internet
Author: User

[. NET Object-oriented Programming fundamentals ] (6) Basics in basics--operators and Expressions

Speaking of C # operators and expressions, the small partners must think very simple, in fact, to use a good expression, is not an easy thing.    A good expression allows you to do more with less, such as ternary expressions, which allows you to write fewer than N if and case statements.

An expression consists of an operand (operand) and an operator (operator)

1. Operator classification and precedence

2. Operators are categorized by number of operations

Unary operators

-X x + + x--

Binary operators

X+y x+1

Ternary operators

C? X:y

3. Classification of expressions

By number of operators can be divided into: unary expression, two-tuple expression, ternary expression

Categorized by Purpose: Regular expressions,Lambda expressions, and so on.

The expressions for these complex types are described in more detail later.

Points:

1./ division operator 5/2=1 ( not rounding, but rounding off decimals )

2.% modulo operator 5%2=1 5.0%2.2=0.6 ( 2 number divisible by 1 Remainder of the number )

3. + + increment operator -- decrement operator

The above two operators can be before or after, but before and after, their order of execution is different for example:

int a=1; int b=a++;

After the operator is in, the program assigns the value of a to B, and then executes +1 on a ;

The result of this operation is : a=2 b=1

If you write the following example:

int M=1;int n=++m;

The result after execution is: m=2 n=2;

actually equivalent to int m=1; m=m+1; int n=m;

4. Shift operator << ( shift left ) and >>(shift right)

These two operators are less commonly used and are slightly more complicated by official understanding.

Can understand this: in fact, the data is converted into binary left and right movement ;

Shift right to left 0, left to right 0, after the extra part is removed.

such as: the right displacement of a person , can be calculated as follows:

String flag = Convert.ToString (89, 2); This is the conversion of your 2 into the number of votes.

Flag Result:1011001

you need to shift right, 0onthe left, one more out of the back.

Int J = Convert.ToInt32 ("0101100", 2); and then convert the 2 into the decimal number.

results:

displacement is so simple .

5. Logical XOR operator ^

For simple logical operators & | | we are familiar, but for different or many people do not understand, and even rarely use

Understand this operator, first of all it is a logical operator, but it is also an integer that can be calculated

The first Boolean operation , simply said, is that two operands are different, true;

such as:true ^ true=false; false^false=false; true ^false=true; false ^true=true;

When manipulating integers , bitwise operations are performed, such as:

100^45=73

Examples of numerical operations

3 features of bitwise XOR :

(1) 0^0=0,0^1=1 0 xor or any number = any number

(2) 1^0=1,1^1=0 1 xor or any number--any number taken against

(3) 1^1=0,0^0=0 any number of different or self = put yourself in 0

Example:10100001^00010001=10110000

Several common uses of bitwise XOR :

(1) turn some specific bits upside down

For example, 2 bits and 3 bits of the logarithm 10100001 are flipped, you can associate that number with the 00000110 Perform a bitwise XOR operation.

  0100001^00000110 = 10100111

(2) The interchange of two values is implemented without the use of temporary variables.

For example, Exchange two integer a=10100001,the value ofb=00000110 , can be implemented by the following statement:

    A = A^b ; //a=10100111

    B = B^a ; //b=10100001

    A = A^b ; //a=00000110

(3) in assembly language is often used to set the variable 0:

Xor a,a

(4) quickly determine whether two values are equal

Example 1: determine whether two integers a,b are equal, and can be implemented by the following statements:

Return ((a ^ b) = = 0)

    1. Character and string operations

Since a character can be implicitly converted to an integral type , such as 'a' +6=103 a is converted to Unicode

' A ' * ' B ' =9506 equivalent to 97*98

' A ' > ' B ' result is false equivalent to 97>98 return false

' A ' + ' Good morning ' return ' a good morning '

Thus. Different types of operations perform an implicit conversion in C # First

==============================================================================================

Back to Catalog

==============================================================================================

[. NET object-oriented programming fundamentals] (6) Foundations--operators and expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.