Arithmetic Operators

Arithmetic Operators

The fundamental operators in C# are much the same as they are in most

other modern languages. The fundamental operators in C#

+          Addition

-           Subtraction, unary minus

*          Multiplication

/           Division

%         modulo (remainder after integer division)

The bitwise and logical operators are derived from C rather. Bitwise operators operate on individual bits of two words, producing a result based on an AND, OR or NOT operation.

 

These are distinct from the Boolean operators; because they operate on a logical condition which

evaluates to true or false.

 

&         bitwise And

|           Bitwise Or

^          Bitwise exclusive Or

~          One’s complement

>>        n right shift n places

<<        n left shift n places

 

Increment and Decrement Operators

Like Java and C/C++ , C# allows you to express incrementing and decrementing of integer variables using the ++and -- operators. You can apply these to the variable before or after you use it:

i = 5;

j = 10;

x = i++; //x = 5, then i = 6

y = --j; //y = 9 and j = 9

z = ++i; //z = 7 and i = 7

 

Combining Arithmetic and Assignment Statements

C# allows you to combine addition, subtraction, multiplication, and division with the assignment of the result to a new variable:

x = x + 3; //can also be written as:

x += 3; //add 3 to x; store result in x

//also with the other basic operations:

temp *= 1.80; //mult temp by 1.80

z -= 7; //subtract 7 from z

y /= 1.3; //divide y by 1.3

This is used primarily to save typing; it is unlikely to generate any different code. Of course, these compound operators (as well as the ++and – operators) cannot have spaces between them.

 

Shashi Ray