Arithmetic Operators. Portability: Printing Numbers

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

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

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

JAVA OPERATORS GENERAL

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

The Arithmetic Operators

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

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

Chapter 3: Operators, Expressions and Type Conversion

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Representing and Manipulating Integers Part I

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

UNIT 3 OPERATORS. [Marks- 12]

Operators and Expressions:

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Lecture 3. More About C

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

Programming in C++ 5. Integral data types

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Operators in java Operator operands.

Operators in C. Staff Incharge: S.Sasirekha

LECTURE 3 C++ Basics Part 2

Operators & Expressions

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Prof. Navrati Saxena TA: Rochak Sachan

Representing and Manipulating Integers. Jo, Heeseung

Operators. Java operators are classified into three categories:

UNIT- 3 Introduction to C++

Representing Integers

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

1.3b Type Conversion

Computer System and programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computers Programming Course 6. Iulian Năstac

Engineering Computing I

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

Department of Computer Science

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

Prepared by: Shraddha Modi

Continued from previous lecture

SECTION II: LANGUAGE BASICS

Expression and Operator

Types, Operators and Expressions

Expressions and Precedence. Last updated 12/10/18

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Types, Operators and Expressions

Information Science 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Lecture 02 C FUNDAMENTALS

CSE 230 Intermediate Programming in C and C++

Chapter 12 Variables and Operators

Chapter 4: Basic C Operators

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Data Types and Variables in C language

COMP Primitive and Class Types. Yi Hong May 14, 2015

Work relative to other classes

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programming. Elementary Concepts

Fundamentals of Programming

Chapter 7. Expressions and Assignment Statements ISBN

THE FUNDAMENTAL DATA TYPES

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Reserved Words and Identifiers

Programming for Engineers Iteration

More Programming Constructs -- Introduction

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Data Type Fall 2014 Jinkyu Jeong

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Computer Programming CS F111

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

The Design of C: A Rational Reconstruction: Part 2

Infix to Postfix Conversion

Chapter 7. Additional Control Structures

Arithmetic and Bitwise Operations on Binary Data

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Chapter 2: Using Data

Declaration and Memory

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Applied Computer Programming

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Basics of Programming

Lecture Set 4: More About Methods and More About Operators

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

Internal representation. Bitwise operators

ME 461 C review Session Fall 2009 S. Keres

Transcription:

Arithmetic Operators Normal binary arithmetic operators: + - * / Modulus or remainder operator: % x%y is the remainder when x is divided by y well defined only when x > 0 and y > 0 Unary operators: - + Precedence (see H&S, section 7.2.1) highest unary - + * / % lowest + - so -2*a + b is parsed as (((-2)*a) + b) Associativity: left to right a + b + c is parsed as ((a + b) + c) Copyright 1997 Computer Science 217: Expressions Page 49 Portability: Printing Numbers Print a number in decimal void putd(int n) { if (n < 0) { n = -n; if (n >= 10) putd(n/10); putchar(n%10 + '0'); Can this program print INT_MIN == -2147483648? Copyright 1997 Computer Science 217: Expressions Page 50

Portability: Printing Numbers, Cont d Convert to negative numbers static void putneg(int n) { if (n <= -10) putneg(n/10); putchar("0123456789"[-(n%10)]); void putd(int n) { if (n < 0) { putneg(n); else putneg(-n); n/10 and n%10 are implementation dependent when n < 0 Copyright 1997 Computer Science 217: Expressions Page 51 Portability, cont d Remainder is a mess: int a, b, q, r; q = a/b; r = a%b; ANSI Standard guarantees only q*b + r == a r < b r >= 0 when a >= 0 && b > 0 r might be negative if a is 5/(-3) = -1.666... -2-1 0 1 2 if 5/(-3) == -2, 5%(-3) = 5 - (-2)(-3) = -1 if 5/(-3) == -1, 5%(-3) = 5 - (-1)(-3) = 2 Check for sign of n%10, handle both cases static void putneg(int n) { int q = n/10, r = n%10; if (r > 0) { r -= 10; q++; if (n <= -10) putneg(q); putchar("0123456789"[-r]); if (-7)/10 == -1, (-7)%10 = -7 - (-1)(10) = 3 if (-7)/10 == 0, (-7)%10 = -7 - (0)(10) = -7 Copyright 1997 Computer Science 217: Expressions Page 52

An Easier Way Use unsigned arithmetic #include <limits.h> #include <stdio.h> static void putu(unsigned n) { if (n > 10) putu(n/10); putchar("0123456789"[n%10]); void putd(int n) { if (n == INT_MIN) { putu((unsigned)int_max + 1); else if (n < 0) { putu(-n); else putu(n); Copyright 1997 Computer Science 217: Expressions Page 53 Increment/Decrement Prefix operator increments operand before returning the value n = 5; x = ++n; x is 6, n is 6 Postfix operator increments operand after returning the value n = 5; x = n++; x is 5, n is 6 Operands of ++ and -- must be variables ++1 2 + 3++ are illegal Copyright 1997 Computer Science 217: Expressions Page 54

Relational & Logical Operators Logical values are ints: 0 is false,!0 is true Normal relational operators: > >= < <= Equality operators: ==!= Unary logical negation:! Logical connectives: && Evaluation rules: left-to-right ; as far as to determine outcome && stops when the outcome is known to be 0 stops when the outcome is known to be!0 if (i >= 0 && i < 10 && a[i] == max) ++a[i]; Associativity: left to right; precedence: highest! arithmetic operators < <= >= > ==!= && lowest Copyright 1997 Computer Science 217: Expressions Page 55 Bit Manipulation Bitwise logical operators apply to all the bits of an integer value: & bitwise AND 1&1=1 0&1=0 bitwise inclusive OR 1 0=1 0 0=0 ^ bitwise exclusive OR 1^1=0 1^0=1 unary ~ bitwise complement ~1=0 ~0=1 The operator can be used to turn on one or more bits #define BIT0 0x1 #define BIT1 0x2 #define BITS (BIT0 BIT1) flags = flags BIT0; the & operator can be used to mask off one or more bits test = flags & BITS; examples using 16-bit quantities BIT0 = 0000000000000001 BIT1 = 0000000000000010 BITS = 0000000000000011 flags = 0100011100000001 flags BITS = 0100011100000011 flags & BITS = 0000000000000001 Copyright 1997 Computer Science 217: Expressions Page 56

Shifting Shift operators: << >> x<<y shifts x left y bit positions x>>y shifts x right y bit positions When shifting right: if x is signed, shift may be arithmetic or logical if x is unsigned, shift is logical arithmetic shift fills with sign bit logical shift fills with 0 When shifting left, the vacated bits are always filled with 0 Examples using 16-bit quantities bits = 1100011100000001 bits << 2 = 0001110000000100 bits >> 2 = 1111000111000000 (arithmetic, with sign extension) bits >> 2 = 0011000111000000 (logical) Copyright 1997 Computer Science 217: Expressions Page 57 Assignment Assignment is an operator, not a statement c = getchar(); if (c == EOF)... can be written as if ((c = getchar()) == EOF)... Watch out for typos like if (c = EOF)... probably meant ==; power tools can maim... Augmented assignment combines + - * / % >> << & ^ with = i = i + 2 is the same as i += 2 flags = flags BIT0 flags = BIT0; e 1 op= e 2 is the same as e 1 = e 1 op e 2 except that e 1 is evaluated once Watch out for precedence x *= y + 1 means x *= (y + 1) not (x *= y) + 1 (which is also legal) Copyright 1997 Computer Science 217: Expressions Page 58

Conversions Implicit conversions occur in expressions and across assignments In expressions with mixed types, Promote to the higher type int + float float + float short + long long + long Watch out for sign extension! e.g. char int char c = '\377'; int i = c; is i equal to 0377 or -1? when in doubt, mask: i = c&0377 Assigning a big int to a small int, causes the extra bits to be discarded Assigning a float or double to an int truncates int n = 2.5 assigns 2 to n Explicit conversions are specified with casts: (type)expr sqrt((double)n) (int)1.5 study H&S, section 6.2 carefully Copyright 1997 Computer Science 217: Expressions Page 59 Evaluation Order Except for && and, the evaluation order of expressions is undefined; Avoid expressions whose outcome might depend on evaluation order x = f() + g(); a[i] = i++; f(++n, g(n)); Operators use lint! Associativity () [] ->. left to right! ~ ++ -- + - * & (type) sizeof right to left * / % left to right + - left to right << >> left to right < <= > >= left to right ==!= left to right & ^ left to right left to right left to right && left to right left to right?: right to left = += -= /= %= &= ^= = <<= >>= right to left, left to right Copyright 1997 Computer Science 217: Expressions Page 60