4.
What is an Operator ? <ul><li>Operator is an operation performed over data at runtime </li></ul><ul><ul><li>Takes one or more arguments (operands) </li></ul></ul><ul><ul><li>Produces a new value </li></ul></ul><ul><li>Operators have precedence </li></ul><ul><ul><li>Precedence defines which will be evaluated first </li></ul></ul><ul><li>Expressions are sequences of operators and operands that are evaluated to a single value </li></ul>

27.
Other Operators <ul><li>String concatenation operator + is used to concatenate strings </li></ul><ul><li>If the second operand is not a string, it is converted to string automatically </li></ul>string first = &quot;First&quot;; string second = &quot;Second&quot;; Console.WriteLine(first + second); // FirstSecond string output = &quot;The number is : &quot;; int number = 5; Console.WriteLine(output + number); // The number is : 5

28.
Other Operators (2) <ul><li>Member access operator . is used to access object members </li></ul><ul><li>Square brackets [] are used with arrays indexers and attributes </li></ul><ul><li>Parentheses ( ) are used to override the default operator precedence </li></ul><ul><li>Class cast operator (type) is used to cast one compatible type to another </li></ul>

29.
Other Operators (3) <ul><li>Conditional operator ?: has the form </li></ul><ul><ul><li>(if b is true then the result is x else the result is y ) </li></ul></ul><ul><li>The new operator is used to create new objects </li></ul><ul><li>The typeof operator returns System.Type object (the reflection of a type) </li></ul><ul><li>The is operator checks if an object is compatible with given type </li></ul>b ? x : y

33.
Implicit Type Conversion <ul><li>Implicit Type Conversion </li></ul><ul><ul><li>Automatic conversion of value of one data type to value of another data type </li></ul></ul><ul><ul><li>Allowed when no loss of data is possible </li></ul></ul><ul><ul><ul><li>&quot;Larger&quot; types can implicitly take values of smaller &quot;types&quot; </li></ul></ul></ul><ul><ul><li>Example: </li></ul></ul>int i = 5; long l = i;

34.
Explicit Type Conversion <ul><li>Explicit type conversion </li></ul><ul><ul><li>Manual conversion of a value of one data type to a value of another data type </li></ul></ul><ul><ul><li>Allowed only explicitly by (type) operator </li></ul></ul><ul><ul><li>Required when there is a possibility of loss of data or precision </li></ul></ul><ul><ul><li>Example: </li></ul></ul>long l = 5; int i = (int) l;

43.
Exercises <ul><li>Write an expression that checks if given integer is odd or even. </li></ul><ul><li>Write a boolean expression that checks for given integer if it can be divided (without remainder) by 7 and 5 in the same time. </li></ul><ul><li>Write an expression that calculates rectangle’s area by given width and height . </li></ul><ul><li>Write an expression that checks for given integer if its third digit (right-to-left) is 7 . E. g. 1732  true . </li></ul><ul><li>Write a boolean expression for finding if the bit 3 (counting from 0 ) of a given integer is 1 or 0 . </li></ul><ul><li>Write an expression that checks if given point ( x , y ) is within a circle K( O , 5 ). </li></ul>

44.
Exercises (2) <ul><li>Write an expression that checks if given positive integer number n ( n ≤ 100) is prime. E.g. 37 is prime. </li></ul><ul><li>Write an expression that calculates trapezoid's area by given sides a and b and height h . </li></ul><ul><li>Write an expression that checks for given point (x, y) if it is within the circle K( ( 1 , 1 ), 3 ) and out of the rectangle R(top= 1 , left= -1 , width= 6 , height= 2 ). </li></ul><ul><li>Write a boolean expression that returns if the bit at position p (counting from 0 ) in a given integer number v has value of 1 . Example: v = 5 ; p = 1  false. </li></ul>

45.
Exercises (3) <ul><li>Write an expression that extracts from a given integer i the value of a given bit number b . Example: i=5; b=2  value=1. </li></ul><ul><li>We are given integer number n , value v ( v =0 or 1) and a position p . Write a sequence of operators that modifies n to hold the value v at the position p from the binary representation of n . </li></ul><ul><li>Example: n = 5 (00000101), p=3, v=1  13 (00001101) </li></ul><ul><li>n = 5 (00000101), p=2, v=0  1 (00000001) </li></ul>

46.
Exercises (4) <ul><li>Write a program that exchanges bits 3, 4 and 5 with bits 24,25 and 26 of given 32-bit unsigned integer. </li></ul><ul><li>* Write a program that exchanges bits {p, p+1, …, p+k-1) with bits {q, q+1, q+k-1} of given 32-bit unsigned integer. </li></ul>