Representing signed numbers in Two s Complement notation

Similar documents
ECE260: Fundamentals of Computer Engineering

CprE 281: Digital Logic

hexadecimal-to-decimal conversion

Chapter 1 CSCI

Complement Arithmetic


Hakim Weatherspoon CS 3410 Computer Science Cornell University

14:332:231 DIGITAL LOGIC DESIGN. 2 s-complement Representation

Design of Digital Circuits Reading: Binary Numbers. Required Reading for Week February 2017 Spring 2017

UNSIGNED BINARY NUMBERS DIGITAL ELECTRONICS SYSTEM DESIGN WHAT ABOUT NEGATIVE NUMBERS? BINARY ADDITION 11/9/2018

convert a two s complement number back into a recognizable magnitude.

CSE 331 Winter 2018 Homework 1

1 Modular Arithmetic Grade Level/Prerequisites: Time: Materials: Preparation: Objectives: Navajo Nation Math Circle Connection

Chapter 1 Review of Equations and Inequalities

Menu. Review of Number Systems EEL3701 EEL3701. Math. Review of number systems >Binary math >Signed number systems

We are here. Assembly Language. Processors Arithmetic Logic Units. Finite State Machines. Circuits Gates. Transistors

Combinational Logic Design Arithmetic Functions and Circuits

Supplemental Worksheet Problems To Accompany: The Pre-Algebra Tutor: Volume 2 Section 16 Solving Single Step Equations

Numbers and Arithmetic

Some Review Problems for Exam 1: Solutions

Lecture 2e Row Echelon Form (pages 73-74)

STEP Support Programme. Hints and Partial Solutions for Assignment 1

Page 1. Math Module 2: Negative Numbers, Proportions, and the Dreaded Fractions

What does such a voltage signal look like? Signals are commonly observed and graphed as functions of time:

VLSI Physical Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Math 3361-Modern Algebra Lecture 08 9/26/ Cardinality

14:332:231 DIGITAL LOGIC DESIGN. Why Binary Number System?

Multiplication of signed-operands

Homework 2 Foundations of Computational Math 1 Fall 2018

Notes for Chapter 1 of. Scientific Computing with Case Studies

EE 109 Final Don t Make the Same Mistake

CprE 281: Digital Logic

ECE/CS 250 Computer Architecture

Numbers and Arithmetic

ECE 250 / CPS 250 Computer Architecture. Basics of Logic Design Boolean Algebra, Logic Gates

Arithmetic and Error. How does error arise? How does error arise? Notes for Part 1 of CMSC 460

Introduction to Digital Logic Missouri S&T University CPE 2210 Subtractors

Physics of Everyday Phenomena. Chapter 2

Algorithms (II) Yu Yu. Shanghai Jiaotong University

A. Incorrect! This inequality is a disjunction and has a solution set shaded outside the boundary points.

CS 360, Winter Morphology of Proof: An introduction to rigorous proof techniques

Digital Systems Overview. Unit 1 Numbering Systems. Why Digital Systems? Levels of Design Abstraction. Dissecting Decimal Numbers

Modern Algebra Prof. Manindra Agrawal Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

Binary floating point

EE260: Digital Design, Spring n Digital Computers. n Number Systems. n Representations. n Conversions. n Arithmetic Operations.

Algebra 2: Semester 2 Practice Final Unofficial Worked Out Solutions by Earl Whitney

(+2) + (+7) = (+9)? ( 3) + ( 4) = ( 7)? (+2) + ( 7) = ( 5)? (+9) + ( 4) = (+5)? (+2) (+7) = ( 5)? ( 3) ( 4) = (+1)?

Numbers & Arithmetic. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See: P&H Chapter , 3.2, C.5 C.

Why Use Negative Numbers?

An Invitation to Mathematics Prof. Sankaran Vishwanath Institute of Mathematical Science, Chennai. Unit - I Polynomials Lecture 1B Long Division

Lesson 21 Not So Dramatic Quadratics

What is Binary? Digital Systems and Information Representation. An Example. Physical Representation. Boolean Algebra

University of Minnesota Department of Electrical and Computer Engineering

E40M. Binary Numbers. M. Horowitz, J. Plummer, R. Howe 1

Adding Three or More Fractions

EE 209 Logic Cumulative Exam Name:

Number Representations

MPM1D - Practice Mastery Test #6

Direct Proof MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Direct Proof Fall / 24

Data byte 0 Data byte 1 Data byte 2 Data byte 3 Data byte 4. 0xA Register Address MSB data byte Data byte Data byte LSB data byte

Self-Directed Course: Transitional Math Module 4: Algebra

CHM Units and Dimensional Analysis (r14) Charles Taylor 1/6

MITOCW ocw f99-lec17_300k

Chapter 5. Digital systems. 5.1 Boolean algebra Negation, conjunction and disjunction

Math101, Sections 2 and 3, Spring 2008 Review Sheet for Exam #2:

Math 2 Variable Manipulation Part 1 Algebraic Equations

JUST THE MATHS UNIT NUMBER 1.6. ALGEBRA 6 (Formulae and algebraic equations) A.J.Hobson

Define a rational expression: a quotient of two polynomials. ..( 3 10) (3 2) Rational expressions have the same properties as rational numbers:

Math 016 Lessons Wimayra LUY

Context-free grammars and languages

What to Add Next time you update?

Carmen s Core Concepts (Math 135)

1.2 Functions What is a Function? 1.2. FUNCTIONS 11

Modern Algebra Prof. Manindra Agrawal Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

Numbering Systems. Contents: Binary & Decimal. Converting From: B D, D B. Arithmetic operation on Binary.

Math 56 Homework 1 Michael Downs. ne n 10 + ne n (1)

We want to determine what the graph of an exponential function. y = a x looks like for all values of a such that 0 > a > 1

EE 209 Spiral 1 Exam Solutions Name:

Lecture Slides. Elementary Statistics Eleventh Edition. by Mario F. Triola. and the Triola Statistics Series 4.1-1

Chapter 1: Solutions to Exercises

ENGIN 112 Intro to Electrical and Computer Engineering

Binary Floating-Point Numbers

1 Limits and continuity

CHAPTER 2 NUMBER SYSTEMS

LESSON EII.C EQUATIONS AND INEQUALITIES

Lesson #9 Simplifying Rational Expressions

8.3 Zero, Negative, and Fractional Exponents

Physics 8 Monday, September 12, 2011

DIVIDER IMPLEMENTATION

ALU (3) - Division Algorithms

Computer Architecture, IFE CS and T&CS, 4 th sem. Representation of Integer Numbers in Computer Systems

Mechanics, Heat, Oscillations and Waves Prof. V. Balakrishnan Department of Physics Indian Institute of Technology, Madras

CprE 281: Digital Logic

ACCESS TO SCIENCE, ENGINEERING AND AGRICULTURE: MATHEMATICS 1 MATH00030 SEMESTER / Lines and Their Equations

Recitation 4: Quantifiers and basic proofs

DIRECTED NUMBERS ADDING AND SUBTRACTING DIRECTED NUMBERS

STEP Support Programme. Hints and Partial Solutions for Assignment 17

AN ALGEBRA PRIMER WITH A VIEW TOWARD CURVES OVER FINITE FIELDS

CSE 20 Discrete Math. Algebraic Rules for Propositional Formulas. Summer, July 11 (Day 2) Number Systems/Computer Arithmetic Predicate Logic

System Data Bus (8-bit) Data Buffer. Internal Data Bus (8-bit) 8-bit register (R) 3-bit address 16-bit register pair (P) 2-bit address

Transcription:

EE457 Representing signed numbers in Two s Complement notation A way to view the signed number representation in 2 s complement notation as a positional weighted coefficient system. example ( 2) = (6 4 ) = 00000 = = 0x2 7 0x2 6 0x2 5 x2 4 0x2 3 x2 2 0x2 x2 0 How to represent -2 walking 2 steps backward is same as walking 256-2 = 235 steps forward on a 256-segment circle. 235 = 28 64 32 8 2 = 00. Hence (-2) is represented 00. - 4 2 3-3 - 5 Error point: C bit is set 0-2 - 6 4 0 00 0 0 00 0 9 5 0 0 8 0 7 0000 000 000 00 000 00 00 00 00 000 0-0 - 7 UNSIGNED - 8 7 0000 000 000 SIGNED 00 000 00 00 00 00 000 0 6 6 2 2 5 3 3 5 4 4 Error Point: V bit is set / 3 C Copyright 2006 Gandhi Puvvada

We prove that the weight of the most significant bit is a -2 7 where as the rest of the weights are all powers of 2 (2 6, 2 5, 2 4, 2 3, 2 2, 2, 2 0 ). i.e. 00 = = x(-2 7 ) x(2 6 ) x(2 5 ) 0x(2 4 ) x(2 3 ) 0x(2 2 ) x(2 ) x(2 0 ) = - 28 64 32 0 8 0 2 = - 28 07 = - 2 Proof: Take some random negative number B = b 6 b 5 b 4 b 3 b 2 b b 0. To find the value of 2 s complement of B, we find (256 - B) = (255 - B) = ( - b 6 b 5 b 4 b 3 b 2 b b 0 ) = [ ( - )x2 7 ( - b 6 )x2 6 ( - b 5 )x2 5 ( - b 4 )x2 4 ( - b 3 )x2 3 ( - b 2 )x2 2 ( - b )x2 ( - b 0 )x2 0 ] Ignore ( - )x2 7 and process other terms. Gathering all s together in the above right-hand side expression, we get = [ {(x2 6 x2 5 x2 4 x2 3 x2 2 x2 x2 0 ) } - { b 6 x2 6 b 5 x2 5 b 4 x2 4 b 3 x2 3 b 2 x2 2 b x2 b 0 x2 0 } ] = [{x2 7 } - { b 6 x2 6 b 5 x2 5 b 4 x2 4 b 3 x2 3 b 2 x2 2 b x2 b 0 x2 0 }] 2 / 3 C Copyright 2006 Gandhi Puvvada

Hence the value of B = - [{x2 7 } - { b 6 x2 6 b 5 x2 5 b 4 x2 4 b 3 x2 3 b 2 x2 2 b x2 b 0 x2 0 }] = x(-2 7 ) b 6 x(2 6 ) b 5 x(2 5 ) b 4 x(2 4 ) b 3 x(2 3 ) b 2 x(2 2 ) b x(2 ) b 0 x(2 0 ) Example: Find the value of the negative number 00 (a 6-bit number represented in 2 s complement notation. -2 5 2 4 2 3 2 2 2 2 0-32 6 8 4 2 0 0 = (-32) (8 4 2) = (-32) (4) = -8 Verify. Negate the number and find the value of the corresponding number. Flip bits: 0 0 0 0. Add : 0 0 0 0. Find value: 6 2 = 8. Hence the give number is - 8. ======================================================================== 3 / 3 C Copyright 2006 Gandhi Puvvada

Addition of two numbers expressed in two s complement notation: S = A B; A = a 3 a 2 a a 0, B = b 3 b 2 b b 0, S = s 3 s 2 s s 0 We note that the positional values are (-2 3 ), (2 2 ), (2 ), and (2 0 ). The carry from lower three bits has a weight of (2 3 ) but the column into which it is going has a weight of (-2 3 ). Total of 8 cases. Four cases are a 3 b 3 = 00, 0, 0,. For each of these four cases, consider two cases: C 3 = a or a 0. 0a 2 a a 0 0a 2 a a 0 a 2 a a 0 a 2 a a 0 0b 2 b b 0 b 2 b b 0 0b 2 b b 0 b 2 b b 0 Hypothesis #: A number added to a negative number does not cause overflow. -2 3 0 a 2 a a 0 b 2 b b 0? s 2 s s 0 Since there is a already in in the MSB column, C4 follows C3. V = C3 () C4 = 0 If a carry C3 comes into the MSB column, it has a value of 2 3. And it will cancel the -2 3 resulting in a 0 for s3. Otherwise s3 will be a. Either way the values were preserved properly. 4 / 3 C Copyright 2006 Gandhi Puvvada

Hypothesis #2: A number added to another number causes overflow if C3 is true. -2 3 0 a 2 a a 0 0 b 2 b b 0? s 2 s s 0 C4 is surely a zero here. If C3 =, we have overflow. V = C3 () C4 = = overflow If a carry C3 comes into the MSB column, it has a value of 2 3. Since it results in making s3 a, which is interpreted as -2 3 it causes an overflow. Hypothesis #3: A negative number added to another negative number causes overflow if C3 is NOT true. -2 3 a 2 a a 0 b 2 b b 0 C4 is surely a one here. If C3 = 0, we have overflow. V = C3 () C4 =? s 2 s s 0 0 = overflow If a carry C3 comes into the MSB column, then s3 comes out to be a, which matches with the mathematical sum (2 3 ) (-2 3 ) (-2 3 ) = (-2 3 ). Absence of C3 results in overflow. 5 / 3 C Copyright 2006 Gandhi Puvvada

Subtraction A-B where A and B are expressed in two s complement notation: D = A - B; A = a 3 a 2 a a 0, B = b 3 b 2 b b 0, D = d 3 d 2 d d 0 (A - B) = (A B ), more precisely (A B 000). 8 cases. Four cases are a 3 b 3 = 00, 0, 0,. For each of these four cases, consider two cases: C 3 = a or a 0 while performing (A B 000). 0a 2 a a 0 0a 2 a a 0 a 2 a a 0 a 2 a a 0 0b 2 b b 0 b 2 b b 0 0b 2 b b 0 b 2 b b 0 An observation about 2 s complement notation. Given X ( or negative), we find the representation for -X by performing X, but... the 0000 and 000 are special cases. 0000 is considered to be a zero and there is no representation for a negative zero in 2 s complement notation, which is actually good! If you perform X, then you get ( 000) = 0000. Note that ( 000) contains a (-2 3 ) and also contains (2 2, 2, 2 0, and 2 0 ). Similarly 000 is also special. 000 is a -8, but if you negate it by doing X, you get (0 000) = 000. You get 000 again and it is NOT 8. In fact we caused an overflow in this addition. However please notice that the expression (0 000) as a whole still represents 8, as it has (2 2, 2, 2 0, and 2 0 ). 6 / 3 C Copyright 2006 Gandhi Puvvada

So, are we saying that we can NOT perform (A B ) for (A - B) if B is either 0000 or 000? No, no, we did not say that. In fact subtracting 0000 will never cause any problem. If you are subtracting 000, as long as you do not perform (A B ) in two steps [ (A) (B ) ] (i.e. as long as you add the three items all in one stroke), the procedure is still fine and there will not be any spurious overflow. Conclusion: Always perform (A B ) in one stroke. Hypothesis #: Subtracting a number from another number does not cause overflow. - 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0 0 a 2 a a 0 b 2 b b 0? d 2 d d 0 We already proved that the above addition cannot cause overflow. B can be 0000 without any problem. Since there is a already in in the MSB column, C4 follows C3. V = C3 () C4 = 0 7 / 3 C Copyright 2006 Gandhi Puvvada

Hypothesis #2: Subtracting a negative number from another negative number does not cause overflow. - a 2 a a 0 b 2 b b 0? d 2 d d 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0 We already proved that the above addition cannot cause overflow. B can be 0000 without any problem. Hypothesis #3: Subtracting a number from a negative number can cause overflow. - Since there is a already in in the MSB column, C4 follows C3. V = C3 () C4 = 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0-2 3 0 = overflow C4 is surely a one here. If C3 = 0, we have overflow. V = C3 () C4 = a 2 a a 0 b 2 b b 0? d 2 d d 0 As stated before, if a carry C3 comes into the MSB column, then d3 comes out to be a, which matches with the mathematical sum (2 3 ) (-2 3 ) (-2 3 ) = (-2 3 ). Absence of C3 results in overflow. 8 / 3 C Copyright 2006 Gandhi Puvvada

Hypothesis #4: Subtracting a negative number from a number can cause overflow. - 0 a 2 a a 0 b 2 b b 0? d 2 d d 0 C4 is surely a zero here. If C3 =, we have overflow. V = C3 () C4 = -2 3 = overflow 0 a 2 a a 0 0 b 2 b b 0? d 2 d d 0 As stated before, if a carry C3 comes into the MSB column, then d3 comes out to be a, which cause overflow Presence of C3 results in overflow. Conclusion: Whether you subtract or add, the V = C3 () C4 provides overflow signal. 9 / 3 C Copyright 2006 Gandhi Puvvada

Addition of two unsigned numbers: C4 = indicates overflow in addition. Subtraction in unsigned numbers: 2 3 Error point: C bit is set 4 0 00 5 0 0 0000 000 000 00 UNSIGNED 000 2 3 4 Since negative numbers can not be represented in unsigned system, when the correct result is expected to be negative, there is an overflow. If A is lower than B in the subtraction A - B, then we have an overflow. 0 0 00 00 9 000 8 0 00 00 7 6 5 To perform A - B, let us perform A (2 4 - B). A (2 4 - B) = A ( - B ) = A B If (A B ) produces C4, then we know that the (2 4 ) added by us was not really needed for subtracting. It means that A is higher than or equal to B. If C4 was not produced, then A is less than B. C4 = 0 indicates overflow in subtraction. We call the above C4 Raw Carry. In many processors, it (the raw carry) is flipped for subtraction and recorded in the Flag Register (also called CCR (Condition Code Register), PSW (Processor Status Word). etc.). The processor designers do this (flip the raw carry) because it is easy for the programmers to remember one simple rule, Carry flag is the overflow indicator for unsigned addition or subtraction. When it is set, unsigned overflow has occurred. We call this Processed Carry to distinguish from the Raw Carry. 0 / 3 C Copyright 2006 Gandhi Puvvada

If you try to perform addition and subtraction with each possible pair of numbers in the range of 0000 to, is it possible to encounter all combinations of overflows (signed overflow and unsigned overflow)? Yes! Try to avoid using (though not necessary) 0000 and 000 numbers for A and B. 4-bit A 4-bit B UNSIGNED 4 2 3 0 0 00 0 Operation 4-bit Result Result / if numbers are treated as signed numbers Addition Addition Addition Addition Subtraction Subtraction Subtraction Subtraction 5 0 9 0 0000 000 2 000 3 00 000 4 00 5 Smallest (0) to Largest (5) 00 00 00 000 0 8 7 6 Result / if numbers are treated as unsigned numbers - 0-2 0000 000 2 0-3 000 0 00 Smallest (-8) 3-4 - 5 00 0 00 to Largest (7) 000 00 00 4 5-6 00 000 0 6-7 - 8 7 V Raw Carry C4 2-4 3-3 - 5 Error point: C bit is set 0-2 - 6 4 0 00 0 00 9 5 0 LARGER mag. 0 8 7 0000 000 000 00 SMALLER mag. 000 00 00 00 00 000 0 0 0-0 - 7 0-8 7 UNSIGNED 0000 000 000 SMALLER mag. LARGER mag. 00 000 00 00 00 00 000 0 6 6 2 2 5 3 5 4 SIGNED 3 4 Error Point: V bit is set / 3 C Copyright 2006 Gandhi Puvvada

Special note about avoiding spurious overflow while performing (A - B) by doing (A B ): Subtraction in unsigned numbers: We said that the absence of the Raw Carry C4 at the end of the subtraction is an indication of the overflow. When we said end we meant at the end of the addition of the 3 items in (A B ). If you perform this addition in two parts (add two of the three items together first, and then add the third, then there is a problem if you try to subtract a zero. A - B = a3 a2 a a0-0 0 0 0 = (a3 a2 a a0 ) If you perform the addition of the three items in the last step in one stroke, then C4 will definitely be true because => C4 = ). However if you perform the addition in two steps as [ a3 a2 a a0 ( ) ], then in the second step you will not find Raw Carry C4 as and you could wrongly interpret that there was an unsigned subtraction overflow. Subtracting other numbers (other than 0000) including the number 000 do not pose any problems whether you perform the subtraction in one step or in two steps [(B ) A]. However, if you perform the addition in two steps grouping the three terms differently, then you can run into the same problem. Examples: [(A ) B ] or [(A B ) ] Conclusion: always perform the addition of the three items in one stroke. 2 / 3 C Copyright 2006 Gandhi Puvvada

Subtraction in signed numbers: Here again we need to perform the addition of the three items (A B ) in on stroke. If you perform the addition in two strokes in any one of the three possible ways below, you can be causing spurious overflow(s). [(B ) A ] -- case [(A ) B ] -- case 2 [(A B ) ] -- case 3 Note that the is considered as a one, i.e. 000. To provide examples where we run into spurious overflow problem, basically we need to make the first step to cause signed overflow, which would have been averted had we added the three items together in one stroke. For (something ) to cause overflow, the something should be the largest number 0. Also for that overflow to be averted the third item shall be negative. So an example for case or case 2 is [(0 ) ] [(B ) A ] Consider A = (-) = and B = (-8) = 000. Then A - B = (7) [(A ) B ] Consider A = (7) =0 and B = (0) = 0000. Then A - B = (7) [(A B ) ] Consider A = (-4) =00 and B = (4) = 000. Then A - B = (-8) Note that when there is a spurious overflow in the first step, then there will be a spurious overflow in the second step also. 3 / 3 C Copyright 2006 Gandhi Puvvada