CHAPTER 4 CS 3370 – C++ Expressions. Operators Unary -, *, ++, -- higher precedence than binary operators most associate right-to-left Binary most associate.

Slides:



Advertisements
Similar presentations
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C Operators, Operands, Expressions & Statements Hands-on, crash course with code examples 1/46 ©
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.
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.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
OPERATORS.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Data Types Declarations Expressions Data storage C++ Basics.
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
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.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
C++ Programming Language Lecture 4 C++ Basics – Part II
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Operators and Expressions
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Chapter 7: Expressions and Assignment Statements
Bit Operations Horton pp
Object Oriented Programming COP3330 / CGS5409
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
More about Numerical Computation
Chapter 7 Additional Control Structures
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Associativity and Prescedence
The System.exit() Method
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Operator Overloading; String and Array Objects
C++ Programming Language Lecture 4 C++ Basics – Part II
Module 10 Operations on Bits
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Lecture 2: Bits, Bytes, Ints
Bit Operations Horton pp
Presentation transcript:

CHAPTER 4 CS 3370 – C++ Expressions

Operators Unary -, *, ++, -- higher precedence than binary operators most associate right-to-left Binary most associate left-to-right exception: assignment Ternary Conditional operator: a ? b : c yields a value

Precedence and Associativity Associativity applies to operators of the same precedence left vs. right associativity Full list on page 166

lvalues and rvalues Term suggests components of an assignment statement lvalue = address (stored data and functions have addresses) rvalue = simple values (e.g., temporaries like x + y) x = y; y’s rvalue is copied to the location indicated by x’s lvalue driven by context rvalue lvalue

Unmodifiable lvalues const int n = 7; n has an address, therefore it has an lvalue but it cannot be modified lvalues are required: with &x, *x, and x[i] // x must be an lvalue modifiable lvalues are required: on the left side of an assignment as the target of -- and ++

Order of Evaluation Applies to the operands of binary operators it is usually unspecified in C++ int i = f1() * f2(); We don’t know which function will execute first don’t depend on the order you write them in use separate, sequential statements if order matters Undefined behavior: cout << i << ++i; // indeterminate result

More About ++ (and --) Unary operators Post is higher precedence than pre Pre returns an lvalue ++ ++x is legal Post return an rvalue x++ ++ is not legal ++x++ is not legal Consider x = *p++; ++ executes first (!!!) but it yields the un-incremented value then * executes after the assignment, p is incremented as a side effect

Sequence Points Points in code where pending side effects are carried out The + operator is not a sequence point: that’s why f1() + f2() was indeterminate Occur with the following operators/contexts: Logical connectives ( &&, || ) if (x++ && x > y) … Comma operator if (f1(), f()) … The ? in the ternary operator (?:) Statements Complete conditions in if, while, for, and switch End of an initializer: int a = x++, b = ++x; // done in order

Bitwise Operators

Who cares about bits? Most people don’t! Operating systems do Embedded systems programming Flags, etc. People short on memory resources do, Can pack boolean values into an int Examples: std::bitset You do! Since Program 3 requires bitwise operations

Bitwise Operators Bitwise-OR (|) Bitwise-AND (&) Bitwise-XOR (^)(“exclusive-or”) Bitwise-NOT(~) Shift-left (<<) Shift-right(>>) Only work on integers Any size (char, short, int, long) Unsigned integers should be used To avoid sign extension with >> and integral promotion

Typical Bit-processing Functions Test, set, reset, or flip (toggle) bits Extract a subset of bits as a number

Bitwise Principles ANDing a bit with a 1 reads it (b & 1 == b) ORing a bit with 1 sets it to 1 (b | 1 == 1) ANDing a bit with 0 resets it (b & 0 == 0) XORing a bit with 1 flips it (b ^ 1 == ~b) ORing a bit with 0 is a nop (b | 0 == b) ANDing a bit with 1 is a nop (b & 1 == b) XORing a bit with 0 is a nop (b ^ 0 == b)

Testing a Bit Use the & operator First form a one-mask Place 1 in the desired bit position (n), zeroes elsewhere: unsigned int mask = 1u << n; Note: bit-0 is on the far right (1’s place) Then AND it with the number x & mask The result is non-zero iff the bit is 1 To convert the result to 1 or 0: !!(x & mask) See printbits.cpp

Setting a Bit Use the |= operator OR the 1-mask into the number x |= mask

Setting Multiple Contiguous bits OR multiple 1’s into the number To set all bits: x = -1, or x = ~0u (preferred) Mask to set the lower n bits: mask = (1u << n) – 1 (preferred), or mask = ~0u >> (NBITS – n) where NBITS is # of bits in an int (= sizeof(int) * CHAR_BIT) Mask to set bits m through n (inclusive, m < n): mask = (1u << (n-m+1) – 1) << m

Resetting a Bit Use the &= operator Form a 0-mask 0 in the bit position, 1’s elsewhere By flipping the 1-mask: unsigned mask = ~(1u << n) Then AND into the number: x &= mask

Resetting Multiple Bits AND multiple 0’s into the number Flip the corresponding masks for setting bits forming zero-masks Then AND the mask (&=) into the number

Copying a Bit If the desired value is 0, reset the bit Otherwise set it No short-cut!

Flipping Bits XOR 1’s into the number Form the appropriate 1-mask x ^= mask

Extracting Contiguous Bits as a Number Form the 1-mask to extract the bits By AND-ing Shift right the appropriate number of positions so the right-most bit of interest is in the 1’s place See next slide Extracts components of an IEEE float

float x = 6.5; unsigned int n = *reinterpret_cast (&x); cout << hex << n << endl; // 40d00000 int sb = n >> 31; cout << sb << endl; // 0 unsigned int expmask = ((1u << 8) - 1) << 23; cout << expmask << endl; // 7f int exp = (n & expmask) >> 23; cout << dec << exp << endl; // 129 cout > 24) << endl; // 129 (another way) int mask4 = 1u << 4; n |= mask4; // Set bit 4 cout << hex << n << endl; // 40d00010 n &= ~mask4; // Clear it cout << hex << n << endl; // 40d00000