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.

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Chapter 4: Basic C Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
OPERATORS.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Expressions and Assignment Statements
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Operators and Expressions. Introduction C operators can be classified into a number of categories 1.Arithmetic(+, -, *, /, %) 2.Relational (, >=, ==,
ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Relational Operations
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Variable Declaration, Data types, Expressions
Operators and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
More about Numerical Computation
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Chapter 3 Operators and Expressions
Herbert G. Mayer, PSU CS Status 7/19/2015
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
OPERATORS AND EXPRESSIONS IN C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Operator King Saud University
OPERATORS in C Programming
Presentation transcript:

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

2 What’s This Blue Code? #include // 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

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

4 Table 1: C Operator Precedence Table LDescriptionOperatorAssociativityLDescriptionOperatorAssociativity 1 Function call ( ) left-to-right 5 Bitwise left shift << left-to-right Array subscript [ ] Bitwise right shift >> Struct member. 6 Less than < left-to-right Struct dereference -> Greater than > Increment (post) expr++ LT or equal to <= Decrement (post) expr-- GT or equal to >= 2 Indirection * right-to-left 7 Equal to == left-to-right Reference (addr) & Not equal to != Unary plus + 8Bitwise AND & left-to-right Unary minus - 9Bitwise XOR ^ left-to-right Logical negation ! 10Bitwise OR | left-to-right Bitwise NOT ~ 11Logical AND && left-to-right Increment (pre) ++expr 12Logical OR || left-to-right Decrement (pre) --expr 13Conditional ? : right-to-left Cast ( type ) 14Assignment = += -= *= /= %= >>= <<= &= ^= |= right-to-left Size in bytes sizeof 3 Multiplication * left-to-right 15Comma, left-to-right Division / Highest precedence is Level 1. Lowest precedence is Level 15. Use parentheses () to document or alter the order of evaluation. Modulo % 4 Addition + left-to-right Subtraction -

5 Arithmetic Operators Order of precedence (High → Low): Unary + - * / % + - OperatorDescriptionExample (Base 10) Unary + Denotes positive value+3 Unary - Denotes negative value Addition is 16 - Subtraction is 8 * Multiplication12 * 4 is 48 / Division12 / 4 is 3 % Modulus (remainder)12 % 4 is 0, 12 % 5 is 2

6 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 base exponent Example: 2.2+(3.5) 3 → * 3.5 * 3.5 -or pow( 3.5, 3 ) Example: y (x+1)/2.8 → pow( y, ( x + 1 ) / 2.8 )

7 Relational and Logical Operators Order of precedence (High → Low): ! >= == != && || TypeOperatorDescription Relational (comparing values) < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != Not equal to Logical (Boolean logic) ! Logical negation && Logical AND (short-circuit) || Logical OR (short-circuit)

8 Expressions expression  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:  false if the computed result is 0  true if the computed result is not 0

9 Examples: w = 5 + sin(3.14) / 2.75; → x = 123 – ( 10 * w ); → t1 = w >= 5; → 1 (i.e., true) t3 = x > 0 && w < 5; → 0 (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

10 Pre- Post-Increment, Decrement Operators 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-- OperatorDescriptionExample (x is variable) ++ Increment ++x, x++ -- Decrement --x, x--

11 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

12 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;

13 Bitwise Operators Bitwise operators act on individual bits. OperatorDescriptionExample (unsigned char) Base 2 Calculation ~ bitwise complement ~x ~128 is 127 & bitwise AND x & y 12 & 5 is 4 | bitwise OR x | y 12 | 5 is 13 ^ bitwise XOR x ^ y 12 ^ 5 is 9 << bitwise shift left x << #of bits 46 << 1 is 92 >> bitwise shift right x >> #of bits 130 >> 2 is → → → 4 12 → → → → → → → → → → →  → 127

14 Example: (assume MSB is bit #7 and LSB is bit #0) Read only bit #7 (use mask = ) if SR & 128 equals 128, then bit #7 was 1, otherwise bit #7 was 0 Suppose SR =  SR & = Suppose SR =  SR & = Set only bit #6 to 0 (use mask = ) CR = CR & 191; Suppose CR =  CR & = Suppose CR =  CR & = Set only bit #2 to 1 (use mask 4 10 = ) CR = CR | 4; Suppose CR = | = Suppose CR = | =

15 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

16 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 */

17 Be aware of mixed types when doing arithmetic! Example: float x; int y = 3; x = 1 / 2; x = 1.0 / 2; x = y / 2; x = (float) y / 2; x contains 0.0 x contains 0.5 x contains 1.0 x contains 1.5

18 Memory Access Operators 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 The 32-bit integer number 21 is stored there. OperatorDescriptionExample ( x is a variable) * Indirection *x & Reference (Address of) &x sizeof Size of type (in bytes) sizeof(x) Value of x is 21 Value of &x is 1024 sizeof(x) is 4 AddressContents x