Sei sulla pagina 1di 30

OPERATORS AND

EXPRESIONS
2
1. Introduction
Expression: one or more operands linked by operators
Operands: constant names, symbolic constants, simple
variable, array name, structure name, data type names,
functions name, array- structure elements, expressions
included in round parentheses
To an operand will correspond a type and a value

Operators: symbols that specifies the operations against
operands
A result will be obtained
types :
unary
binary
ternar
other: arithmetic, relational, logic, etc.
C++ allows overloading operators
An expression may use operators from
different classes. In the evaluation we
consider:
priority operations order in an expression
with different operators
associativity operations order in an
operations sequence of the same priority
implicit conversion rule to binary operators,
lower operand converted to higher operand
3
4
2. Arithmetic Operators
Unary Operators:
+: no effect
- : negation
examples : -123.45; -a; -(a+b);
Binary Operators:
multiplicative: * (multiply), /(divide), %(modulo)
addition: +(add), -(subtract)
Examples (see other):
a*b + c;
a%b;
a/b;
5
3. Relational Operators
Are binary operators
Will test the relation between operands:
operand
1
operator_relational operand
2

Arithmetic or pointers operands
Result type is int (C) or int- bool (C++):
0 (False)
!= 0, or 1 (True)

Types: <, <=, >, >=, ==, !=
Example:
int x, y;
x = 0;
y = 0;
cout << x < y: << (x<y) << \n; // x<y: 0 (false)
cout << x > y: << (x>y) << \n; // x>y: 0 (false)
cout << x <= y: << (x<=y) << \n; // x<=y: 1 (true)
cout << x == y: << (x==y) << \n; // x==y: 1 (true)
6
4. Logical Operators
Operand logical operators
- logical negation ! (unary):
!operand
!operand = 0 (false) if operand != 0 (true)
!operand = 1 (true) if operand == 0 (false)

AND logic && (binary):
operand
1
&& operand
2

Is 1 (true) if both operands are (true),
Otherwise 0 (false)

OR logic || (binary):
operand
1
|| operand
2
Is 0 (false) if both operands are zero (false),
Otherwise 1 (true)
7
Example:

int x, y;

cout << Introduceti 2 numere intregi: ;
cin >> x >> y;
cout << !x: << !x << \n;
cout << x && y: << (x&&y) << \n;
8
Bit logical operators
Applied to int type, bit by bit

Ones complement (C1) ~
~operand
if x = 00110011
then ~x= 11001100


9
Left shift <<
operand
1
<< operand
2
Left shift of the value of the first operand with a number of
positions given by the second operand
On the right bits 0 will be inserted and left bits are lost
Equivalent with multiply with twos power: a << 2 <-> a*4
a = 10 = 00001010
a<< 2 = 00101000 = 40 = 10*4

Remark1: it is possible to loose or alter data
a = 42 = 00101010
a<< 3 = 01010000 ???
Remark2: if by left shift of a positive number a 1 bit will be on
the sign position (-) the compiler will keep 0 (+). Same for
negative we keep -.
10
Right shift >>
operand
1
>> operand
2
Right shift of the value of the first operand with a number of
positions equal with the second operand
For left bits we have sign extension:
For positive numbers 0
For negative numbers 1
Right bits lost
Equivalent with twos power division: a >> 3 <-> a/8
a = 24 = 00011000
a>> 2 = 00000110 = 6 = 24/4
a = 10101000
a>>3 = 11110101

Remark: We may loose or alter data
a = 42 = 00101010
a>> 3 = 00000101 ???
11
Bit AND &:
operand
1
& operand
2
AND, bit by bit
Will impose a "mask"

a & 0x02
a =0x5a 01011010 &
00000010
-------------
00000010
Mask bit 1, others erased

a & 0x0f
a =0x5a 01011010 &
00001111
-------------
00001010
erase significant(semi)byte keep
lower(semi)byte
& 0 1
0 0 0
1 0 1
12
Bit OR |:
operand
1
| operand
2
OR bit by bit;

Used to set different bits
a | 1
a = 0x5a 01011010 |
00000001
-------------
01011011

set value 1 on 0 bit

a | 5
a = 0x5a 01011010 |
00000101
-------------
01011111

set value 1 on the corresponding mask bits
| 0 1
0 0 1
1 1 1
13
Bit XOR ^
operand
1
^ operand
2
XOR, bit by bit

Used to set or erase different bits
a ^ 1
a = 0x5a = 01011010 ^
00000001
-------------
01011011

a = 0x5b = 01011011 ^
00000001
-------------
01011010

Sets 0 on the last bit if is odd, else 1
^ 0 1
0 0 1
1 1 0
14
5. Assignment Operator
Very often used to assign a value to a declared
variable

Assignment operator is =
15
Simple assignment
Form:
var = expression;
where var is a simple variable or a reference

Effect:
Expression value assigned to replace the old value

Multiple assignment ( R -> L):
var
n
= var
n-1
= ... = var
2
= var
1
= expression;
first: var
1
= expression, next var
2
= var
1
...

Conversion of expression result to variable type
16
Compound Assignment:
Form: operator=
where operator could be arithmetic, binary or logical bit
Types:
/=, %=, *=, -=, +=, <<=, >>=, &=, |=, ^=
Structure:
var operator= expression; <=> var = var operator expression;
Motivation:
Generated code more efficient
Example: n += 9; <=> n = n + 9
n *= 5; <=> n = n * 5
int x=4, y=10;
x^=y^=x^=y; // x=10, y=4, evaluation R -> L
17
Increment and decrement
Forms: ++, -- (unary)
Variants:
Prefixed (++op, --op):
increment/decrement before using operand in the current
context

Postfixes (op++, op--):
increment/decrement after using operand in the current context

Example:
int x, y;
y = 4;
x = ++y; // x=5; y=5;
x = y--; // x=5, y=4;

Compound assignment will consider all assignment types

18
6. Explicit conversion Operator (cast)
Form:
(type)operand

Result:
the operand value is converted to new type
The type or the operand value is unchanged
Example:
int i;
float a, r;
...
i = 5;
a = 2.0;
r = i/(int)a; // r=2;
19
7. Dimension Operator
Form:
sizeof(type)
sizeof expression or sizeof(expression)

If expression is the name of a variable, the result is
the number of bytes allocated for the variable
If expression is the name of an array, the result is the
number of bytes allocated for the array
If expression is the name of a structure, the result is
the number of bytes allocated for the structure
20
Example:
int i, n;
float tab[10];
...
i = sizeof(int); // i will be 2 (4)
i = sizeof n; // i = 2 (4)
i = sizeof tab[5]; // i = 4
i = sizeof tab; // i = 40
n = sizeof(tab)/sizeof(float) // n = 10
8. Addressing Operator (&) and
indirection (*)
&, addressing operator, used to determine the address
of a memory allocated data. The result is a pointer to the
operand
*, indirection operator, allows to access the value indirect
by a pointer. The result type is the operand type
addressed by the pointer
- the effect of both operators is complementary
Example:
int x, *pa;
int a[20];
pa=&a[5];
x=*pa;

21
22
9. Parentheses Operators
Round ( )
Used to:
An expression to be evaluated
Evaluation result no increment, decrement, addressing;
Call a function
Example: int i, j, k;
...
i = (j+k) - 5;
suma(i, j);
Square [ ]
Named indexing operators
Used to declare and access an array
Example: tab[i];
tab[3];
tab[i*j - k];
23
9. Conditional Operator (? and :)
Allow to construct an expression depending on a
condition
Condition could be true or false
Form: operand
1
? operand
2
: operand
3
Evaluation:
Operand1 is the condition that is evaluated;
The result could be of different types
if operand
1
!= 0 then value and type is of operand
2
else of operand
3

Example:
int i, j, k;
...
i = (j < 0) ? (-j) : (j); // i = abs(j);
k = (i >= j) ? i : j; // k = max(i,j)
24
10. Comma Operator
Comma has a double significance in C/C++:
Frequently is used as delimitation in data declarations
or in list of parameters of a function

As operator, will link two or more expressions in
one, so: expr
1
, expr
2
.
The value and the result type is given by the last
expression

25
Example:
int a, b, c;
a = 10;
c = (b = a-6, a/2); // b=4, c=5
cout << b = << b << \n;
cout << c = << c << \n;

For ambiguities is recommended round
parentheses

Other operators will be included later
26
11. Expresions
Expressions evaluation is based on rules of:
priority
Associativity
Conversions
27
Conversions rules established for different type
operators
Round parentheses will change the rules
Redundant parentheses and spaces are ignored
but used to clarify
28
Order to evaluate expressinos
Operators Associativity
( ), [ ] L->R
!, ~, +, -, ++, --, &, *, cast, sizeof R->L
*, /, % L->R
+, - L->R
<<, >> L->R
<, <=, >, >= L->R
==, != L->R
& L->R
^ L->R
| L->R
&& L->R
|| L->R
?: R->L
=, *=, /=, %=, +=, -=, &=, ^=, |=, <<=, >>= R->L
, L->R
29
Type Conversions in expressions (implicit)
Base rule:
we promote the type with a lower range value to the
type with higher range value

For integers:
char -> int byte m.s. 0 or sign extension
unsigned char -> int byte m.s. 0
signed char -> int byte m.s. with sign extension
short -> int same value
unsigned short -> int same value
30
For real data:
Lower type to upper type
float -> double -> long double

Potrebbero piacerti anche