C # Operators

Source: Internet
Author: User
Tags arithmetic operators bitwise operators

C # 's Wonderful World is also inseparable from its rich operators, according to the number of operands, C # operators can be divided into the following categories:

    • Unary operator: ++x,x++,--y,z--

    • Binary operator: X+y,x-y,x*y

    • Ternary operator: x= (X>A?X:A);

The C # operator can be divided into assignment operators, arithmetic operators, relational operators, logical operators, bitwise operators, and other operators if viewed from the function of the operator.

1. Assignment operators

The assignment operator includes the following combination assignment operators in addition to the basic assignment operator "=":

    • + = such as: int x; x+=5; Equivalent: x=x+5;
    • -= such as: int y,z=4;  Y-=z; Equivalent: y=y-z;
    • *= such as: Double x, y = 2.3; x*=y; equivalent: x=x*y;
    • /= such as: int x,y;int a=5;  y=3; X/=y+a; equivalent: x=x/(y+a);
    • %= such as: int y,x=12; Y%=x; Equivalent: y=y%x;
    • >>= such as: int y=16; y>>=2; equivalent to:y=y>>2;
    • <<= such as: int a=32; a<<=3; equivalent to:a=a<<3;
    • ^= such as: int a=32;a^=2; Equivalent: a=a^2;

Note that for the compound assignment operator, there is no space allowed in the middle, otherwise the program will error.

On the use of assignment operators a comprehensive example is given below:

Using System;

Class comop
       {
                 public static Void main ()
                 {
                        int  x=5,y=13;
                        x-= y+3;
                        console.writeline ("x={0}", x);

                  DOUBLE&NBSP;D1 = 10.5;
                         int d2=2;
                         d1*=d2+2;
                         console.writeline ("d1={0}", D1);

int a=16;
a>>=2;
Console.WriteLine ("A={0}", a);
}
}

The results of the operation are as follows:

Note: The data type that conforms to the right value of the assignment operator must be the same as the left-worthy data type or be implicitly convertible, or an error occurs.

2. Arithmetic operators

C # provides us with 5 arithmetic operators:

    • Addition operator (+)
    • Subtraction Operator (-)
    • Multiplication operator (*)
    • Division operator (/)
    • Remainder operator (%)

For the +,-,*,/, we are not unfamiliar, the following only say to find the remainder operator%.

In C #,% can not only find the remainder for integers, but also the remainder for decimals. such as 5%3=2,3.2%2=1.2,5%1.5=0.5.

Here is a comprehensive example of the use of arithmetic operators:

Using System;

Class Ariop
{
static void Main ()
{
int i;
i = 10/5;
Console.WriteLine ("I=10/5={0}", i);
i = 13/5;
Console.WriteLine ("I=13/5={0}", i);
i = 33/44;
                            Console.WriteLine ("I=33/44={0}", i);

int a=13,b=11;
Console.WriteLine ("A%b={0}", a%b);
Double d1=12.3,d2=1.4;
Console.WriteLine ("D1%d2={0}", D1%D2);
}
}

The results of the operation are as follows:

3. Relational operators

The relational operator, the comparison operator, is used to compare two value sizes or equal. The result should be a logical value of "true" or "false". The relational operators in C # have the following main types:

    • = = Comparison equals
    • ! = does not equal
    • < less than
    • > Greater than
    • <= less than or equal to
    • >= greater than or equal to

when writing a combination operator, be aware that there are no spaces between two characters, otherwise the system is not recognized and an error occurs .

For Boolean types, reference types, and string types, you can only use = = and! = Two relational operators, that is, no size, only equal and unequal. Here's an example of how to use it:

Using System;

Class Comop
{
static void Main ()
{
int a=23,b=19;
BOOL B1=false,b2=true,b3=false;
Double x=7e+3,y=0.0001;
Char ch= ' x ';
Console.WriteLine ("A<b+4:{0}", a<b+4);
Console.WriteLine ("(B1&AMP;&AMP;B3)!=b2:{0}", (B1&AMP;&AMP;B3) ==b2);
Console.WriteLine ("(B1&AMP;&AMP;B2) ==b3:{0}", (B1&AMP;&AMP;B2) ==b3);
Console.WriteLine ("X<y*1000:{0}", x<y*1000);
Console.WriteLine ("ch-32== ' x '-' A ' +65:{0}", ch-32== ' x '-' a ' +65);
}
}

The results of the operation are as follows:

4. Logical operators

The logical operator is used between two Boolean values. C # provides 3 types of logical operators:

&& represents the relationship between logic "and", two-dollar operator;

|| A two-dollar operator that represents a logical "or" relationship;

! Represents a logical "non" relationship, a unary operator;

The arithmetic relationship between them can be represented by a truth table. Suppose that x and Y are two Boolean values, their initial values and the results of the operation are the following table:

X

Y

X&&y

x| | Y

!x

False

False

False

False

True

Flase

True

False

True

True

True

False

False

True

Fasle

True

True

True

True

False

Here is a simple example of how to use the logical operators:

The results of the operation are as follows:

5. Conditional operators

The only ternary operator in C # is:?:, which is the conditional operator. He requires that each operand must be an expression that can be implicitly converted to type bool, which is used in the following form:

Strap style 1? Expression 2: Expression 3;

The its first operation expression 1, if the value of expression 1 is true, evaluates to Expression 2, evaluates to the value of expression 2, and if the value of expression 1 is false, the expression 3, the result of which is the value of expression 3.

An example is given below:

The result of the operation is:

We can also use the ternary operator to achieve a size of two values:

The result of this operation is:

6. Bitwise operators

We know that any information is stored in binary form on the computer. Bitwise operators are operators that manipulate data by binary. The bitwise operators in the C # language are:

    • & and
    • | Or
    • ^ XOR or
    • ~ Fetch and fill
    • << left Shift
    • >> Right Shift

Where the complement has only one operand, while the other bitwise operators have two operands. None of these operations will cause overflow. The operand of a bitwise operator is an integer or any other type that can be converted to an integral type.

(1) & and Operators

The operands of the & operator are performed in binary order with the following rules:

0&0=0
0&1=0
1&0=0
1&1=1

That is, except that two bits are 1 o'clock, and the result of the operation is 1, in any other case and the result of the operation is 0. For example, 3 and 9 are performed with operations:

Binary representation of 3:00000011

Binary representation of 9:00001001

And operation Result: 00000001

That is, the result of 3&9 is 1.

(2) | Or operator

The operand of the bits or operator is performed or operated according to the following rules:

0|0=0
0|1=1
1|0=1
1|1=1

That is, except that two bits are 0 o'clock, or the result of the operation is 0, in other cases or the result of the operation is 1. For example, 3 and 9 are performed or calculated:

Binary representation of 3:00000011

Binary representation of 9:00001001

Or operation result: 00001011

That is, the result of 3|9 is 11.

(3) ^ Xor operator

The operands of an XOR operator are bits or operated by an operation rule of:

0^0=0
0^1=0
1^0=0
1^1=1

It can be seen that when the two phase is simultaneous, the result of the XOR operation is 0; the difference is different or the result of operation is 1. such as 3 and 9 are different or operations:

Binary representation of 3:00000011

Binary representation of 9:00001001

XOR operation Result: 00001010

That is, the result of 3^9 is 10.

(4) ~ Fetch and fill operator

The take-up operator takes each bit of the operand, such as 9 to complement the result:

Binary representation of 9:00001001

Result of fetch operation: 11110110

(5) Shift operator

The left shift moves the operand to the left, the high position is discarded, and the low order is 0. For example, the binary of 12 is 00001100, the left one is 00011000 (24), the left shift two bits is 00110000 (48).

When you move the operation right, if the operand x is an int or a long type, the low of x is discarded, the other order is shifted to the right, if X is non-negative, the highest bit is set to zero, and if X is negative, the highest bit is set to 1. When x is a uint or ulong type, the low of x is discarded, and the others are shifted to the right, and the high is set to 0.

Here's a quick example to illustrate the usage of the above operators:

Using System;

Class bit
      {
              static Void main ()
              {
                   int a = 3,b = 9;
                   Console.WriteLine ("A={0},b={1}", A, b);

Console.WriteLine ("\ n demo with operator ...");
Console.WriteLine ("A&b={0}", a&b);

Console.WriteLine ("\ n demo or operator ...");
Console.WriteLine ("A|b={0}", a|b);
Console.WriteLine (" -2|2={0}", -2|2);

Console.WriteLine ("\ n Demo xor operator ...");
Console.WriteLine ("A^b={0}", a^b);

           console.writeline ("\ n Demo fetch operator ...");
                  Console.WriteLine ("~a={0},~b={1}", ~a,~b);
                  Console.WriteLine ("~16={0},~-16={1},~16u={2}", ~16,~-16,~16u);

Console.WriteLine ("\ n Demo shift operator ...");
Console.WriteLine ("A<<1={0},a<<2={1}", a<<1,a<<2);
Console.WriteLine ("B>>1={0},b>>2={1}", b>>1,b>>2);
Console.WriteLine ("A<<10={0},b>>10={1}", a<<10,b>>10);
Console.WriteLine ("12u>>2={0}", 12u>>2);

}
}

The operation results are as follows

C # Operators

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.