Unit 1 - III Operators and Expressios
Unit 1 - III Operators and Expressios
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
1. Arithmetic operators 3
Operator Meaning
+ Addition
– Subtraction
* Multiplication
/ Division
❑ z=x–y 🡺 22
❑ z=x*y 🡺 135
❑ z=x/y 🡺5
❑ int c = 15/10.0;
❑ c = ?? ⇒ 1
❑ float f = 15/10; // 1
❑ f = ?? ⇒ 1.0
2. Relational Operators 8
❑ 1. TRUE 🡺 Represented by 1
❑ 2. FALSE 🡺 Represented by 0
2. Relational Operators 10
❑ Example usage of relational operator:
int main(){
int age = 20;
if( age >= 18)
{
printf("Person can vote");
}
else
{
printf("Person can not vote");
}
return 0;
}
Logical Operators 11
Operator Meaning
NZ 🡺 Non-zero
► For TRUE output both expressions must be NON-ZERO
Logical Operators 14
NZ 🡺 Non-zero
For TRUE output, either of the expressions only need to be NON-ZERO
Truth table of logical NOT operator 16
Syntax: !Exp
e.g !x , !0, !5, !(x>5)
Exp !Exp
====================
NZ 0
0 1
Logical Operators 17
Assignment Operators:
❑ = 🡺 Assign value of RHS expression to variable on
LHS
E.g: a = 5; b = c+5; d = a+b*c; b = b+5;
❑ Shorthand Assignment Operators
❑ General Syntax : v op= exp;
❑ += , -=, *=, /=, %=
❑ E.g: b+=5; a*=b; a%=3;
Assignment Operators 19
Examples:
❑ m = 5;
❑ y = ++m; // (prefix) // m=m+1; y = m;
❑ After Execution:
❑ result ⇒ y=6, m=6
❑ m = 5;
❑ y = m++; // (postfix) // y = m; m=m+1
❑ After Execution:
❑ y=5, m=6
Increment & Decrement Operators 23
Rules:
❑ ++, -- are unary operators, these work only on variables.
❑ Postfix ++(--) used in expression: first value is used in
evaluation of expression, then
incremented(decremented).
❑ Prefix ++(--) used in expression first variable
incremented(decremented), the new value is used in
evaluation of expression.
Conditional Operator ( ?: ) 24
❑ Example 1:
❑ a = 10; b=15;
❑ x = (a>b)?a:b; // x = b; 🡺 Finding larger of two.
❑ Same as:
if(a>b)
x = a;
else
x = b;
❑ A concise way of doing if-else condition.
❑ Reduces number of lines of code.
❑ z = x<0?x * -1: x; // Finds absolute value of x
Bitwise Operators 26
❑ Act on bit level in a data item(integer variable, int constant
etc.)
❑ Can’t work on floating point numbers(real numbers)
❑ Operators are:
<< 🡺Shift left
>> 🡺Shift right
& 🡺 Bitwise AND
| 🡺 Bitwise OR
^ 🡺Bitwise Exclusive OR
~ 🡺 Bitwise complement
Computer Memory(RAM) layout 27
int y = 2; 31(decimal) =
0000000000011111(binary)
1(decimal) =
8 bits = 1 Byte
0000000000000001(binary)
1024 bytes = 1 KiloByte(KB)
1024 KB = 1 Mega Byte(MB)
1024 MB = 1 Giga Byte(GB)
1024 GB = 1 Tera Byte (TB) 1006
1005
1004
y 00000010 1003
1002
00000000 1001
x
00000001 1000
View of RAM/ Memor
primary memory
y Addr
Bitwise Operators (<<) 28
❑ Operators are:
❑ b = a>>1; // Shift the bits one position right
and assign to b.
❑ b‘s Bit Sequence in Memory: 0 0 0 0 0 0 1 0
❑ c = a & b;
❑ c‘s Bit Sequence in Memory: 0 0 0 0 0 1 0 0
Bitwise Operators(|) 31
❑ c = a | b;
❑ c‘s Bit Sequence in Memory: 0 0 0 0 0 1 1 1
Bitwise Operators(^) 32
❑ c = a ^ b;
❑ c‘s Bit Sequence in Memory: 0 0 0 0 0 0 1 1
Special Operators:Comma(,) 33
1. Comma (,)
❑ Used to link related expressions together.
❑ Comma separated expressions execute from
left to right.
❑ Right most expression is the value of the
combined expression.
❑ E.g: v = (x=10, y=5, x+y);
❑ Value of v?
❑ E.g. for loop: for(i=0,j=1; i<n; i++,j++)
Special Operators: sizeof 34
► (m+n)(x+y) (m+n)*(x+y)
► (ab)/c (a*b)/c
► (x/y) + c x/y+c
36
Evaluation of expressions
❑ General format:
variable = expression; // 5, a, x*a+5
❑ First expression is evaluated, the result is assigned
to variable in LHS.
❑ x = a * b - c;
❑ y = b * c/a; // *,/ > +,-
❑ z = a – b / c + d;
Precedence of Arithmetic Operators 37
❑ E.g. a+b*c-e/f
❑ E.g. if((a>b) && (c>=d))
❑ E.g. a=5, b=10; !(a<b)
❑ (Note: ! has higher precedence than <)
❑ (a*b)*(c-d)/(e+f)
End