SESSION 5 INTEGER REPRESENTATION

Similar documents
The string of digits in the binary number system represents the quantity

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Computer Science 281 Binary and Hexadecimal Review

Lecture 2. Binary and Hexadecimal Numbers

Binary Adders: Half Adders and Full Adders

COMBINATIONAL CIRCUITS

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Solution for Homework 2

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

A Short Guide to Significant Figures

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Lecture 8: Binary Multiplication & Division

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Systems I: Computer Organization and Architecture

Chapter 1: Digital Systems and Binary Numbers

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Opposites are all around us. If you move forward two spaces in a board game

CSI 333 Lecture 1 Number Systems

Number and codes in digital systems

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

HOMEWORK # 2 SOLUTIO

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CHAPTER 5 Round-off errors

Number Systems and Radix Conversion

Integer Operations. Overview. Grade 7 Mathematics, Quarter 1, Unit 1.1. Number of Instructional Days: 15 (1 day = 45 minutes) Essential Questions

NUMBER SYSTEMS. 1.1 Introduction

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Binary Representation

Let s put together a Manual Processor

Circuits and Boolean Expressions

Year 9 set 1 Mathematics notes, to accompany the 9H book.

MATH-0910 Review Concepts (Haugen)

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Two's Complement Adder/Subtractor Lab L03

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

Binary Numbers. Binary Octal Hexadecimal

Mathematics Success Grade 6

Understanding Logic Design

Adding and Subtracting Positive and Negative Numbers

1 Description of The Simpletron

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Measures of Error: for exact x and approximation x Absolute error e = x x. Relative error r = (x x )/x.

2011, The McGraw-Hill Companies, Inc. Chapter 3

Multiplying and Dividing Signed Numbers. Finding the Product of Two Signed Numbers. (a) (3)( 4) ( 4) ( 4) ( 4) 12 (b) (4)( 5) ( 5) ( 5) ( 5) ( 5) 20

DEPARTMENT OF INFORMATION TECHNLOGY

Session 29 Scientific Notation and Laws of Exponents. If you have ever taken a Chemistry class, you may have encountered the following numbers:

=

CPEN Digital Logic Design Binary Systems

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

An Efficient RNS to Binary Converter Using the Moduli Set {2n + 1, 2n, 2n 1}

Number Representation

CS321. Introduction to Numerical Methods

Base Conversion written by Cathy Saxton

MEP Y9 Practice Book A

Accentuate the Negative: Homework Examples from ACE

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

The Mathematics 11 Competency Test Percent Increase or Decrease

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Counters and Decoders

MACHINE INSTRUCTIONS AND PROGRAMS

RN-coding of Numbers: New Insights and Some Applications

Definition 8.1 Two inequalities are equivalent if they have the same solution set. Add or Subtract the same value on both sides of the inequality.

2.6 Exponents and Order of Operations

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Properties of Real Numbers

EXPERIMENT 4. Parallel Adders, Subtractors, and Complementors

Lies My Calculator and Computer Told Me

Course Syllabus. MATH 1350-Mathematics for Teachers I. Revision Date: 8/15/2016

Using Algebra Tiles for Adding/Subtracting Integers and to Solve 2-step Equations Grade 7 By Rich Butera

Appendix A Installation of Visual C++ and MASM

Objective. Materials. TI-73 Calculator

Sistemas Digitais I LESI - 2º ano

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Addition Methods. Methods Jottings Expanded Compact Examples = 15

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

Chapter 4 Register Transfer and Microoperations. Section 4.1 Register Transfer Language

Chapter 2. Binary Values and Number Systems

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Chapter 4: Computer Codes

CS201: Architecture and Assembly Language

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

Subnetting Examples. There are three types of subnetting examples I will show in this document:

Commutative Property Grade One

Lecture 15. IP address space managed by Internet Assigned Numbers Authority (IANA)

Grade 6 Math Circles. Binary and Beyond

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

JobTestPrep's Numeracy Review Decimals & Percentages

25 Integers: Addition and Subtraction

Tom wants to find two real numbers, a and b, that have a sum of 10 and have a product of 10. He makes this table.

Transcription:

SESSION 5 INTEGER REPRESENTATION Reading: Section 2.4 Robert F. Kelly, 2012-2016 2 Objectives Understand the fundamentals of integer data representation and manipulation in digital computers Gain experience in working with binary numbers Understand the difference between a number and the representation of that number in a computer Understand how errors can occur in computations because of overflow and truncation 1

Robert F. Kelly, 2012-2016 3 Number Representation Understanding the computer representation (in 0s and 1s) helps you: Understand the limitations of a type (e.g. maximum value, precision) E.g., an 8-bit integer can only represent 256 integer values Debug problems by reading the actual representation of the data Understand implications of computer architecture choices Better read hardware specs Understand issues in porting code to a different computer Robert F. Kelly, 2012-2016 4 Background Computers are limited in how they can represent data Example 1/3 can be represented mathematically, but in a computer is it.3 or.33 or.333 or.3333 or Some numbers can be represented exactly, others cannot 2

Robert F. Kelly, 2012-2016 5 Signed Integer Representation The high-order bit usually indicates the sign of an integer The high-order bit is the leftmost bit (also called the most significant bit or sign bit) 0 is used to indicate a positive number; 1 indicates a negative number 1000 0000 0000 0001 The remaining bits contain the value of the number (but this can be interpreted in different ways) You will see machine instructions that examine the sign bit of an integer Robert F. Kelly, 2012-2016 6 Integer Representations There are three ways in which signed binary integers may be expressed: Signed magnitude concerning the best One s complement representation Two s complement (most common) In an 8-bit integer, signed magnitude representation places the absolute value of the number in the 7 bits to the right of the sign bit We mainly cover 2 s complement representation (text covers all 3) Years ago there was disagreement 3

Robert F. Kelly, 2012-2016 7 Signed Magnitude For example, in 8-bit signed magnitude representation: +3 is: 00000011-3 is: 10000011 This is an intuitive representation for humans, but not ideal for computers Robert F. Kelly, 2012-2016 8 Complement - Terminology General term that means something that completes Color a color, which when combined with its complement forms black (or white) Music an interval, which when combined with its complement forms an octave Geometry an angle, which when combined with its complement, forms a right angle Computer circuits can form a complement very easily 4

Robert F. Kelly, 2012-2016 9 Complement Systems In arithmetic, we just represent a negative number with a minus sign in front of the number In computers, we represent a negative number with a different set of bits so that we can use an adder to add a positive and a negative number We still would like to access a sign bit to easily determine if a number is negative Robert F. Kelly, 2012-2016 10 Complement Systems More general representation of binary numbers Subtraction can be thought of as the addition of a positive number with the complement of the subtrahend E.g., 48-19 is the same as 48 + (-19) Negative values are represented by some difference between a number and its base minuend-subtrahend = difference If we represent a negative number as a complement, the addition circuit can be used for both addition and subtraction 5

Robert F. Kelly, 2012-2016 11 Computing the Complement Computing a bit sequence whose complement contains the opposite bit at each position 0101 1100 Computer circuits can form 1010 0011 a complement very easily Sometimes referred to as flipping the bits Form the complement by changing the value of each bit The computer can flip each bit position without waiting for others Robert F. Kelly, 2012-2016 12 One s Complement One s complement of a binary number represents the negation of that number Behaves like the negative of the original number in most arithmetic operations It amounts to little more than flipping the bits of a binary number 0000 0001 represents 1 1111 1110 represents -1 What do you get if you add these two binary numbers? Definition - Wikipedia 6

Robert F. Kelly, 2012-2016 13 One s Complement For example, using one s complement in an 8-bit representation: + 3 is: 0000 0011-3 is: 1111 1100 In one s complement representation, as with signed magnitude, negative values are indicated by a 1 in the high order bit If we add the binary representation of +3 and -3 we get 1111 1111 or negative zero Why do we refer to 1111 1111 as negative zero? Robert F. Kelly, 2012-2016 14 One s Complement Subtraction The difference of two values is found by adding the minuend to the complement of the subtrahend E.g., 48-19 is the same as 48 + (-19) 48 is the minuend and 19 is the subtrahend First form the complement on the number to be subtracted, then add the minuend to the complement Complement circuitry allows the adder to be used for subtraction 7

Robert F. Kelly, 2012-2016 15 Use of Carry in Addition Decimal Binary 1 2345 1272 3617 1 1011 0010 1101 carry When a pair of digits equals or exceeds the base number, you mark a carry Robert F. Kelly, 2012-2016 16 One s Complement Addition The final carry bit is carried around and added to the sum Example: Using one s complement binary arithmetic, find the sum of 48 and -19 48-19 Can you verify that this is correct? 19 in binary is: 0001 0011-19 in one s complement is: 1110 1100 8

Robert F. Kelly, 2012-2016 17 Transformation to Integer The result of your addition was a one scomplement number In the computer this is OK since integers are stored as one s-complement To verify the value in a hand calculation, you can transform to decimal integer Robert F. Kelly, 2012-2016 18 Are We on Track? Perform one s complement subtraction (53-23) Express 53 and 23 in binary Form the one s complement of 23 Add the two binary numbers Express the result in decimal Check with your decimal subtraction Use 8 bits to represent each number 9

Robert F. Kelly, 2012-2016 19 Were We on Track? Using one s complement binary arithmetic, find the sum of -23 and 53 53 in binary is: 00110101, 23 in binary is: 00010111-23 in one s complement is: 11101000 00011101 00000001 (add carry) 00011110 (30) Addition in one s complement 00110101 (53) 11101000 (-23) 00011101 (plus carry) We add the carry to the result Robert F. Kelly, 2012-2016 20 Comments One s complement: is simpler to implement than signed magnitude has the disadvantage of having two different representations for zero: positive zero and negative zero 10

Robert F. Kelly, 2012-2016 21 Multiple Representations of Zero In one s complement, zero is represented as 1111 1111 and 0000 0000 Can cause problems or complications in some computer operations Robert F. Kelly, 2012-2016 22 Two s Complement Representation Defined as the complement with respect to 2 N (the result of subtracting the number from 2 N ) To express a value in two s complement representation: If the number is positive, just convert it to binary If the number is negative, find the one s complement of the number and then add 1 Example: In 8-bit binary, 3 is: 0000 0011-3 using one s complement representation is 1111 1100 For 2 s complement, add 1: 1111 1100 + 1 = 1111 1101 11

Robert F. Kelly, 2012-2016 23 Two s Complement Arithmetic Add our two binary numbers Discard any carry emitting from the high order bit Example: Using one s complement binary arithmetic, find the sum of 48 and -19 19 in binary is 00010011, -19 using one s complement is 11101100, and -19 using two s complement is 11101101 Robert F. Kelly, 2012-2016 24 Transformation to Integer The result of your addition was a two scomplement number In the computer this is OK since integers are stored as two s-complement To verify the value using a hand calculation, you can transform to integer If the number has 0 in the sign bit, no change is needed If the number has 1 in the sign bit, subtract 1 from the result, then reverse the bits Think of this as just doing the reverse of what you did to form the 2 s complement 12

Robert F. Kelly, 2012-2016 25 Example Using two s complement binary arithmetic, find the sum of -48 and 19 19 in binary is: 00010011, 48 in binary is: 00110000-48 in two s complement is: 11001111 +00000001 11010000 Convert result to 1 s complement 11100011-00000001 (subtract 1) 11100010 Addition in two s complement 00010011 (19) 11010000 (-48) 11100011 The result is negative, so we transform the result to integer Flipping the bits in the result above gives us 00011101 or 29, hence the result is -29 Robert F. Kelly, 2012-2016 26 Are We on Track? Perform two s complement subtraction (23-53) Express 53 and 23 in binary Form the two s complement of 53 Add the two binary numbers Express the result in decimal Check with your decimal subtraction Use 8 bits to represent each number 13

Robert F. Kelly, 2012-2016 27 Were We on Track? Using two s complement binary arithmetic, find the sum of 23 and -53 53 in binary is: 00110101, 23 in binary is: 00010111-53 in one s complement is: 11001010 so two s complement is 11001010 + 1 = 11001011 11100010-00000001 (subtract 1) 11100001 Addition in two s complement 00010111 (23) 11001011 (-53) 11100010 flip the bits of the result above to find the result 00011110 (30) Since the original result was negative the answer is -30 Robert F. Kelly, 2012-2016 28 Overflow Calculating a number that is too large or too small to be represented correctly in a computer Overflow is always possible when we use any finite number of bits to represent a number While we can t always prevent overflow, we can always detect overflow In complement arithmetic, an overflow condition is easy to detect 14

Robert F. Kelly, 2012-2016 29 Overflow in Java Example causes an overflow of a short Notice that the overflow causes a wrap Robert F. Kelly, 2012-2016 30 Integer Range Example: Using two s complement binary arithmetic, find the sum of 107 and 46 We see that the nonzero carry from the seventh bit overflows into the sign bit, giving us the erroneous result: 107 + 46 = -103. Ranges One s: (2 N 1 1) to +(2 N 1 1) Two s: (2 N 1 ) to +(2 N 1 1) What is the maximum number of distinct integers you can represent with 8 bits? What is the range of a 8-bit integer in two s complement? 15

Robert F. Kelly, 2012-2016 31 Detecting Overflow Example: Using two s complement binary arithmetic, find the sum of 23 and -9 We see that there is carry into the sign bit and carry out. The final result is correct: 23 + (-9) = 14. Overflow into the sign bit does not always mean that we have an error Rule for detecting signed two s complement overflow: When the carry in and the carry out of the sign bit differ, overflow has occurred. If the carry into the sign bit equals the carry out of the sign bit, no overflow has occurred Robert F. Kelly, 2012-2016 32 Did You Satisfy the Objectives? Understand the fundamentals of integer data representation and manipulation in digital computers Gain experience in working with binary numbers Understand the difference between a number and the representation of that number in a computer Understand how errors can occur in computations because of overflow and truncation 16