CS计算机代考程序代写 03_expressions

03_expressions

@NCStateECE

Connect to slido.com
#ece209

ECE 209
Computer Systems Programming
Spring 2021

Lecture 03

Operators
Arithmetic Bitwise Logical Relational

a+b
a-b
a*b
a/b
a%b
-a

a&b
a|b
a^b
~a

a<>b

a&&b
a||b
!a

a==b
ab
a<=b a>=b
a!=b

Arithmetic (floating-point)

Operator Meaning Example Value
– (unary) Change sign -a -2.6

+ Add a + b 3.4
– Subtract a – b 1.8
* Multiply a * b 2.08
/ Divide a / b 3.25

double a = 2.6, b = 0.8;

Arithmetic (integer)

Operator Meaning Example Value
– (unary) Change sign -a -9

+ Add a + b 14
– Subtract a – b 4
* Multiply a * b 45
/ Divide a / b 1

% Modulo (remainder) a % b 4

int a = 9, b = 5;

discards
fractional
result

remainder of division

Arithmetic
int a = 9
double b = 5.0;
int c = 5;

Example Value
a / b 1.8 a’s value is cast to double

b / c 1.0 c’s value is cast to double

a / c 1 integer division

a % b ??? illegal

b – a -4.0 a’s value is case to double

If types don’t match, cast (convert) to representation with larger range.

Arithmetic
int a = 9
double b = 5.0;
int c = 5;

To force a conversion, use the cast operator.

a / (int) b

(double) a / c

Convert b to int, then use integer division.

Convert a to double, then use floating-point division.

Bitwise (integer)

Operator Meaning Example Value
& AND a & b 0x8b
| OR a | b 0xaf
^ XOR a ^ b 0x24
~ NOT ~a 0x..fff54
>> Right shift a >> 2 0x2a
<< Left shift a << 2 0x2ac int a = 0xab, b = 0x8f; depends on size of int ...0010101011 ...0010001111 a b Bitwise (integer) int a = 0xab, b = 0x8f; "Why hex? Does bitwise only work on hex integers?" Bitwise (integer) int a = 0xab, b = 0x8f; "Why hex? Does bitwise only work on hex integers?" NO! There's no such thing as a "hex integer" or a "decimal integer." There are only integers. We can write integer values as decimal or hex. There is no difference. (Always binary in the CPU.) Bitwise (integer) Operator Meaning Example Value & AND a & b 139 | OR a | b 175 ^ XOR a ^ b 36 ~ NOT ~a -172 >> Right shift a >> 2 42
<< Left shift a << 2 684 int a = 171, b = 143; ...0010101011 ...0010001111 a b Logical (integer) Operator Meaning Example Value && Logical AND a && b false (0) || Logical OR a || b true (1) ! Logical NOT !a !b true (1) false (0) int a = 0, b = 0x8f; Entire value is treated as true (non-zero) or false (zero).

Relational (integer, floating-point)

Operator Meaning Example Value
== is equal to a == b false (0)
!= is not equal to a != b true (1)
> is greater than a > b false (0)
< is less than a < b true (1) <= is less than or equal to a <= b true (1) >= is greater than or equal to a >= b false (0)

int a = -25, b = 62;

Operators
Arithmetic Bitwise Logical Relational

a+b
a-b
a*b
a/b
a%b
-a

a&b
a|b
a^b
~a

a<>b

a&&b
a||b
!a

a==b
ab
a<=b a>=b
a!=b

Practice
fee 10
fie 5
foe 25.0
fum ‘0’
foo 5e2

ASCII code for ‘0’ is 48 or 0x30.

fee – fie ___________

foe – fee ___________

fum + fie ___________

fum / fee ___________

foo + foe ___________

Practice
fee 10
fie 5
foe 25.0
fum ‘0’
foo 5e2

fee – fie ____5______

foe – fee __15.0_____

fum + fie _53_0x35_’5’_

fum / fee ____4______

foo + foe ____525.___
ASCII code for ‘0’ is 48 or 0x30.

In C, an expression represents a value.

Expression !

Literal

Variable

expr OP expr

OP expr

binary op

unary op

a + b * c

a + b * c a + b * c
expr OP expr expr OP expr

2 + 3×4 = 24 (2 + 3)×4 = 20

plus, minus, logical not, bitwise not
cast expression to type

+, -, !, ~
(type) expr

multiply, divide, modulus *, /, %

add, subtract +, –

bitwise left shift, bitwise right shift <<, >>

comparisons >, >=, <, <= comparisons ==, != bitwise AND & bitwise XOR (exclusive-OR) ^ bitwise OR | logical AND && logical OR || Precedence ___ op1 ___ op2 ___ Operator from higher row is applied first. a + b * c a + b * c a + b * c expr OP expr expr OP expr 2 + 3×4 = 24 C D If you want to override precedence, use parentheses. (a + b) * c What if operators on the same row? a * b % c 2×(8 mod 3) = 4 (2×8) mod 3 = 1 What if operators on the same row? (a * b) % c Group left to right, except for unary and assignment. Assignment is an Operator var = expr This is an expression! Value is the right-hand side. Lefthand side is restricted. Must be an Lvalue. For now, this must be a variable name. 3 = a Doesn't make sense, because 3 is not a variable, and does not have a memory location. NOTE: No semicolon. A semicolon at the end makes it a statement. plus, minus, logical not, bitwise not cast expression to type +, -, !, ~ (type) expr multiply, divide, modulus *, /, % add, subtract +, - bitwise left shift, bitwise right shift <<, >>

comparisons >, >=, <, <= comparisons ==, != bitwise AND & bitwise XOR (exclusive-OR) ^ bitwise OR | logical AND && logical OR || assignment = Precedence int a = -25, b = 62, c = 10; Expression Interpreted as... Result a = b + c a = (b + c) a changed to 72,value of expression is 72 a + b = c (a + b) = c ILLEGAL a + (b = c) a + (b = c) b changes to 10,value of expression is -15 This is rarely (but occasionally) useful, but it's important because a common error is to write a = b when you mean a == b, and both are legal expressions. Combined Assignment Ops Expression Interpreted as... var += expr var = var + (expr) var *= expr var = var * (expr) var &= expr var = var & (expr) etc. Programming is not math! Looks like math... but it's not. a = a+5 In math, this can't possibly be true. Makes no sense. In a program, makes perfect sense. "Change a to become the current value of a plus 5." Looks like math... but it's not. a <= 10 In math, this would be ≤. Don't write that in a C program. The operator requires two characters. Looks like math... but it's not. 10 < x < 20 Suppose the value of x is 5. Is this expression true or false? Math: false C program: true (10 < x) < 20 0 < 20 1 Probably meant: (10 < x) && (x < 20) Side effects New operators (++ and --) Statements Conditional statement Side Effects An operator that also changes the value in a variable is said to have a side effect. Side Effects An operator that also changes the value in a variable is said to have a side effect. Assignment (=) has a side effect. Operator Syntax Evaluates to... Side effect Assignment Lvalue = expr expr Changes Lvalue to expr. count = x + 3 value of expression: x + 3 side effect: count ← x + 3 Increment and Decrement There are two other operators with a side effect: increment (++) and decrement (--). INCREASE by 1. DECREASE by 1. Changes the operand -- must be an Lvalue. Integer types and Pointers Pre-increment/decrement If operator written in front of operand, change the value before evaluating. INCREASE by 1. Operator Syntax Evaluates to... Side effect Pre-increment ++Lvalue new value Adds 1 to Lvalue. Pre-decrement --Lvalue new value Subtracts 1 from Lvalue. ++x value of expression: x + 1 side effect: x ← x + 1 --x value of expression: x - 1 side effect: x ← x - 1 DECREASE by 1. Post-increment/decrement If operator written after operand, change the value after evaluating. INCREASE by 1. Operator Syntax Evaluates to... Side effect Post-increment Lvalue++ old value Adds 1 to Lvalue. Post-decrement Lvalue-- old value Subtracts 1 from Lvalue. x++ value of expression: x side effect: x ← x + 1 x-- value of expression: x side effect: x ← x - 1 DECREASE by 1. Summary: Side Effect Operator Syntax Evaluates to... Side effect Assignment Lvalue = expr expr Changes Lvalue to expr. Pre-increment ++Lvalue new value Adds 1 to Lvalue. Pre-decrement --Lvalue new value Subtracts 1 from Lvalue. Post-increment Lvalue++ old value Adds 1 to Lvalue. Post-decrement Lvalue-- old value Subtracts 1 from Lvalue. These are the only operators with side effects. a = 3; b = a++; After these two statements: (1) What is the value of a? _______ (2) What is the value of b? _______ a = 3; b = a++; After these two statements: (1) What is the value of a? _______ (2) What is the value of b? _______ c = 3; d = ++c; After these two statements: (1) What is the value of c? _______ (2) What is the value of d? _______ a = 3; b = 10; c = a++ + --b; After these three statements: (1) What is the value of a? _______ (2) What is the value of b? _______ (3) What is the value of c? _______ x = 5; y = x++ + x++; After these two statements: (1) What is the value of x? _______ (2) What is the value of y? _______ This behavior is undefined. C does not specify when the side effect will occur, relative to the rest of the expression. In general, don't use multiple side effect operators on the same variable within a single expression. increment, decrement plus, minus, logical not, bitwise not cast expression to type ++, -- +, -, !, ~ (type) expr multiply, divide, modulus *, /, % add, subtract +, - bitwise left shift, bitwise right shift <<, >>

comparisons >, >=, <, <= comparisons ==, != bitwise AND & bitwise XOR (exclusive-OR) ^ bitwise OR | logical AND && logical OR || assignment = Precedence Building Blocks LiteralsVariables Operators Expression Statement Function Program Unit of work Unit of modularity Values (Data) Source Code File Unit of compilation