C#. Basic operators




Basic operators


Contents


Search other websites:

1. What kinds of operators are in C# language?

In C# are realized the following types of operators:

  • arithmetic operators;
  • operators of comparison;
  • logical operators;
  • operators of assignment;
  • bitwise operators;
  • special operators for special cases.

   


2. What kinds of operators belong to the category of arithmetic operators?

C# includes the following arithmetic operators:

+  - addition
-  - subtraction
*  - multiplication
/  - division
%  - modulo division
-- - decrement
++ - increment

   


3. Examples of using the operators of addition (+), subtraction (), multiplication (*) and division (/).

The operators of addition (+), subtraction (), multiplication (*) and division (|) may be used to the any of built-in data type.

Example 1. Operations on variables of integer types.

int a, b, c;

a = 5;
b = 8;
c = a + b; // c = 13
c = a - b; // c = -3
c = a * b; // c = 40
c = a / b; // c = 0
с = 9/4;   // c = 2

Important: division of integer numbers gives the integer result.

Example 2. Operations on variables where there are floating point data.

double a, b, c;

a = 1;
b = 2;
c = a + b; // c = 3.0
c = a - b; // c = -1.0
c = a * b; // c = 2.0
c = a / b; // c = 0.5

Example 3. Operations on variables of different numeric types.

If either operand is of floating point type, and second operand is of integer type then result will be of floating point type.

int i1, i2;
double d1, d2;

i1 = 5;
d1 = 0.5;
d2 = i1 + d1; // d2 = 5.5
d2 = i1 - d1; // d2 = 4.5

// Error: "Cannot implicitly convert type 'double' to 'int'"
// i2 = i1 + d1;

d2 = d1 / i1; // d2 = 0.1 - the "double" type
d2 = d1 * i1; // d2 = 2.5

   


4. What are the features of using of modula division operator (%)?

The operator “%” is used to calculate residue of dividing two numbers. Unlike C/C++ language the operator “%” can be applied to the floating point data.



Example.

int i;
double d;

i = 7 % 3; // i = 1
i = 8 % 3; // i = 2
i = 9 % 3; // i = 0

d = 7.0 % 3.0; // d = 1
d = 7.0 % 3.5; // d = 0
d = 7.0 % 3.8; // d = 3.2

   


5. Examples of using the operators increment (++) and decrement ().

An increment operator (++) increments the operand’s value at 1. Increment operator i++ or ++i is analogical to the operator

i = i + 1;

Decrement operator () decrements the operand’s value at 1. Decrement operator i– or –i is analogical to the operator

i = i - 1;

Example 1.

int a;

a = 5;
a++; // a = 6
a--; // a = 5
++a; // a = 6
--a; // a = 5

Example 2. Using the increment operation and decrement operation in the expression.

int a, b, c;

a = 5;
b = a++; // b = 5; a = 6

a = 5;
c = ++a; // c = 6; a = 6

a = 5;
b = a--; // b = 5; a = 4

a = 5;
c = --a; // c = 4; a = 4

Example 3.

int a, b;

a = 5; // a = 5
b = 5; // b = 5
b = b + ++a; // b = 5 + 6 = 11; a = 6

   


6. What are the features of using the comparison operators in C#?

The comparison operators is applied for comparison the values of two operands between themselves. The value “true” or “false” is a result of comparison operators.

There are following comparison operators are in C#:

== - equality
!= - not equal
>  - more
<  - less
>= - more or equal
<= - less or equal

Example.

bool b;
int x, y;

x = 8;
y = 5;
b = x > y;  // b = true
b = x == y; // b = false

   


7. What are the features of using the logical operators in C#?

The operands of logical operations must be the “bool” type. There are following logical operations in C#:

& - logical "AND"
| - logical "OR"
^ - logical exclusive "OR"
&& - shortened logical "AND"
|| - shortened logical "OR"
! - logical "NOT"

Example of using the logical operations.

bool b;
bool x, y;

x = true;
y = false;
b = x ^ y;  // b = true
b = x & y;  // b = false
b = x | y;  // b = true
b = !x;     // b = false
b = x && y; // b = false
b = x || y; // b = true

   


8. What benefits of using the shortened logical operators && and ||?

Shortened logical operations && and || are similar to logical operations & (logical “AND“) and | (logical “OR“). However, in the case of long logical expressions, the operations allow to reduce run time and increase code efficiency. This is achieved due to the fact that after executing the first operand, second operand is executed as needed.

For example. Let’s consider the logical “OR” (operator ||). If the first operand is true, then there is no sense to calculate the second operand as the entire expression will be set to true.

The same is in the logical “AND” (operator &&). If the first operand is false, then the entire expression will be set to false. So, there is no sense to calculate the second operand.

Due to this, there is a reduction of processing time long logical expressions that contain the operation “AND” and “OR“.

Example.

bool b;
bool x, y;

x = true;
y = false;

// There is no sense to calculate (!y !x || x), because y = false
b = y && (!y !x || x); // b = false

// There is no sense to calculate (x & !y), because x = true
b = x || (x & !y); // b = true

Conventional logical operations & and | is expedient to use in that cases if you definitely need to calculate the right (second) operand in the program code.

   


9. What features of using the assignment operator in the program?

The simplest assignment operator is marked by symbol ‘=‘ (equal). The general form of an assignment operator:

variable_name = expression;

The assignment operator can create a “chain” of assignments.

Example.

double x, y, z;
x = y = z = 0.00; // Zeroing the value of variables x, y, z

In the above example, the value ‘0.00‘ first of all is assigned to the variable z, next it is assigned to the variable y, next it is assigned to the variable x.

The assignment operator can be constructed.

   


10. How are working the abbreviated (composite) assignment operators?

Abbreviated (composite) assignment operators are allowed to simplify the programming of some assignment operators. Abbreviated operators give the more efficient executing code, as the left operand of these operators is evaluated only once.

There are following abbreviated assignment operators in C# for arithmetic and logical operations:

+=, -=, *=, /=, %=, &=, |=, =

Examples of using the abbreviated assignment operators.

int a = 0, b;
float f = 0f;

a += 10; // a = a + 10, a = 10
f -= 3;  // f = f - 3, f = -3
b = a;   // b = 10
b %= 3;  // b = b % 3, b = 1
f = 5;   // f = 5
f *= 3;  // f = f * 3, f = 15

   


11. What features of using the bitwise operators in C#?

The bitwise logical operators allow to operate the separate bits. The bitwise logical operators is allowed only for integer operands.

There are the following bitwise logical operators in C#:

& - bitwise "AND"
| - bitwise "OR"
^ - bitwise exclusive "OR"
>> - right shift
<< - left shift
~ - addition to 1 (unary operator "NOT")

Bitwise operators with binary ones and zeros will operate according to the following table.

-------------------------------------------
 x     y     x&y     x|y     x^y     ~x
-------------------------------------------
 0     0      0       0       0       1
 0     1      0       1       1       1
 1     0      0       1       1       0
 1     1      1       1       0       0
--------------------------------------------

Example.

int x, y, z;

x = 0x03;
y = 0xFF;
z = x & y; // z = 0x03
z = x | y; // z = 0xFF

x = 0x01;
y = 0x02;
z = x ^ y; // z = 0x03

byte b;
x = 0x00;
b = (byte)~x; // b = 0xFF

   


12. What are the features of using the shift operators in C#?

The shift operators can be applied only for integers. There are two operators for shifting the bits in C#:

<< - left shift
>> - right shift

The general form of shif operators:

value << num_bits
value >> num_bits

where num_bits – the number of bits, which moves the specified value.

Example.

int a, b, c;

a = 5;
b = a >> 2; // b = 5/4 = 1;
c = a << 1; // c = 5*2 = 10

The operation of left shift at n bits multiples the number at to 2 in power n. Right shift at n bits divides at 2 in power n.

   


13. What are the special operators for special cases?

The special operators are following:

   


Related topics