C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Similar documents
Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Programming. Elementary Concepts

Arithmetic Operators. Portability: Printing Numbers

JAVA OPERATORS GENERAL

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CSCI 2212: Intermediate Programming / C Chapter 15

Prof. Navrati Saxena TA: Rochak Sachan

Expressions and Assignment Statements

Chapter 7. Expressions and Assignment Statements

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 3: Operators, Expressions and Type Conversion

Operators in java Operator operands.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Beginning C Programming for Engineers

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

CS 241 Data Organization Binary

More Programming Constructs -- Introduction

Computers Programming Course 6. Iulian Năstac

Operators in C. Staff Incharge: S.Sasirekha

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Expression and Operator

Arithmetic and Bitwise Operations on Binary Data

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Logical and Bitwise Expressions

COMP2121: Microprocessors and Interfacing. Number Systems

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Expressions & Assignment Statements

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Infix to Postfix Conversion

More about Binary 9/6/2016

Arithmetic and Bitwise Operations on Binary Data

Data III & Integers I

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

QUIZ: What value is stored in a after this

Software II: Principles of Programming Languages. Why Expressions?

Binary representation of integer numbers Operations on bits

Chapter 7. Expressions and Assignment Statements ISBN

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Signed Binary Numbers

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

Chapter 7. Expressions and Assignment Statements

Informatics Ingeniería en Electrónica y Automática Industrial

Data III & Integers I

CT 229. Java Syntax 26/09/2006 CT229

Chapter 4. Operations on Data

Logical and Bitwise Expressions

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

SECTION II: LANGUAGE BASICS

File Handling in C. EECS 2031 Fall October 27, 2014

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Operators And Expressions

Lecture 02 C FUNDAMENTALS

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A flow chart is a graphical or symbolic representation of a process.

Where we are going (today)

AGENDA Binary Operations CS 3330 Samira Khan

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Operators. Java operators are classified into three categories:

Chapter7 Expression and Assignment Statement. Introduction

Computer Organization & Systems Exam I Example Questions

Programming in C++ 5. Integral data types

Applied Computer Programming

Extended Introduction to Computer Science CS1001.py. Lecture 5 Part A: Integer Representation (in Binary and other bases)

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS 11 C track: lecture 8

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

CS 107 Lecture 2: Bits and Bytes (continued)

Representing and Manipulating Integers. Jo, Heeseung

Chapter 4 C Program Control

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

UNIT 3 OPERATORS. [Marks- 12]

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

C Programming Language (Chapter 2 of K&R) Variables and Constants

Department of Computer Science

Hardware: Logical View

CT 229 Java Syntax Continued

Types, Operators and Expressions

Data III & Integers I

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

Operators and Expressions:

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Engineering Computing I

Types, Operators and Expressions

Transcription:

C expressions (Reek, Ch. 5) 1

Shift operations Left shift: value << n discard the n leftmost bits, and add n zeroes to the right Right shift: value >> n Two definitions: logical version: discard the n rightmost bits, and add n zeroes to the left for negative values, the sign bit is the leftmost bit so logical right shift has the effect of making the value positive arithmetic right shift: like logical right shift, but maintain sign bit The distinction is only relevant for negative values 2

Right shift: example INT_MAX 01111111111111111111111111111111 2147483647 INT_MAX >> 16 00000000000000000111111111111111 32767 INT_MIN 10000000000000000000000000000000-2147483648 logical right shift: INT_MIN >> 16 11111111111111111000000000000000-32768 arithmetic right shift: INT_MIN >> 16 00000000000000001000000000000000 32768 3

Bitwise operations Binary operators &,, ^ perform bitwise and, or, xor on each bit of the operands & 0 1 0 0 0 1 0 1 0 1 0 0 1 1 1 1 ^ 0 1 0 0 1 1 1 0 Unary operator ~ perform one s complement of the operand: change each 0 to a 1 and vice versa 4

Bitwise operators: Example a: 00101110 (0x2E) b: 01011011 (0x5B) ~a: 11010001 (0xD1) ~b: 10100100 (0xA4) 00101110 00101110 00101110 01011011 01011011 01011011 a&b 00001010 a b 01111111 a^b 01110101 (0x0A) (0x7F) (0x75) 5

Example: setting a bit to one value = value 1 << bit_number; 0xAA 1 << 2 10101010 00000100 10101110 6

Example: setting a bit to zero value = value & ~ ( 1 << bit_number ); 0xAA & ~ (1 << 3) 00001000 11110111 10101010 10100010 7

Assignment is an expression In many languages, expressions only compute values They never have side effects: lasting changes to the program state (e.g. update a variable, print a symbol). C (like Java and C++) does have side effects in expressions notably, assignment expressions (e.g. ==, +=, ++) 8

Why does x = y = z = 17 work? = is right-associative so the expression is interpreted as: x = ( y = ( z = 17 ) z is assigned 17; return value is 17 y is assigned 17; return value is 17 x is assigned 17; return value is 17 9

Another example of = as subexpression The stdio library function getchar() returns a character value (read from the input), or the value EOF if end-of-file is detected. int ch; while ( (ch = getchar())!= EOF) {... Use the character stored in ch... } Note: ch is an int because EOF is larger than any char value 10

Precedence, associativity, evaluation order Precedence: given two operators of different types, which is evaluated first? Associativity: given a sequence of instances of the same operator, are they evaluated left-to-right or right-to-left? Evaluation order: given an operator with a number of operands, in what order are the operands evaluated? 11

Example: Expression evaluation a * b + c * d + e * f * has precedence over + So, multiplications are performed before additions This leaves us with a sequence of two additions + has left-to-right associativity, so do the leftmost addition first Note: the multiplications can be done in any order * has left-to-right associativity, but it doesn t matter, since we don t have a sequence of multiplications 12

Side effects and evaluation order int i = 10; i = i-- - --i * ( i = -3 ) * i++ + ++i; What are the possibilities? Note: ++i increments i and returns the incremented value; i++ does the same but returns the value before the increment 13

&& and have short circuit evaluation left operand evaluated first; right operand evaluated only if necessary &&: if left operand evaluates to 0, immediately return 0 : if left operand evaluates to nonzero value, immediately return 1 This allows you to write code like this with confidence: if (y == 0 x / y > MAX)... 14

Conditional operator: order of evaluation Ternary operator: a? b : c If a evaluates to nonzero, evaluate b and return its value Else evaluate c and return its value Only two of the three operands are evaluated at one time if (a) vs. result = a? b : c; result = b; else result = c; But avoid abuse of conditional operator e.g. nested conditionals yikes! More concise, less room for typos particularly if result is a complex expression 15

Comma operator a, b : evaluate a, then evaluate b and return b s value Useful only if a has a side effect 16