Operators and Expressions: Performing Simple Calculati Ons With C#
Operators and Expressions: Performing Simple Calculati Ons With C#
Operators and Expressions: Performing Simple Calculati Ons With C#
Performing Simple
P r o g ra m
Calculati ons with C#
ming
Basics
6
Operators Precedence
Operators Precedence
Precedence Operators
Highest ()
++ -- (postfix) new typeof
++ -- (prefix) + - (unary) ! ~
* / %
+ -
<< >>
< > <= >= is as
== !=
&
Lower ^
8
Operators Precedence (2)
Precedence Operators
Higher |
&&
||
?:
Lowest = *= /= %= += -= <<= >>= &= ^= |=
12
Arithmetic Operators – Example (2)
Console.WriteLine(12 / 3); // 4
Console.WriteLine(11 / 3); // 3
int x = 0;
Console.WriteLine(5 / x); // DivideByZeroException
13
Arithmetic Operators – Overflow Examples
int bigNum = 2000000000;
int bigSum = 2 * bigNum; // Integer overflow!
Console.WriteLine(bigSum); // -294967296
bigNum = Int32.MaxValue;
bigNum = bigNum + 1;
Console.WriteLine(bigNum); // -2147483648
checked
{
// This will cause OverflowException
bigSum = bigNum * 2;
}
14
Beware of Integer Division!!!
When you divide two integers, the result is always an integer.
To determine the remainder use the remainder operator %.
Cast the dividend or divisor to type float or double.
int a = 5;
int b = 2; int a = 5;
int b = 2;
double result = a / b;
Console.WriteLine(result); // 2 int result = a % b;
Console.WriteLine(result);
double result = (double)a / b; // 1 (remainder)
Console.WriteLine(result); // 2.5
15
Arithmetic Operators
Live Demo
Logical Operators
Logical Operators
Logical operators take boolean operands and return boolean
result
Operator ! turns true to false and false to true
Behavior of the operators &&, || and ^
(1 == true, 0 == false):
Operator || || || || && && && && ^ ^ ^ ^
Operand1 0 0 1 1 0 0 1 1 0 0 1 1
Operand2 0 1 0 1 0 1 0 1 0 1 0 1
Result 0 1 1 1 0 0 0 1 0 1 1 0
18
Logical Operators – Example
bool a = true;
bool b = false;
Console.WriteLine(a && b); // False
Console.WriteLine(a || b); // True
Console.WriteLine(a ^ b); // True
Console.WriteLine(!b); // True
Console.WriteLine(b || true); // True
Console.WriteLine(b && true); // False
Console.WriteLine(a || true); // True
Console.WriteLine(a && true); // True
Console.WriteLine(!a); // False
Console.WriteLine((5>7) ^ (a==b)); // False
19
Logical Operators
Live Demo
Bitwise Operators
Bitwise Operators
Bitwise operator ~ turns all 0 to 1 and all 1 to 0
Like ! for boolean expressions but bit by bit
The operators |, & and ^ behave like ||, && and ^ for boolean
expressions but bit by bit
The << and >> move the bits (left or right)
Behavior of the operators|, & and ^:
Operator | | | | & & & & ^ ^ ^ ^
Operand1 0 0 1 1 0 0 1 1 0 0 1 1
Operand2 0 1 0 1 0 1 0 1 0 1 0 1
Result 0 1 1 1 0 0 0 1 0 1 1 0
22
Bitwise Operators (2)
Bitwise operators are used on integer numbers (byte, sbyte,
int, uint, long, ulong)
Bitwise operators are applied bit by bit
Examples: ushort a = 3; // 00000000 00000011
ushort b = 5; // 00000000 00000101
Console.WriteLine( a | b); // 00000000 00000111
Console.WriteLine( a & b); // 00000000 00000001
Console.WriteLine( a ^ b); // 00000000 00000110
Console.WriteLine(~a & b); // 00000000 00000100
Console.WriteLine( a << 1); // 00000000 00000110
Console.WriteLine( a >> 1); // 00000000 00000001
23
Bitwise Operators – Tips & Tricks
How to get the bit at position p from a number n?
int p = 5;
int n = 291; // 00000001 00100011
int nRightP = n >> p; // 00000000 00001001
int bit = nRightP & 1; // 00000000 00000001
Console.WriteLine(bit); // 1
int? x = 1;
int y = x ?? -1;
Here the value of y is 1
35
Other Operators – Example
int a = 6;
int b = 4;
Console.WriteLine(a > b ? "a>b" : "b>=a"); // a>b
Console.WriteLine((long) a); // 6
int c = b = 3; // b=3; followed by c=3;
Console.WriteLine(c); // 3
Console.WriteLine(a is int); // True
Console.WriteLine((a+b)/2); // 4
Console.WriteLine(typeof(int)); // System.Int32
int d = new int();
Console.WriteLine(d); // 0
36
Other Operators
Live Demo
Implicit and Explicit Type
Conversions
Implicit Type Conversion
Implicit type conversion
Automatic conversion of value of one data type to value of
another data type
Allowed when no loss of data is possible
"Larger" types can implicitly take values of smaller "types"
Example:
int i = 5;
long l = i; // implicit type conversion
39
Explicit Type Conversion
Explicit type conversion
Manual conversion of a value of one data type to a value of
another data type
Allowed only explicitly by (type) operator
Example:
long l = 5;
int i = (int) l; // explicit type conversion
40
Type Conversions – Example
Example of implicit and explicit conversions:
float heightInMeters = 1.74f; // Explicit conversion
double maxHeight = heightInMeters; // Implicit
int a = 2 + 3; // a = 5
int b = (a + 3) * (a - 4) + (2 * a + 7) / 4; // b = 12
bool greater = (a > b) || ((a == 0) && (b == 0));
? ?
stio ns ? ?
Qu e ?
?
?
https://softuni.bg/courses/programming-basics/
Homework Review
Live Demo
Resources
Boolean algebra (logic)
http://en.wikipedia.org/wiki/Boolean_algebra_%28logic%29
Bitwise mask
http://en.wikipedia.org/wiki/Mask_%28computing%29
Bitwise operation
http://en.wikipedia.org/wiki/Bitwise_operation
Bit hacks
graphics.stanford.edu/~seander/bithacks.html
50
License
This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International" license
51
Free Trainings @ Software University
Software University Foundation – softuni.org
Software University – High-Quality Education,
Profession and Job for Software Developers
softuni.bg
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University @ YouTube
youtube.com/SoftwareUniversity
Software University Forums – forum.softuni.bg