Binghamton University. CS-211 Fall Data Conversion. software diversity in action

Similar documents
Declaration and Memory

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

Floating Point Arithmetic

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting

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

Operators and Expressions:

CHAPTER 3 Expressions, Functions, Output

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Reserved Words and Identifiers

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Chapter 3 Structured Program Development in C Part II

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

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

Computer System and programming in C

Arithmetic type issues

Lecture 3: C Programm

Number Representation & Conversion

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

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Logical and Bitwise Expressions

Unit 4. Input/Output Functions

Programming for Engineers Iteration

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

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

Chapter 3. Fundamental Data Types

Tutorial 1: C-Language

UNIT 3 OPERATORS. [Marks- 12]

Data Types. Data Types. Integer Types. Signed Integers

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Data Types and Variables in C language

C Programming

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

CS101 Introduction to computing Floating Point Numbers

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

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

Arithmetic Operators. Portability: Printing Numbers

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Lecture 2: Python Arithmetic

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

5.Coding for 64-Bit Programs

Computer Programming CS F111

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

(T) x. Casts. A cast converts the value held in variable x to type T

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

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

Main Memory Organization

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Programming. C++ Basics

EL2310 Scientific Programming

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

Visual C# Instructor s Manual Table of Contents

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Chapter 4: Basic C Operators

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Programming in C++ 6. Floating point data types

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating Point Numbers

Floating Point Numbers

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Logical and Bitwise Expressions

Work relative to other classes

Basics of Programming

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

COMP 122/L Lecture 2. Kyle Dewey

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Programming in C++ 5. Integral data types

Creating, Compiling and Executing

Type Conversion. and. Statements

What Every Programmer Should Know About Floating-Point Arithmetic

CS102: Variables and Expressions

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

NAME: int x=7; int y=9; int z=10*(x/y); assert(z>0);

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

Java Fall 2018 Margaret Reid-Miller

Computers Programming Course 6. Iulian Năstac

Arithmetic Expressions in C

ANSI C Programming Simple Programs

printf( Please enter another number: ); scanf( %d, &num2);

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

Lecture 1. Types, Expressions, & Variables

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Chapter 2: Using Data

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Transcription:

Data Conversion software diversity in action 1

What happens when types are mixed? Mixed Type Expressions int x; float y; x=y*x; Assignment Statements int x; float y; x=y*3.0; Argument Evaluation int myfn(float x); int y=myfn(3); Explicit Casting int x=7; float y = ((float)x)/3; 2

C Automatic type conversion rules In an expression, C converts all components in that expression to the most general type, and then evaluates the expression using that general type In an assignment (or argument evaluation), C converts the value of the expression to the type of the receiver C converts expressions with a valid explicit cast 3

Generality of Numeric Types Most General char unsigned char short unsigned short int unsigned int long unsigned long float double Least General 4

Converting Signed vs Unsigned Bits stay exactly the same, but the bits are INTERPRETTED differently 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 char x = -22; unsigned char y=x; printf( y=%d\n,y); // prints y=234 unsigned char w = 234; char z=w; printf( z=%d\n,z); // prints z=-22

Converting Signed vs Unsigned Bits stay exactly the same, but the bits are INTERPRETTED differently 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 char x = -22; unsigned char y=x; printf( y=%d\n,y); // prints y=234 unsigned char w = 234; char z=w; printf( z=%d\n,z; // prints z=-22

Changing Integer Size Truncate or Pad on left with sign bit 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 2 15 2 14 2 13 2 12 2 11 2 10 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 char x = -22; short y=x; short w = -22; char z=w;

Changing Integer Size (unsigned) Truncate or Pad on left with sign bit (=0) 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 0 1 0 1 0 2 15 2 14 2 13 2 12 2 11 2 10 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 0 unsigned char x = 234; unsigned short y=x; unsigned short w = 234; unsigned char z=w;

Integer to Float Add.0 and convert to nearest floating point representation int x = 1331254215; float y=x; printf("y=%f\n",y); // prints y=1331254272.000000

Float to Integer Truncate at the decimal point (round towards zero) float w=-374289.74112; int z=w; printf("z=%d\n",z); // prints z=-374289

Conversion Errors When C converts a negative signed number to a positive number char x = -1; unsigned char y = x; printf( y = %d\n,y); When C converts a wide number to a smaller width, but the number doesn t fit short x=260; char y = x; printf( y = %d\n,y); When C truncates decimals float x=2.7; int y=x;; printf( y = %d\n,y);

Integer Division Pitfall int atbats = atoi(argv[1]); int hits = atoi(argv[2]); float battingaverage = (hits/ atbats) * 1000; printf( Everybody has a zero batting average?\n );

Integer Division Pitfall // Convert 120 km/hr to meters per second float vterminal = 120 * (3600 / 1000); printf( Terminal velocity in m/s is %f\n,vterminal); // WE CAN T MOVE OUR TERMINAL VELOCITY IS 0!

Unsigned Pitfall unsigned int width=+8; signed int leftx = -13; if ((leftx < 0) && (leftx + width) > 0) { printf( Rectangle crosses y axis\n ); } what is the most general type in this expression?

Explicit Casting Programmer tells C explicitly to perform conversion cast prefix operator: (type)expression Causes expression to be evaluated and then converted to the specified type Needed when the programmer knows better than the compiler! Note that casting is pretty high in operator precedence after parens, but before any mathematical operations int battingaverage = ((float) hits/ atbats) * 1000; 15

Resources Programming in C, Chapter 3, 13 (pp 325-328) Wikipedia Type Conversion: https://en.wikipedia.org/wiki/type_conversion C Tutorial Cast operator: http://www.crasseux.com/books/ctutorial/the-castoperator.html#the%20cast%20operator 16