Professional Documents
Culture Documents
PPS Chapter 3
PPS Chapter 3
PPS Chapter 3
eg. c = a + b;
=, + operator
a, b operand
Classifiaction of Operator:
Type of operator Symbolic representation
Arithmetic operators +,-,*,/,%
Relational operators <,>,<=,>=,==,!=
Logical operators &&,||,!
Assignment operators =,+=,-=,*=,/=
Increment and decrement operators ++,--
Conditional operators ?:
Bit wise operators &,|,~,^,<<,>>
Sizeof sizeof()
2. Relational operators: Relational operators are used for comparision of arithmetic, logical and
character expressions.
General syntax:
expression1 Relational operators expression2
working: The expression1 is compared with expression2 and depending on the relation the result
will be either “TRUE” (1) or “FALSE” (0)
eg. (5<10) => 1
Assume A=10
+= Addition assignment A+=5;
evaluation becomes: A=A+5;
A=10+5;
A=15
-= subtraction assignment A-=5;
evaluation becomes: A=A-5;
A=10-5;
A=5
*= multiplication assignment A*=5;
evaluation becomes: A=A*5;
A=10*5;
A=50
/= division assignment A/=5;
evaluation becomes: A=A/5;
A=10/5;
A=2
The increment and decrement operator act upon a single operand and produce a new value, so it is
called “unary operator”.
Unary operator: operator uses only one operand to perform operation.
eg. increment, decrement
Binary operator: operator uses two operands to perform operation.
eg. + (addition)
Ternary operator: operator uses three operands to perform operation.
eg. conditional operator (?:)
Classfication:
1. ++A (pre-increment): Imedialtly increments the value of operand by 1.
2. A++ (post-increment): The value of the operand will be incremented by 1 after it is utilized.
3. --A (pre-decrement):Imedialtly decrements the value of operand by 1.
4. A-- (post-decrement): The value of the operand will be decremented by 1 after it is utilized.
5. Logical operators:
A logical operators are used to evaluate logical and relational expressions.
A logical operators are used to check more than one condition.
eg. if((a<b)&&(a<c))
A logical operators are used to combine more than one condition.
A logical operators act upon operands that are themselves logical expressions.
Truth Table:
Logical AND
Expres Express Result A=5;B=10;C=15
sion1 ion2 (Expression1&&Expression2
)
TRUE TRUE TRUE (A<B)&&(B<C) =>TRUE&&TRUE =>TRUE
TRUE FALSE FALSE (B<C)&&(A>B) =>TRUE&& FALSE=>FALSE
FALSE TRUE FALSE (B>C)&&(A<B)=>FALSE&&TRUE=>FALSE
FALSE FALSE FALSE (A>B)&&(B>C)=>FALSE&&FLASE=>FALSE
Logical OR
Expres Express Result A=5;B=10;C=15
sion1 ion2 (Expression1||Expression2)
TRUE TRUE TRUE (A<B)||(B<C) =>TRUE||TRUE => TRUE
TRUE FALSE TRUE (B<C)||(A>B) =>TRUE|| FALSE=> TRUE
FALSE TRUE TRUE (B>C)||(A<B)=>FALSE||TRUE=> TRUE
Logical NOT: logical NOT takes single expression and evaluates to true (1) if expression is false or
it evaluates to flase (0) if expression is true.
General syntax: !(relational expression)
Expression1 Result A=5;B=10;C=15
!(Expression1)
TRUE FALSE !(A<B) => !(TRUE) => FALSE
FALSE TRUE !(A>B) => !(FALSE) => TRUE
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
Note: To use these operators, first covert given number into binary format then apply bitwise
operators.
Truth Table:
1 0 0 1 1
1 1 1 1 0
Bitwise AND: If all input bits are high (1) then only output bit is high (1) otherwise output bit is
low (0).
Bitwise OR: If all input bits are low (0) then only output bit is low (0) otherwise output bit is high
(1).
Bitwise XOR: If all input bits are same then output bit is low (0) otherwise output bit is high (1).
eg. x=11 and y=12
then x&y=?, x|y=?, x^y=?
Ques Rem
11/2 5 1
5/2 2 1
2/2 1 0
1/2 0 1
Write remainder in reverse order i.e. 1011
(11)10=(1011)2
Ques Rem
12/2 6 0
6/2 3 0
3/2 1 1
1/2 0 1
Write remainder in reverse order i.e. 1100
(12)10=(1100)2
x&y=(1011)&(1100)
0 0 0 0 1 0 1 1 Input 1
0 0 0 0 1 1 0 0 Input 2
0 0 0 0 1 0 0 0 x&y 8
0 0 0 0 1 1 1 1 x|y 15
0 0 0 0 0 1 1 1 x^y 7
eg. x=(12)10=(1100)2
then ~(x)=?
0 0 0 0 1 1 0 0 Input 12
1 1 1 1 0 0 1 1 output 243
128 64 32 16 0 0 2 1
2’ s complement: 1’s complement+1
<< Left shift This operator shift bits to left hand side
>> Right shift This operator shift bits to right hand side
General syntax:
y=number<< displacement; or y=number>> displacement;
where, displacement means number of position to be shifted.
eg. x=(12)10=(1100)2
y=x<<3
0 0 0 0 1 1 0 0 Input 12
0 1 1 0 0 0 0 0 x<<3 96
0 64 32 0 0 0 0 0
displacement
y=number*2
y=12*23
y=96
eg. x=(12)10=(1100)2
y=x>>3
0 0 0 0 1 1 0 0 Input 12
0 0 0 0 0 0 0 1 x>>3 1
0 0 0 0 0 0 0 1
displacement
y=number/2
y=12/23
y=1
eg. a=5,b=3;
(a>b?printf(“a is greater”):printf(“b is greater”));
output: a is greater
working:
expression1 is evaluated first. If expression1 is true then expression2 is evaluated other wise
expression3 is evaluated.
8. sizeof() operator: This operator is used to obtain size of data type or variable in terms of bytes in
the memory.
eg. int x;
float y;
char c;
double d;
printf(“%d”,sizeof(x));
printf(“%d”,sizeof(y));
printf(“%d”,sizeof(c));
printf(“%d”,sizeof(d));
output:4 4 1 8