Machine Arithmetic 8/31/2007

Similar documents
Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

ECE232: Hardware Organization and Design

IEEE Standard for Floating-Point Arithmetic: 754

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Organisasi Sistem Komputer

Introduction to Computer Systems Recitation 2 May 29, Marjorie Carlson Aditya Gupta Shailin Desai

Chapter 3: Arithmetic for Computers

Bits, Words, and Integers

Chapter Three. Arithmetic

Signed Multiplication Multiply the positives Negate result if signs of operand are different

CS101 Lecture 04: Binary Arithmetic

Numerical computing. How computers store real numbers and the problems that result

Divide: Paper & Pencil

Introduction to Computers and Programming. Numeric Values

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

MA 1128: Lecture 02 1/22/2018

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Number Systems. Both numbers are positive

COMP2611: Computer Organization. Data Representation

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

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Module 2: Computer Arithmetic

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

Data Representation 1

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

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

Floating Point Arithmetic

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

unused unused unused unused unused unused

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

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

Floating Point. What can be represented in N bits? 0 to 2N-1. 9,349,398,989,787,762,244,859,087, x 1067

CS367 Test 1 Review Guide

Inf2C - Computer Systems Lecture 2 Data Representation

COMP Overview of Tutorial #2

The type of all data used in a C (or C++) program must be specified

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

3.5 Floating Point: Overview

Representing numbers on the computer. Computer memory/processors consist of items that exist in one of two possible states (binary states).

Floating Point Arithmetic

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Signed Binary Numbers

Calculations with Sig Figs

Integers and Floating Point

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Floating Point Numbers. Lecture 9 CAP

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

More about Binary 9/6/2016

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

A Level Computing. Contents. For the Exam:

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Chapter 4: Data Representations

Topic Notes: Bits and Bytes and Numbers

Floating Point. EE 109 Unit 20. Floating Point Representation. Fixed Point

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

Introduction to Scientific Computing Lecture 1

Logic, Words, and Integers

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

EE 109 Unit 20. IEEE 754 Floating Point Representation Floating Point Arithmetic

What Every Programmer Should Know About Floating-Point Arithmetic

Up next. Midterm. Today s lecture. To follow

FLOATING POINT NUMBERS

Computer Systems C S Cynthia Lee

Topic Notes: Bits and Bytes and Numbers

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Number Systems and Computer Arithmetic

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

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

SIMPLE PROGRAMMING. The 10 Minute Guide to Bitwise Operators

Foundations of Computer Systems

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

Representing and Manipulating Floating Points. Jo, Heeseung

Time (self-scheduled): Location Schedule Your Exam: What to bring:

CO212 Lecture 10: Arithmetic & Logical Unit

Objectives. Connecting with Computer Science 2

Systems Programming and Computer Architecture ( )

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Programming Lecture 3

ECE 30 Introduction to Computer Engineering

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Data Representation Floating Point

COMP2121: Microprocessors and Interfacing. Number Systems

Number Systems and Number Representation

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

3.1 DATA REPRESENTATION (PART C)

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Data Representation Floating Point

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Operators in C. Staff Incharge: S.Sasirekha

Transcription:

Machine Arithmetic 8/31/2007 1

Opening Discussion Let's look at some interclass problems. If you played with your program some you probably found that it behaves oddly in some regards. Why is this? What did we talk about last class? 2

Output with printf We have seen printf, but we haven't really used the f part of it. Let's look at the man page for printf and see if we can figure out what it is saying. Let's play with printf some. We can also use the sizeof operator to learn a bit more about the primitive types on our system. 3

Input with scanf The opposite of printf is scanf. Use this function when you want to read input from the user. Let's look at the man page for scanf. At this point, all the variables you pass to scanf will need to have & in front of them. This is because scanf needs to know where the variable is in memory and that is what & does. We'll go into more detail about this when we cover pointers. 4

I/O Redirection in Linux There is another feature of the command line interface that provides tremendous power that we haven't discussed yet: I/O redirection. Not only does this make the command line more powerful, it will be really helpful on some assignments. You can make a program use a file as standard input or standard output. command < input > output You don't have to specify both. The output of one program can be piped so become the input of another program with. 5

Numbers on Machines We've already said that computers use binary. 8 binary digits, bits, are called a byte. The machine's native size chunk of memory is called a word. These machines have a 32-bit word. If you have a 64-bit machine it will use a 64-bit word. We can use the sizeof operator in C to look at the sizes of the different primitive types. What it reports is a multiple of the size of a char which is normally a single byte. 6

Binary Addition Adding binary numbers is quite simple. You just do a lot of carrying. Let's do some examples. We'll assume we are working with 8-bit numbers. The number of bits we have can matter. 7

Twos Complement and Subtraction Subtraction is just addition of the negative. So how do we make negatives in binary? Remember, you don't have a -. You only have 0 and 1. There are actually three different ways that negatives have been done in binary. Your book covers all three, but I will only discuss the one that computers today use. Twos compliment is based on the idea that a+(- a)=0. So we define -a to be the number such that when added to a we get zero. This is possible because we have a finite number of bits. Your book discusses an easy shortcut. 8

Bit Shifting In decimal when you multiply/divide by a factor of 10 you are just moving the decimal point around. In binary the operation of moving all the bits left or right is called bit shifting and it is equivalent to multiplication or division by powers of two. 9

Multiplication To multiply two binary numbers do standard long multiplication. Just remember when you are adding that you are adding binary numbers. Note that adding more than two binary numbers can get tricky. It can result in carrying operations you have never done by hand in decimal. 10

Binary Fractions If the digits in binary start with one and move to larger powers of two moving to the left, what happens if you put in a binary point and move to the right? Numbers that have a fixed number of bits with a point at a selected position are called fixed point numbers. These were popular before floating point accelerators became common, but now they are only used on specialty hardware with low end chips. 11

Floating Point Numbers Numbers that can have fractional values are represented with floating point numbers on virtually all modern machines. Floating point numbers are basically like scientific notation in binary. v=(-1) s * (1+frac) * 2 (exp-bias) Single precision numbers use one bit for the sign, 8 for the exponent, 23 for the fractional part, and have a bias of 127. Double precision uses one bit for sign, 11 for exponent, 52 for fraction, and has a bias of 1023. 12

Limitations of FP Floating point numbers are not actually the real numbers you know from math even though we often use them that way. They have limitations because of the fixed number of bits. Some of the math properties you are used to for numbers don't actually hold for floating point numbers. Example: (a+b)+c=a+(b+c) Try this with a, b, and c as floats and make a=1e7, b=- 1e7, c=0.01. You also have to be careful of things like subtracting two numbers that are almost equal. 13

Minute Essay Give me the 8-bit representation of -5. Interclass Problem Write a program to demonstrate potential problems with floating point numbers. I suggest using floats instead of doubles to make it easier. There is a quiz on Wednesday after the long weekend. It can cover anything that we have talked about in class or in the readings. 14