S5 1 Operators
S5 1 Operators
Objectives
• To learn and appreciate
− Arithmetic Operators
− Relational and Logical Operators
− Type conversions
− Increment and Decrement Operators
− Bitwise Operators
− Assignment Operators and Conditional Expressions
− Precedence and Order of Evaluation
#include <stdio.h>
int main ()
{
int a = 25;
int b = -2;
printf(“%d\n”,-a);
printf(“%d\n”,-b);
return 0;
}
!= Is not equal to
ATTENTION !
the “is equal to” operator == and the “assignment” operator =
The value of a relational expression is one, if the specified relation is true and
zero if the relation is false.
E.g.:
10 < 20 is TRUE
20 < 10 is FALSE
A simple relational expression contains only one relational operator and takes
the following form.
ae1 & ae2 are arithmetic expressions, which may be simple constants,
variables or combinations of them.
4/16/2021 CSE 1051 Department of CSE 10
Relational operators
The arithmetic expressions will be evaluated first & then the results
will be compared. That is, arithmetic operators have a higher priority
over relational operators. > >= < <= all have the same precedence
and below them are the next precedence equality operators i.e. == and
!=
Suppose that i, j and k are integer variables whose
values are 1, 2 and 3 respectively.
Expression Interpretation Value
i<j true 1
(i+j)>=k true 1
(j+k)>(i+5) false 0
k!=3 false 0
true 1
j==2
!(FALSE) = TRUE
!(TRUE) = FALSE
m=5;
y=m++; Postfix Mode
Here y continues to be 5. Only m changes to 6.
Don’ts:
Attempting to use the increment or decrement
operator on an expression other than a modifiable
variable name or reference.
Example:
++(5) is a syntax error
&(AND),|(OR),^(EXOR)
op op
These are binary operators and & | ^
1 2
require two integer operands.
1 1 1 1 0
These work on their operands bit 1 0 0 1 1
by bit starting from LSB
(rightmost bit). 0 1 0 1 1
0 0 0 0 0
These are used to move bit patterns either to the left or right.
Note:
x=y<<1; same as x=y*2 (Multiplication)
x=y>>1; same as x=y/2 (Division)
Suppose x=1051100010001111
~x=0110511101110000 (complement)