C # Tutorial C # operator

Source: Internet
Author: User
Tags arithmetic operators bitwise operators logical operators


C # Operators



An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C # has a rich set of built-in operators, categorized as follows:



Arithmetic operators



Relational operators



logical operators



Bitwise operators



Assignment operators



Miscellaneous operators



This tutorial will cover arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators.



Arithmetic operators



The following table shows all the arithmetic operators supported by C #. Assuming that the value of variable A is 10 and the value of variable B is 20, then:






Operator



Describe



Instance






+ Add two operands A + B will get 30



-Subtract the second operand from the first operand a-B will get-10



* Multiply two operands by a * B to get 200



/numerator divided by denominator b/a will get 2



% modulo operator, divisible by the remainder B% A will get 0



+ + increment operator, integer value increased by 1 a++ will get 11



--The decrement operator, the integer value is reduced by 1 a--will get 9



Instance



Take a look at the following example to learn all the arithmetic operators available in C #:



Using System;

Namespace Operatorsappl
{
Class Program
{
static void Main (string[] args)
{
int a = 21;
int B = 10;
int C;

c = a + B;
Console.WriteLine ("Line 1-c value is {0}", c);
c = A-B;
Console.WriteLine ("Line 2-c value is {0}", c);
c = A * b;
Console.WriteLine ("Line 3-c value is {0}", c);
c = A/b;
Console.WriteLine ("Line 4-c value is {0}", c);
c = a% B;
Console.WriteLine ("Line 5-c value is {0}", c);
c = a++;
Console.WriteLine ("Line 6-c value is {0}", c);
c = a--;
Console.WriteLine ("Line 7-c value is {0}", c);
Console.ReadLine ();
}
}
}



When the above code is compiled and executed, it produces the following results:



The value of line 1-c is 31
The value of line 2-c is 11
The value of line 3-c is 210
The value of line 4-c is 2
The value of line 5-c is 1
The value of line 6-c is 21
The value of line 7-c is 22



Relational operators



The following table shows all the relational operators supported by C #. Assuming that the value of variable A is 10 and the value of variable B is 20, then:






Operator



Describe



Instance






= = checks whether the values of the two operands are equal, and if equal, the condition is true. (A = = B) is not true.



! = checks whether the values of the two operands are equal, and if not equal, the condition is true. (A! = B) is true.



> 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.



< checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. (A < B) is true.



>= checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so the condition is true. (A >= B) is not true.



<= checks whether the value of the left operand is less than or equal to the value of the right operand, and if so the condition is true. (a <= B) is true.



Instance



Take a look at the following example to learn all the relational operators available in C #:


Using System;

 

Class Program

{

   Static void Main(string[] args)

   {

       Int a = 21;

       Int b = 10;

       

       If (a == b)

       {

           Console.WriteLine("Line 1 - a equals b");

       }

       Else

       {

           Console.WriteLine("Line 1 - a is not equal to b");

       }

       If (a < b)

       {

           Console.WriteLine("Line 2 - a is less than b");

       }

       Else

       {

           Console.WriteLine("Line 2 - a is not less than b");

       }

       If (a > b)

       {

           Console.WriteLine("Line 3 - a is greater than b");

       }

       Else

       {

           Console.WriteLine("Line 3 - a is not greater than b");

       }

       /* Change the value of a and b */

       a = 5;

       b = 20;

       If (a <= b)

       {

          Console.WriteLine("Line 4 - a is less than or equal to b");

       }

       If (b >= a)

       {

          Console.WriteLine("Line 5 - b is greater than or equal to a");

       }

   }

} 


When the above code is compiled and executed, it produces the following results:


Line 1 - a is not equal to b

Line 2 - a is not less than b

Line 3 - a is greater than b

Line 4 - a is less than or equal to b

Line 5 - b is greater than or equal to a 


logical operators



The following table shows all the logical operators supported by C #. Assuming that the variable A is a Boolean value of true and that the variable B is a Boolean value of false, then:






Operator



Describe



Instance






&& is called logic and operator. If the two operands are nonzero, the condition is true. (A && B) are false.



|| is called a logical OR operator. If any of the two operands is nonzero, the condition is true. (A | | B) is true.



! is called a logical non-operator. The logical state used to reverse the operand. If the condition is true then the logical non-operator will make it false. (A && B) is true.



Instance



Take a look at the following example to learn all the logical operators available in C #:


Using System;

 

Namespace OperatorsAppl

{

     Class Program

     {

         Static void Main(string[] args)

         {

             Bool a = true;

             Bool b = true;

            

             If (a && b)

             {

                Console.WriteLine("Line 1 - condition is true");

             }

             If (a || b)

             {

                 Console.WriteLine("Line 2 - condition is true");

             }

             /* Change the value of a and b */

             a = false;

             b = true;

             If (a && b)

             {

                 Console.WriteLine("Line 3 - condition is true");

             }

             Else

             {

                 Console.WriteLine("Line 3 - condition is not true");

             }

             If (!(a && b))

             {

                 Console.WriteLine("Line 4 - condition is true");

             }

             Console.ReadLine();

         }

     }

}


When the above code is compiled and executed, it produces the following results:


Line 1-Condition true Line 2-condition true line 3-condition not true line 4-condition is true


Bitwise operators



Bitwise operators Act on BITS and perform bitwise operations. &, | and ^ 's truth table is as follows:






P



Q



P & Q



P | Q



P ^ q






0 0 0) 0 0



0 1 0) 1 1



1 1 1) 1 0



1 0 0) 1 1



Assuming that A = 60, and B = 13, are now represented in binary format, they are as follows:



A = 0011 1100



B = 0000 1101



-----------------



a&b = 0000 1100



a| B = 0011 1101



A^b = 0011 0001



~a = 1100 0011



The following table lists the bit operators supported by C #. Assuming that the value of variable A is 60 and the value of variable B is 13, then:






Operator



Describe



Instance






& if both are present in two operands, the binary and operator copies one to the result. (A & B) will be 12, i.e. 0000 1100



| If it exists in either operand, the binary OR operator copies one into the result. (A | B) will get 61, i.e. 0011 1101



^ If it exists in one of the operands but does not exist in two operands at the same time, the binary XOR operator copies one to the result. (A ^ B) will be 49, i.e. 0011 0001



The ~ twos complement operator is a unary operator with a "flip" bit effect. (~a) will get-61, which is 1100 0011,2 of the complement form, signed binary number.



<< binary left shift operator. The value of the left operand moves the number of digits specified by the right operand to the left. A << 2 will get 240, which is 1111 0000



>> binary Right-shift operator. The value of the left operand moves the right operand to the specified number of digits. A >> 2 will get 15, which is 0000 1111



Instance



Take a look at the following example to learn all the available bitwise operators in C #:


Using System;

Namespace OperatorsAppl

{

    Class Program

    {

        Static void Main(string[] args)

        {

            Int a = 60; /* 60 = 0011 1100 */

            Int b = 13; /* 13 = 0000 1101 */

            Int c = 0;

 

             c = a & b; /* 12 = 0000 1100 */

             Console.WriteLine("Line 1 - c has the value {0}", c );

 

             c = a | b; /* 61 = 0011 1101 */

             Console.WriteLine("The value of Line 2 - c is {0}", c);

 

             c = a ^ b; /* 49 = 0011 0001 */

             Console.WriteLine("The value of Line 3 - c is {0}", c);

 

             c = ~a; /*-61 = 1100 0011 */

             Console.WriteLine("The value of Line 4 - c is {0}", c);

 

             c = a << 2; /* 240 = 1111 0000 */

             Console.WriteLine("The value of Line 5 - c is {0}", c);

 

             c = a >> 2; /* 15 = 0000 1111 */

             Console.WriteLine("The value of Line 6 - c is {0}", c);

            Console.ReadLine();

        }

    }

}

When the above code is compiled and executed, it produces the following results:


The value of Line 1 - c is 12

The value of Line 2 - c is 61

The value of Line 3 - c is 49

The value of Line 4 - c is -61

The value of Line 5 - c is 240

The value of Line 6 - c is 15


Assignment operators



The following table lists the assignment operators supported by C #:






Operator



Describe



Instance






= Simple assignment operator, assigning the value of the right operand to the left operand C = A + B assigns the value of A + B to c



+ = Plus and assignment operator, assigning the result of the right operand plus the left operand to the left operand C + = a equals c = C + A



-= minus and assignment operator to assign the result of the left operand minus the right operand to the left operand c-= A equals c = c-a



*= Multiply and assign operator to assign the right operand to the result of the left operand to the left operand C *= a equals c = c * A



/= the assignment operator, divide the left operand by the result of the right operand to the left operand C/= A equals C = c/a



%= modulo and assignment operator, the modulus assignment of two operands to the left operand C%= a equals c = c% A



<<= left Shift and assignment operator C <<= 2 equals c = C << 2



>>= right Shift and assignment operator C >>= 2 equals c = C >> 2



&= bitwise-and-assignment operator C &= 2 equals c = C & 2



^= bitwise XOR and assignment operator C ^= 2 equals c = c ^ 2



|= bitwise OR and assignment operator C |= 2 equals c = C | 2



Instance



Take a look at the following example to learn all the assignment operators available in C #:


Using System;

 

Namespace OperatorsAppl

{

    Class Program

    {

        Static void Main(string[] args)

        {

            Int a = 21;

            Int c;

 

            c = a;

            Console.WriteLine("Line 1 - = c value = {0}", c);

 

            c += a;

            Console.WriteLine("Line 2 - += c value = {0}", c);

 

            c -= a;

            Console.WriteLine("Line 3 - -= c value = {0}", c);

 

            c *= a;

            Console.WriteLine("Line 4 - *= c value = {0}", c);

 

            c /= a;

            Console.WriteLine("Line 5 - /= c value = {0}", c);

 

            c = 200;

            c %= a;

            Console.WriteLine("Line 6 - %= c value = {0}", c);

 

            c <<= 2;

            Console.WriteLine("Line 7 - <<= c value = {0}", c);

 

            c >>= 2;

            Console.WriteLine("Line 8 - >>= c value = {0}", c);

 

            c &= 2;

            Console.WriteLine("Line 9 - &= c value = {0}", c);

 

            c ^= 2;

            Console.WriteLine("Line 10 - ^= c value = {0}", c);

 

            c |= 2;

            Console.WriteLine("Line 11 - |= c value = {0}", c);

            Console.ReadLine();

        }

    }

}

When the above code is compiled and executed, it produces the following results:



Line 1 - = c value = 21

Line 2 - += c = 42

Line 3 - the value of -= c = 21

Line 4 - the value of *= c = 441

Line 5 - value of /= c = 21

Line 6 - %= c = 11

Line 7 - <<= value of c = 44

Line 8 - the value of >>= c = 11

Line 9 - the value of &= c = 2

Line 10 - ^= value of c = 0

Line 11 - the value of |= c = 2


Miscellaneous operators



The following table lists some other important operators supported by C #, including sizeof, typeof, and? :。



The operator describes the size of the data type that the instance sizeof () returns. sizeof (int), which will return 4. typeof () returns the type of class. typeof (StreamReader); & Returns the address of the variable. &a; The actual address of the variable will be obtained. * Pointer to the variable. *a; Will point to a variable.? : conditional expression If the condition is true? or X: Otherwise, Y is to determine whether the object is of a certain type. If (Ford is car)//Check if Ford is an object of the car class. As cast, the exception is not thrown even if the conversion fails. Object obj = new StringReader ("Hello");
StringReader r = obj as StringReader;



Instance


Using System;

 

Namespace OperatorsAppl

{

     

    Class Program

    {

       Static void Main(string[] args)

       {

          

          /* An instance of the sizeof operator */

          Console.WriteLine("The size of int is {0}", sizeof(int));

          Console.WriteLine("short is {0}", sizeof(short));

          Console.WriteLine("The size of double is {0}", sizeof(double));

          

          /* Example of a ternary operator */

          Int a, b;

          a = 10;

          b = (a == 1) ? 20 : 30;

          Console.WriteLine("The value of b is {0}", b);

 

          b = (a == 10) ? 20 : 30;

          Console.WriteLine("The value of b is {0}", b);

          Console.ReadLine();

       }

    }

When the above code is compiled and executed, it produces the following results:


The size of int is 4

The size of short is 2

The size of double is 8

The value of b is 30

The value of b is 20


Operator Precedence in C #



The precedence of an operator determines the combination of items in an expression. This affects how an expression is evaluated. Some operators have higher precedence than other operators, for example, the multiplication operator has a higher precedence than the add-minus operator.



For example, x = 7 + 3 * 2, where x is assigned a value of 13 instead of 20 because the operator * has a higher priority than +, so the multiplication 3*2 is first computed, and then 7 is added.



The following table lists the operators by operator precedence from high to low, operators with higher precedence appear above the table, and operators with lower precedence appear below the table. In an expression, the higher-priority operators are evaluated first.






Category



Operator



Binding nature






Suffix () []. + +--from left to right



One Dollar +-! ~ + +--(type) * & sizeof right to left



Multiplication */% from left to right



Add minus +-from left to right



Shift << >> from left to right



Relationships < <= > >= from left to right



Equality = = = From left to right



Bits and and & left to right



Bitwise XOR or XOR ^ from left to right



Bit OR OR | From left to right



Logic and && left to right



Logical OR OR | | From left to right



Condition?: Right to Left



Assignment = + = = *=/=%=>>= <<= &= ^= |= right to left



Comma, from left to right



Instance


Using System;

 

Namespace OperatorsAppl

{

     

    Class Program

    {

       Static void Main(string[] args)

       {

          Int a = 20;

          Int b = 10;

          Int c = 15;

          Int d = 5;

          Int e;

          e = (a + b) * c / d; // ( 30 * 15 ) / 5

          Console.WriteLine("(a + b) * c / d has a value of {0}", e);

 

          e = ((a + b) * c) / d; // (30 * 15 ) / 5

          The value of Console.WriteLine("((a + b) * c) / d is {0}", e);

 

          e = (a + b) * (c / d); // (30) * (15/5)

          The value of Console.WriteLine("(a + b) * (c / d) is {0}", e);

 

          e = a + (b * c) / d; // 20 + (150/5)

          Console.WriteLine("a + (b * c) / d has the value {0}", e);

          Console.ReadLine();

       }

    }

}


When the above code is compiled and executed, it produces the following results:


(a + b) * The value of c / d is 90

((a + b) * c) / d has a value of 90

(a + b) * The value of (c / d) is 90

The value of a + (b * c) / d is 50


This is the "C # tutorial" C # Operator of the content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!


  • Related Article

    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.