CS101 Lecture 04: Binary Arithmetic

Similar documents
Chapter 3: Arithmetic for Computers

Chapter Three. Arithmetic

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

Floating Point Arithmetic

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

Introduction to Computers and Programming. Numeric Values

World Inside a Computer is Binary

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014


Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

Chapter 2 Bits, Data Types, and Operations

Number System. Introduction. Decimal Numbers

CS 101: Computer Programming and Utilization

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

Floating Point Arithmetic

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

COMP2611: Computer Organization. Data Representation

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

Chapter 2 Bits, Data Types, and Operations

Inf2C - Computer Systems Lecture 2 Data Representation

Signed umbers. Sign/Magnitude otation

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

Digital Fundamentals

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

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Chapter 4. Operations on Data

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Floating Point Numbers. Lecture 9 CAP

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

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

Number Systems. Both numbers are positive

COMP Overview of Tutorial #2

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

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

Introduction to Computer Science-103. Midterm

Numeric Encodings Prof. James L. Frankel Harvard University

Divide: Paper & Pencil

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

T02 Tutorial Slides for Week 2

Chapter 10 Binary Arithmetics

CHW 261: Logic Design

Numerical Representations On The Computer: Negative And Rational Numbers

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

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

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

Chapter 3: part 3 Binary Subtraction

Numerical Representations On The Computer: Negative And Rational Numbers

Computer Organisation CS303

Module 2: Computer Arithmetic

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

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

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

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

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

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Foundations of Computer Systems

LABORATORY. 3 Representing Numbers OBJECTIVE REFERENCES. Learn how negative numbers and real numbers are encoded inside computers.

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

CS & IT Conversions. Magnitude 10,000 1,

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Number representations

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

In this lesson, we will use the order of operations to evaluate and simplify expressions that contain numbers and variables.

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

MACHINE LEVEL REPRESENTATION OF DATA

Introduction to Scientific Computing Lecture 1

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

ecture 25 Floating Point Friedland and Weaver Computer Science 61C Spring 2017 March 17th, 2017

Signed Binary Numbers

Number Systems and Computer Arithmetic

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

CS 61C: Great Ideas in Computer Architecture Floating Point Arithmetic

ECE232: Hardware Organization and Design

Chapter 2. Data Representation in Computer Systems

CHAPTER 5: Representing Numerical Data

Advanced Computer Architecture-CS501

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

unused unused unused unused unused unused

Scientific Computing. Error Analysis

ECE260: Fundamentals of Computer Engineering

UNIT-III COMPUTER ARTHIMETIC

Lecture 25: Other Number Systems

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Floating Point Considerations

Number Systems (2.1.1)

CO212 Lecture 10: Arithmetic & Logical Unit

A Level Computing. Contents. For the Exam:

Binary Representations and Arithmetic

Binary Addition & Subtraction. Unsigned and Sign & Magnitude numbers

CSC201, SECTION 002, Fall 2000: Homework Assignment #2

Number Systems CHAPTER Positional Number Systems

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

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

Transcription:

CS101 Lecture 04: Binary Arithmetic Binary Number Addition Two s complement encoding Briefly: real number representation Aaron Stevens (azs@bu.edu) 25 January 2013 What You ll Learn Today Counting in binary Adding binary numbers Encoding negative numbers Subtraction is a special case of addition Binary floating point numbers Read this on your own if you have trouble sleeping 1

Counting Let s count in binary: 0000 0001 0010 0011 0100 0101 0110 0111 3 Recall: Add with Carry Recall how to add 2 multi-digit decimal numbers: Example: adding 35 + 77 5 + 7 is 12, or 2 with a carry of 1 Addend Values 1 1 3 5 + 7 7 1 1 2 Carry Values Sum Remember: work right to left. 4 14 2

Binary: Add with Carry Remember that there are only 2 digits in binary, 0 and 1 1 + 1 is 0 with a carry of 1 Operands! 1 1 0!! 0 1 1 1!! + 0 0 1 0!! 1 0 0 1! Carry Values Sum Hint: work right to left. 5 14 Try it: 0011 + 1010 Hint: work columns right to left. Remember to carry as needed. Operands! 0 1 0!! 0 0 1 1!! + 1 0 1 0!! 1 1 0 1! Carry Values Sum 6 3

Negative Numbers Signed-magnitude numbers The sign represents the ordering, and the digits represent the magnitude of the number. 7 Negative Numbers PROBLEM: In signed-magnitude representation, there is a plus zero and minus zero complexity. SOLUTION: Store all numbers as natural integer values, with half of them representing negative numbers 8 4

Negative Numbers An example using two decimal digits, let 0 through 49 represent 0 through 49 let 50 through 99 represent -50 through -1 Actual Number Stored Encoded Value This representation scheme is called the ten s complement. 2s Complement Two s Complement Using binary numbers, we call this the two s complement. All numbers are stored as positive binary numbers. Half are encoded to be interpreted as negative. What do you observe about the left-most bit? 10 5

2s Complement How To Calculate a Two's Complement Number First, find the equivalent binary number. If the decimal number was positive: you're done. If the decimal number was negative: invert all the bits, and add 1 (with carries as needed). Examples 25 decimal is 00011001 binary. It s positive, so all done. How about -25 decimal? Begin with binary: 00011001! Invert all the bits to get: 11100110! Add 1 to get: 11100111 (decimal value -25) 11 2s Complement Arithmetic With 2s complement, we can use addition instead of subtraction -- much easier! 123 01111011! - 25 +11100111! 98 01100010! (last bit carried out is ignored) 12 6

Number Overflow If each value is stored using eight bits, consider adding 127 to 3: 01111111!! + 00000011!! 10000010! How do we interpret the value 10000010? Adding two positive integers yields a negative integer. This is an example of overflow. 13 Number Overflow We interpret the value 10000010 as decimal number -126. How did that happen? The left-most bit is 1, so we know it is negative. The most negative signed 8-bit number is binary 10000000, which is -128 (-2 7 ) in decimal. Add binary 00000010 (2) to get 10000010, which is -126 decimal. What does one do about overflow? Prevent overflow by using a wider bit-depth. 14 7

Representing Real Numbers Real numbers A number with a whole part and a fractional part 104.32, 0.999999, 357.0, and 3.14159 For decimal numbers, positions to the right of the decimal point are the tenths position: 10-1, 10-2, 10-3 15 Floating Point Numbers A real value in base 10 can be defined by the following formula: The mantissa (or precision) is a decimal number. The representation is called floating point because the number of digits of precision is fixed but the decimal point floats. Example: 12345.67 can be expressed as 1.234567 X 10 4 16 8

Binary Floating Point Numbers Same rules apply in binary as in decimal. Decimal point is actually the radix point. Positions to the right of the radix point in binary are 2-1 (one half), 2-2 (one quarter), 2-3 (one eighth) 17 Binary Floating Point Numbers IEEE 754 A standard for representation of binary floating point numbers, as expressed by the formula: sign * mantissa * 2 exp A single-precision binary floating-point number is stored in a 32-bit word: 18 9

Binary Floating Point Numbers Decoding the example shown above: the sign is 0 this is a positive number the exponent is 11111101 the mantissa is 1.01 (binary) The represented number is therefore +1.25 2 3 = +0.15625 in decimal. 3 (decimal) 1.25 (decimal) 19 Binary Floating Point Numbers 32-bit floating point numbers The smallest non-zero positive, and largest non-zero negative numbers are: ±2 149 ±1.4012985 10 45 The largest finite positive and smallest finite negative numbers are: ±(2 128 2 104 ) ±3.4028235 10 38 20 10

Binary Floating Point Numbers IEEE 754 also defines a double-precision 64-bit binary floating point number. You can read it for yourself. MOST IMPORTANT PART IEEE754 binary floating point numbers are only approximations of decimal floating point numbers. Not all decimal floating point numbers can be exactly represented in IEEE 754. Example: 21 What You Learned Today Counting in binary Binary addition: 00000010 + 00000010 = 00000100 Two s complement (negative numbers) IEEE754: Binary Floating Point Numbers 22 11

Announcements And To Do Readings: Reed chapter 5, pp89-90 HW02 due Wednesday 1/30 Quiz 1 on Friday 2/1 23 12