Main Memory Organization

Similar documents
Exercise: Using Numbers

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Computer System and programming in C

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

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

Objectives. In this chapter, you will:

Reserved Words and Identifiers

Creating a C++ Program

Lecture 2 Tao Wang 1

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

C++ Programming: From Problem Analysis to Program Design, Third Edition

Programming in C++ 5. Integral data types

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

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

CSc Introduction to Computing

Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

Unit 3. Constants and Expressions

Declaration and Memory

Another Simple Program: Adding Two Integers

Lecture 3 Tao Wang 1

Programming in C++ 6. Floating point data types

UNIT- 3 Introduction to C++

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

CS61B Lecture #14: Integers

1.3b Type Conversion

LECTURE 3 C++ Basics Part 2

Chapter 2: Overview of C++

How a computer runs a program. Binary Representa8on and Opera8ons on Binary Data. Opera8ng System 9/17/13. You can view binary file contents

Operators and Expressions:

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

CHAPTER 3 Expressions, Functions, Output

Floats are not Reals. BIL 220 Introduc6on to Systems Programming Spring Instructors: Aykut & Erkut Erdem

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

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

1. In C++, reserved words are the same as predefined identifiers. a. True

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

Chapter 6 Primitive types

Chapter 2: Introduction to C++

A First Program - Greeting.cpp

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

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

Chapter 3: Operators, Expressions and Type Conversion

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

REVIEW. The C++ Programming Language. CS 151 Review #2

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

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

CS242 COMPUTER PROGRAMMING

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Chapter 7 Arithmetic

Integer Data Types. Data Type. Data Types. int, short int, long int

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Unit 3. Operators. School of Science and Technology INTRODUCTION

COMP2611: Computer Organization. Data Representation

DEPARTMENT OF MATHS, MJ COLLEGE

LECTURE 02 INTRODUCTION TO C++

Full file at

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 2 Basic Elements of C++

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

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

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

Full file at

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

UNIT IV: DATA PATH DESIGN

Basic Operations jgrasp debugger Writing Programs & Checkstyle

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Outline. Data and Operations. Data Types. Integral Types

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Arithmetic type issues

Lecture 3. More About C

Chapter 4. Operations on Data

Data Representa+on: Floa+ng Point Numbers

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

More Programming Constructs -- Introduction

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

6.096 Introduction to C++ January (IAP) 2009

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

CS2141 Software Development using C/C++ C++ Basics

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

Relational Operators and if. Class 10

Objectives. Data Types (continued) Data Types 4. การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Language Reference Manual

Variables and Constants

Chapter 7. Basic Types

ESc101 : Fundamental of Computing

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

Chapter 2: Using Data

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Transcription:

Main Memory Organization Bit Smallest piece of memory Stands for binary digit Has values 0 (off) or 1 (on) Byte Is 8 consecu>ve bits Word Usually 4 consecu>ve bytes Has an address 8 bits 0 1 1 0 0 1 1 1 1 byte 1-1

Data Storage All data is stored in binary bits in main memory The meaning of a set of bits depends on the encoding Encoding determines the length of a meaningful chunk We use bytes (8-bits) as our most common unit Encoding also determines how those bits should be read 8-bit Binary 8-bit Integer 8-bit Character (ASCII) 0100 0001 65 A 0100 0010 66 B 0100 0011 67 C

Simple Data Types Data type: set of values together with a set of opera>ons C++ data types fall into three categories: Including Data Structures, Fourth Edi>on 3

Simple Data Types Three categories of simple data Integral: integers (numbers without a decimal) Floa>ng-point: decimal numbers Enumera>on type: user-defined data type Including Data Structures, Fourth Edi>on 4

Simple Data Types (continued) Integral data types are further classified into the following categories: 5

int Data Type Examples: -6728 0 78 +763 Posi>ve integers do not need a + sign No commas are used within an integer Commas are used for separa>ng items in a list Including Data Structures, Fourth Edi>on 6

char Data Type The smallest integral data type Used for characters: leyers, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0', '*', '+', '$', '&',' ' A blank space is a character Including Data Structures, Fourth Edi>on 7

bool Data Type bool type Two values: true and false Manipulate logical (Boolean) expressions true and false are called logical values bool, true, and false are reserved words Including Data Structures, Fourth Edi>on 8

Simple Data Types Different compilers may allow different ranges of values Including Data Structures, Fourth Edi>on 9

Floating-Point Data Types C++ uses scien>fic nota>on to represent real numbers (floa>ng-point nota>on) Including Data Structures, Fourth Edi>on 10

Floating-Point Data Types (continued) float: represents any real number Range: -3.4E+38 to 3.4E+38 (four bytes) double: represents any real number Range: -1.7E+308 to 1.7E+308 (eight bytes) On most newer compilers, data types double and long double are same Including Data Structures, Fourth Edi>on 11

Floating-Point Data Types (continued) Maximum number of significant digits (decimal places) for float values is 6 or 7 Maximum number of significant digits for double is 15 Precision: maximum number of significant digits Float values are called single precision Double values are called double precision Including Data Structures, Fourth Edi>on 12

Arithmetic Operators C++ arithme>c operators: + Addi>on - Subtrac>on * Mul>plica>on / Division % Modulus +, -, *, and / can be used with integral and floa>ngpoint data types % can be used with integral data types only These are binary operators They operate on 2 operands 13

Order of Precedence All opera>ons inside of () are evaluated first *, /, and % are at the same level of precedence and are evaluated next + and have the same level of precedence and are evaluated last When operators are on the same level Performed from lef to right (associa>vity) 3 * 7-6 + 2 * 5 / 4 + 6 means (((3 * 7) 6) + ((2 * 5) / 4 )) + 6 Including Data Structures, Fifth Edition 14

Overflow and Underflow Occurs when assigning a value that is too large (overflow) or too small (underflow) to be held in a variable Variable contains value that is wrapped around the set of possible values 3-15

Overflow Example // Create a short int initialized to // the largest value it can hold short int num = 32767; cout << num; // Displays 32767 num = num + 1; cout << num; // Displays -32768 3-16

Handling Overflow and Underflow Different systems handle the problem differently. They may display a warning / error message display a dialog box and ask what to do stop the program con>nue execu>on with the incorrect value 3-17

Expressions En>re expression is evaluated according to precedence rules If all operands are integers Expression is called an integral expression Yields an integral result Example: 2 + 3 * 5 If all operands are floa>ng-point Expression is called a floa>ng-point expression Yields a floa>ng-point result Example: 12.8 * 17.5-34.50 Including Data Structures, Fifth Edition 18

Mixed Expressions Mixed expression: Has operands of different data types Contains integers and floa>ng-point Examples of mixed expressions: 2 + 3.5 6 / 4 + 3.9 5.4 * 2 13.6 + 18 / 2 Including Data Structures, Fifth Edition 19

Mixed Expressions (cont'd.) Data types are ranked by largest value they can hold 20

Mixed Expressions (cont'd.) Evalua>on rules: If expression has different types of operands char, short, unsigned short are automa>cally promoted to int All operands are promoted to the data type with the highest rank in the expression When using the = operator, the type of the expression on right will be converted to type of the variable on lef (promo>on or demo>on) Including Data Structures, Fifth Edition 21

Data Types and Conversion The data type assigned to a literal or variable tells the computer how it is encoded That is, how to interpret it You can also tell the computer to treat a literal or variable with a different encoding This is called type cas0ng or type coercion static_cast<datatypename>( expression )

Rounding off a number Suppose we need to round a real number to the nearest hundredth (2 decimal digits), we could do it by using the following procedure: Number to be rounded off: 2.3449 1) Move decimal point 2 places to the right -> 2.3449 * 100.0 => 234.49 2) Add 0.5 -> 234.49 + 0.5 => 234.99 3) Truncate the result -> => 234 4) Convert it to real again -> => 234.0 5) Divide it by 100.0 -> 234.0 / 100.0 => 2.34 Suppose we have a value in a variable called length (type double) and we want to round it off, we could use the following expression to implement the steps shown above: length = static_cast<double>( static_cast<int>( (length * 100.0) + 0.5 )) /100.0 23