Академический Документы
Профессиональный Документы
Культура Документы
Operator
• Operator is a symbol that operates on a
certain data type.
• The data items that operators act upon are
called operands.
• An expression is a combination of variables,
constants and operators written according to
the syntax of the language.
Types of operators
• Arithmetic
• Relational
• Logical
• Assignment
• Increment/Decrement
• Conditional
• Bitwise
• Special operators
Arithmetic Operators
• Perform arithmetic operations.
• Let A=10, B=20
Operator Meaning Example
!= is not equal to
(A != B) is true
Logical Operators
• Combine the results of two relational
expressions.
Operator Meaning Description
Let i=7,f=5.5
Expression Interpretation Value
(i>=6) && (f<10.5) true 1
f>5 true 1
!(f>5) false 0
(f<11) ||(i<7) false 0
c=a&b; (0000000000000100)
What is the value of c? Ans: 4
c=a|b; (0000000000000101)
What is the value of c? Ans: 5
c=a^b; (0000000000000001)
What is the value of c? Ans: 1
Shift operators
• Shift the bits towards left or right.
• Require two operands.
• First operand is the bit pattern to be shifted.
• Second operand is an unsigned integer indicating the
number of displacements.
• Shifting right by n bit positions yields the value by dividing
the number by 2n and truncating the result.
• Shifting left by n bit positions yields the value by
multiplying the number by 2n.
• 2 types
– Left Shift (<<)
– Right Shift (>>)
Left Shift operator (<<)
• Causes all the bits in the first operand to be
shifted towards left by the number of positions
indicated by the second operand.
• The leftmost bits will be lost.
• The rightmost vacant positions will be filled
with 0s for both positive and negative numbers.
• Example: a=13 (0000000000001101)
– c=a<<3=0000000001101000=104 (13*23)
Right Shift operator (>>)
• Causes all the bits in the first operand to be shifted towards
right by the number of positions indicated by the second
operand.
• The rightmost bits will be lost.
• If the number is positive, then leftmost vacant positions
will be filled with 0s.
• Example: a=13 (0000000000001101)
– c=a>>2=0000000000000011=3 (13/22)
• If the number is negative, then leftmost vacant positions
will be filled with 1s.
• Example: a=-3 (1111111111111101)
– c=a>>2=2’s complement of 1111111111111111=
000000000000000001=-1
Special Operators
• Comma operator (,)
• sizeof operator – sizeof()
• Pointer operators – (& and *)
• Member selection operators – (. and ->)
Comma Operator
• Link the related expressions together.
• Evaluated from Left to Right.
• Ex:
– value=(x=10,y=5,x+y);
• First x is assigned the value 10,
• Then, y is assigned the value 5,
• Finally, x+y is evaluated to 15 and is assigned to
value.
sizeof Operator
• Gives the size of an operand in terms of bytes
occupied in the memory.
• The operand can be variable, constant or data
type.
• Consider the declaration
– int i; float x; char c;
– sizeof(i)=2, sizeof(x)=4, sizeof(c)=1
• Similarly, sizeof(7)=2, sizeof(double)=8
Operator precedence and associativity table
Rules for evaluation of expression
• First parenthesized sub expression from left to right are
evaluated.
• If parentheses are nested, the evaluation begins with the
innermost sub expression
• The precedence rule is applied in determining the order of
application of operators in evaluating sub expressions
• The associatively rule is applied when 2 or more operators of the
same precedence level appear in a sub expression.
• Arithmetic expressions are evaluated from left to right using the
rules of precedence
• When parentheses are used, the expressions within parentheses
assume highest priority
Precedence of Operators
• There are 2 different priorities of arithmetic
expression
– High Priority: * / %
– Low Priority: + -
• 2nd Pass
x=5+6-1
x=11-1
x=10
Example 1
Implicit Explicit
Automatic In Assignments
Implicit Type Conversion
• Implicit type conversion, also known as coercion
• An automatic type conversion by the compiler
• If operands are of different types than lower type is
automatically converted to higher type
• If the types of two operands in an expression are
different, then the types of RHS is converted to the
LHS operand.
• If the RHS is of lower rank then it will be promoted
to the rank of LHS.
• If the RHS is higher dank then it will be demoted to
the LHS.
Automatic
long double
double
float
int
• Syntax:
– (datatype) expression
• Ex:
int x,y;
float z;
z=(float)x/y;
Explicit Type Conversion
• There may be a situation where implicit conversion may not solve our purpose.
• Ex:- float z;
int x =20, y=3;
z= x/y;
• The value of z will be 6.0 instead of 6.6
• In these cases we can specify our own conversions known as type casting or
coercion.
• It is a unary operator.
• The syntax is
– (Datatype) expression
• Ex: - z = (float) x / y;
• Now z will have value 6.66
• The cast operator changes the data type of variable x only temporarily for the
evaluation of this expression, everywhere else in the program it will be an int
variable only.
More examples
• (int)20.3
constant 20.3 converted to integer type and fractional part is lost.
• (float) 20/3
20 is converted to float and then divided by 3(result=6.66).
• (float) (20/3)
First 20 is divided by 3 and then result of the whole expression is converted to
float type (6.00)