PPS Chapter 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Operators

operator: An operator is a symbol that operates on certain data type.


The symbol used to perform operation on data item. An operator indicates operations to be
performed on data item.
Operand: The data item on which we perform operation.

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()

1. Arithmetic operators: used to perform arithmetic operations.


int A=10, int B=20
+ Addition A+B=30
- Subtraction A-B=-10
* Multiplication A*B=200
/ Division A/B=0, B/A=2
% Modulo division B%A=0, A%B=10

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

int A=10, int B=20


< Less than A<B =>1
> Greater than A>B =>0
<= Less than or equal to A<=B =>1
>= Greater than or equal to A>=B =>0
== Equal to A==B =>0
!= Not equal to A!=B =>1

3. Assignment operators: An Assignment operators is used to assign (storing) value to variable.


The most commonly used operator is =.
General syntax:
1. variable_name=value;
eg. a=10;
2. variable_name1=variable_name2=value;
eg.a=b=10; =>a=10,b=10;

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

4. Increment and decrement operator:

++ Increment A++ => A=A+1


-- Decrement A-- => A=A-1

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 (?:)

The increment (++) operator adds 1 to the operand value.


The decrement (--) operator subtracts 1 from the operand value.

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.

eg. x=20, y=10


z=x*y++
then x=?, y=?, z=?
Ans: x=20, y=11, z=200

eg. x=20, y=10


z=x*++y
then x=?, y=?, z=?
Ans:x=20,y=11,z=220

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.

&& Logical AND


|| Logical OR
! Logical NOT

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

FALSE FALSE FALSE (A>B)||(B>C)=>FALSE||FLASE=>FALSE

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

6. Bit wise operator:


A bitwise operator operates on each individual bit of data.
Bit: smallest unit of computer science. It stores either 0 or 1.

Operator Meaning
& Bitwise AND
| Bitwise OR

^ Bitwise XOR

~ Bitwise complement (1’s compement)

<< Left shift

>> Right shift

Note: To use these operators, first covert given number into binary format then apply bitwise
operators.
Truth Table:

Bit1 Bit2 Bit1&Bit2 Bit1|Bit2 Bit1^Bit2


0 0 0 0 0
0 1 0 1 1

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

(1000)2=0*20+ 0*21+ 0*22+ 1*23=8


(1111)2=1*20+ 1*21+ 1*22+ 1*23=15
(0111)2=1*20+ 1*21+ 1*22+ 0*23=7

bitwise complement (1’s complement):


Input bit Output bit
0 1
1 0

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

7. Conditional operators (?:)


This operator is called ternary operator because it uses three expressions.
General syntax:
(expression1?expression2:expression3);

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.

eg. a=3, b=3;


(a>b?printf(“a is greater”):printf(“b is greater”));
output: b is greater

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

You might also like