Transcript Operators
ECE 103 Engineering Programming
Chapter 4
Operators
Herbert G. Mayer, PSU CS
Status 6/19/2015
Initial content copied verbatim from
ECE 103 material developed by
Professor Phillip Wong @ PSU ECE
Syllabus
What’s This Blue Code?
Operator Precedence and Associativity
Arithmetic Operators
Relational and Logical Operators
Pre- Post-Increment, Decrement Operators
Assignment Operators
Bitwise Operators
Type Conversion (Casting)
Memory Access Operators
What’s This Blue Code?
#include <stdio.h>
// now your function uses C library function
void foo( void )
{ // foo
printf( ”Hello world!\n” );
} //end foo
int main( /* no params */ )
{ // main
foo();
// clumsy to call function, but OK
return 0;
// no error
} //end main
2
Operator Precedence and Associativity
Operators perform basic arithmetic, comparison, and
logic operations
When operators are combined, rules of precedence and
associativity determine the evaluation order
Precedence Rules → specifies which operator is
evaluated first when operators of different precedence
are adjacent; e.g. multiply * before addition +
Associativity Rules → specifies which operator is
evaluated first when operators of the same precedence
are adjacent; e.g. left-to-right, or right-to-left, or none
whatsoever (e.g. IBM’s APL)
3
Table 1: C Operator Precedence Table
L
1
2
Description
Function call
( )
Array subscript
[ ]
Struct member
.
Struct dereference
expr++
Decrement (post)
expr--
Associativity
L
5
left-to-right
6
Description
Operator
Bitwise left shift
<<
Bitwise right shift
>>
Less than
<
Greater than
>
LT or equal to
<=
GT or equal to
>=
Equal to
==
Not equal to
!=
Associativity
left-to-right
left-to-right
Indirection
*
Reference (addr)
&
Unary plus
+
8
Bitwise AND
&
left-to-right
Unary minus
-
9
Bitwise XOR
^
left-to-right
Logical negation
!
10
Bitwise OR
|
left-to-right
11
Logical AND
&&
left-to-right
Bitwise NOT
~
7
right-to-left
left-to-right
Increment (pre)
++expr
12
Logical OR
||
left-to-right
Decrement (pre)
--expr
13
Conditional
? :
right-to-left
14
Assignment
= += -= *= /=
%= >>= <<=
&= ^= |=
right-to-left
15
Comma
,
left-to-right
Size in bytes
4
->
Increment (post)
Cast
3
Operator
( type )
sizeof
Multiplication
*
Division
/
Modulo
%
Addition
+
Subtraction
-
left-to-right
left-to-right
Highest precedence is Level 1.
Lowest precedence is Level 15.
Use parentheses () to document or alter the order
of evaluation.
4
Arithmetic Operators
Operator
Description
Unary + Denotes positive value
Unary - Denotes negative value
Example (Base 10)
+3
-3
+
Addition
-
Subtraction
12 + 4 is 16
12 - 4 is 8
*
Multiplication
12 * 4 is 48
/
Division
%
Modulus (remainder)
12 / 4 is 3
12 % 4 is 0, 12 % 5 is 2
Order of precedence (High → Low):
Unary + */%
+-
5
C lacks a built-in “to-the-power-of” operator, AKA exponentiation
Include header file math.h.
Use the math library function called pow()
Syntax:
pow( base, exponent ) // returns baseexponent
Example: 2.2+(3.5)3
→ 2.2 + 3.5 * 3.5 * 3.5
-or-
2.2 + pow( 3.5, 3 )
Example: y(x+1)/2.8 → pow( y, ( x + 1 ) / 2.8 )
6
Relational and Logical Operators
Type
Relational
(comparing values)
Logical
(Boolean logic)
Operator
Description
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
==
Equal to
!=
Not equal to
!
Logical negation
&&
||
Logical AND (short-circuit)
Logical OR (short-circuit)
Order of precedence (High → Low):
!
< <= > >=
== !=
&&
||
7
Expressions
a valid combination of constants,
variables, function calls, and operators that produces a
proper value
Relational and logical operators are often used in
logical (AKA boolean) expressions
Numeric value of a relational or logical expression is:
expression
false if the computed result is 0
true if the computed result is not 0
8
Examples:
w = 5 + sin(3.14) / 2.75;
x = 123 – ( 10 * w );
t1 = w >= 5;
→1
t3 = x > 0 && w < 5;
→0
→ 5.0006
→ 72.9942
(i.e., true)
(i.e., false)
! (exclamation) is the unary negation operator.
A zero operand is converted to 1
A non-zero operand is converted to 0
Example: x = 0; y = 3;
!x → 1
!y → 0
9
Pre- Post-Increment, Decrement Operators
Operator
Description
Example (x is variable)
++
Increment
++x , x++
--
Decrement
--x , x--
Increment operator ++ adds 1 to its operand
x++ is equivalent to x=x+1
Decrement operator -- subtracts 1 from its operand
x-- is equivalent to x=x-1
Increment / decrement modes:
Prefix: ++x --x
Postfix: x++ x-10
Example:
w = 0; z = 15;
w++; → 1 ++w; → 2
z--; → 14
Example: Prefix versus Postfix
n = 5; x = n++; Result: n → 6 x → 5
(Get value of n, store in x, increment n)
n = 5; x = ++n; Result: n → 6 x → 6
(Increment n, get value of n, store in x)
++ and – apply to integer variables
11
Assignment Operators
var op= expr is equivalent to var = var op expr
where op is +, -, *, /
Example:
i = i + 2; can be replaced by i += 2;
Assignment operators are:
+= -= *= /=
Example:
dec -= 2; is equivalent to dec = dec - 2;
p *= 5;
is equivalent to p = p * 5;
12
Bitwise Operators
Bitwise operators act on individual bits.
Operator
Description
~
bitwise complement
~x
&
bitwise AND
x & y
|
bitwise OR
x | y
^
bitwise XOR
x ^ y
Example
(unsigned char)
Base 2
Calculation
~128 is 127
128 → 10000000 01111111 → 127
12 → 00001100
12 & 5 is 4
5 → 00000101
00000100 → 4
12 → 00001100
12 | 5 is 13
5 → 00000101
00001101 → 13
12 → 00001100
12 ^ 5 is 9
<<
bitwise shift left
x << #of bits
46 << 1 is 92
>>
bitwise shift right
x >> #of bits
130 >> 2 is 32
5 → 00000101
00001001 → 9
46 → 00101110
001011100 → 92
130 → 10000010
0010000010 → 32
13
Example:
(assume MSB is bit #7 and LSB is bit #0)
Read only bit #7 (use mask 12810 = 1000 00002)
if SR & 128 equals 128, then bit #7 was 1, otherwise bit #7 was 0
Suppose SR = 1100 1011 SR & 1000 0000 = 1000 0000
Suppose SR = 0100 1011 SR & 1000 0000 = 0000 0000
Set only bit #6 to 0 (use mask 19110 = 1011 11112)
CR = CR & 191;
Suppose CR = 1100 1011 CR & 1011 1111 = 1000 1011
Suppose CR = 0000 1001 CR & 1011 1111 = 0000 1001
Set only bit #2 to 1 (use mask 410 = 0000 01002)
CR = CR | 4;
Suppose CR = 1100 1011 | 0000 0100 = 1100 1111
Suppose CR = 0100 1101 | 0000 0100 = 0100 1101
14
Type Conversion (Casting)
“lower” type is promoted to “higher” type before
the operation proceeds. The result is of the higher
type.
If either operand is a long double, convert the other to
long double
Otherwise, if either operand is double, convert the other to
double
Otherwise, if either operand is float, convert the other to
float
Otherwise, convert char and short to int
Then, if either operand is long, convert the other to long
15
The cast operator can convert the value of an
expression to a different type.
Syntax: (type) n
Returns the value of n cast to the specified type.
The original type of n itself is not altered.
Example:
char x = 'A';
int y;
y = x; /* Automatic cast; watch out */
y = (int) x;
/* Explicit cast */
16
Be aware of mixed types when doing arithmetic!
Example:
float x;
int y = 3;
x = 1 / 2;
x = 1.0 / 2;
x contains 0.0
x contains 0.5
x contains 1.0
x = y / 2;
x contains 1.5
x = (float) y / 2;
17
Memory Access Operators
Operator
Description
Example (x is a variable)
*
Indirection
*x
&
Reference (Address of)
&x
sizeof
Size of type (in bytes)
sizeof(x)
All variables are stored in memory, and each memory location has a
unique address.
The & operator returns the address of a variable.
Example:
Suppose x is located at memory address 1024.
The 32-bit integer number 21 is stored there.
x
Address
Contents
1024
21
Value of x is 21
Value of &x is 1024
sizeof(x) is 4
18