Negative Bit Representation Outline

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

ENGIN 112 Intro to Electrical and Computer Engineering

Complement Arithmetic

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

MATH Dr. Halimah Alshehri Dr. Halimah Alshehri

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

Combinational Logic Design Arithmetic Functions and Circuits

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

Binary Floating-Point Numbers

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

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

Chapter 1 CSCI

Number Representation and Waveform Quantization

Physics 310 Lecture 10 Microprocessors

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

0,..., r 1 = digits in radix r number system, that is 0 d i r 1 where m i n 1

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

Multiplication of signed-operands

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Adding and Subtracting Rational Expressions. Add and subtract rational expressions with the same denominator.

CSE 241 Digital Systems Spring 2013

Mathematical preliminaries and error analysis

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

Notes for Chapter 1 of. Scientific Computing with Case Studies

SECTION 2.3: LONG AND SYNTHETIC POLYNOMIAL DIVISION

Graduate Institute of Electronics Engineering, NTU Basic Division Scheme

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

Ex code

CprE 281: Digital Logic

Error Detection and Correction: Small Applications of Exclusive-Or

Arithmetic Circuits How to add and subtract using combinational logic Setting flags Adding faster

Chapter 1: Solutions to Exercises

ECE260: Fundamentals of Computer Engineering

What Every Programmer Should Know About Floating-Point Arithmetic DRAFT. Last updated: November 3, Abstract

CMP 334: Seventh Class

hexadecimal-to-decimal conversion

Algebra II Polynomials: Operations and Functions

Logic and Computer Design Fundamentals. Chapter 5 Arithmetic Functions and Circuits

Jim Lambers MAT 610 Summer Session Lecture 2 Notes

Numbers and Arithmetic

Combinational Logic. By : Ali Mustafa

Lecture 5: Arithmetic

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Primes and Modular Arithmetic! CSCI 2824, Fall 2014!!

CHAPTER 2 NUMBER SYSTEMS

Slide Set 3. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Conversions between Decimal and Binary

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

Park Forest Math Team. Meet #3. Algebra. Self-study Packet

Basic elements of number theory

Basic elements of number theory

Digital Systems Roberto Muscedere Images 2013 Pearson Education Inc. 1

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

ECS 231 Computer Arithmetic 1 / 27

Lecture 6: Introducing Complexity

ELEN Electronique numérique

Siteswap state diagrams

Intro To Digital Logic

QUADRATIC PROGRAMMING?

Lecture 8: Sequential Multipliers

Section 3-4: Least Common Multiple and Greatest Common Factor

4.4 Energy in multiple dimensions, dot product

1 Floating point arithmetic

MATH 115, SUMMER 2012 LECTURE 4 THURSDAY, JUNE 21ST

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

DIFFERENCE EQUATIONS

Algebra II. Slide 1 / 276. Slide 2 / 276. Slide 3 / 276. Polynomials: Operations and Functions. Table of Contents

EAD 115. Numerical Solution of Engineering and Scientific Problems. David M. Rocke Department of Applied Science

Logic. Combinational. inputs. outputs. the result. system can

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Numbers and Arithmetic

EE 109 Final Don t Make the Same Mistake

1 Reals are Uncountable

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

Homework 2 Foundations of Computational Math 1 Fall 2018

Adders, subtractors comparators, multipliers and other ALU elements

Expressions that always have the same value. The Identity Property of Addition states that For any value a; a + 0 = a so = 3

CS 5319 Advanced Discrete Structure. Lecture 9: Introduction to Number Theory II

Grade 11/12 Math Circles Rational Points on an Elliptic Curves Dr. Carmen Bruni November 11, Lest We Forget

COMBINATIONAL LOGIC FUNCTIONS

Math Lecture 4 Limit Laws

Chetek-Weyerhaeuser High School

Lecture 2 Review on Digital Logic (Part 1)

Calculus (Math 1A) Lecture 4

Round-off Errors and Computer Arithmetic - (1.2)

#29: Logarithm review May 16, 2009

You separate binary numbers into columns in a similar fashion. 2 5 = 32

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

Calculus (Math 1A) Lecture 4

Clock Arithmetic. 1. If it is 9 o clock and you get out of school in 4 hours, when do you get out of school?

Design of Sequential Circuits

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

Math 016 Lessons Wimayra LUY

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

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

Groups. s t or s t or even st rather than f(s,t).

CSE 20 DISCRETE MATH. Winter

MAT 460: Numerical Analysis I. James V. Lambers

Computer Arithmetic. MATH 375 Numerical Analysis. J. Robert Buchanan. Fall Department of Mathematics. J. Robert Buchanan Computer Arithmetic

Transcription:

Negative Bit Representation Outline 1. Negative Bit Representation Outline 2. Negative Integers 3. Representing Negativity 4. Which Bit for the Sign? 5. Sign-Value 6. Disadvantages of Sign-Value 7. One s Complement 8. Disadvantages of One s Complement 9. Two s Complement 10. Advantages of Two s Complement #1 11. Advantages of Two s Complement #2 12. Advantages of Two s Complement #3 13. Advantages of Two s Complement #4 14. Advantages of Two s Complement #4 15. Range of Two s Complement Values #1 16. Range of Two s Complement Values #2 17. Range of Two s Complement Values #3 18. Range of Two s Complement Values #4 19. Range of Two s Complement Values #5 20. Overflow #1 21. Overflow #2 22. Underflow #1 23. Underflow #2 24. Overflow Example #1 25. Overflow Example #2 CS1313 Fall 2017 1

Negative Integers In the first slide packet on binary representation, we saw how nonnegative integer values like 97 are represented in memory. What if, instead of having 97, we had -97? We need a way to represent negative integers. CS1313 Fall 2017 2

Representing Negativity For starters, we need a way to represent whether an integer is negative or positive. We can think of this as a binary question: a number is either negative or nonnegative. So, we can simply pick a bit in the binary representation of the integer and decide that it s going to be the sign bit. Which bit should we pick? CS1313 Fall 2017 3

Which Bit for the Sign? Which bit should we pick? Well, we want to pick the bit that we re least likely to use in real life, so that it s not a big waste to use it as a sign bit. In real life, we re much more likely to deal with very small numbers (for example, 0, 1, 13, 97) than very large numbers (for example, 4001431453). So, we pick the leftmost bit, called the most significant bit, and decide that it ll be our sign bit. CS1313 Fall 2017 4

Sign-Value Okay, now we have our sign bit. So here are three ways to represent negative integers. Sign-Value: To get the negative version of a positive number, set the sign bit to 1, and leave all other bits unchanged: 97 = -97 = 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 CS1313 Fall 2017 5

Disadvantages of Sign-Value An unfortunate feature of Sign-Value representation is that there are two ways of representing the value zero: all bits set to zero, and all bits except the sign bit set to zero. 0 = 0 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 This makes the math a bit confusing. More importantly, when performing arithmetic operations, we need to treat negative operands as special cases. CS1313 Fall 2017 6

One s Complement One s Complement: To get the negative version of a positive number, invert (complement) all bits; that is, all 1 s become 0 s and vice versa. 97 = -97 = 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 0 CS1313 Fall 2017 7

Disadvantages of One s Complement An unfortunate feature of One s Complement representation is that there are two ways of representing the value zero: all bits set to zero, and all bits set to one. 0 = 0 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 This makes the math a bit confusing. More importantly, when performing arithmetic operations, we need to treat negative operands as special cases. CS1313 Fall 2017 8

Two s Complement Two s Complement: To get the negative version of a positive number, invert all bits and then add 1; if the addition causes a carry bit past the most significant bit, discard the high carry: 97 = 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 ~97 = -97 = 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 0 + 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 CS1313 Fall 2017 9

Advantages of Two s Complement #1 In Two s Complement representation, the value zero is uniquely represented by having all bits set to zero: 0 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ~0 = -0 = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 CS1313 Fall 2017 10

Advantages of Two s Complement #2 When you perform an arithmetic operation (for example, addition, subtraction, multiplication, division) on two signed integers in Two s Complement representation, you can use exactly the same method as if you had two unsigned integers (that is, nonnegative integers with no sign bit)... EXCEPT, you throw away the high carry (or the high borrow for subtraction). CS1313 Fall 2017 11

Advantages of Two s Complement #3 This property of Two s Complement representation is so incredibly handy that virtually every general-purpose computer available today uses Two s Complement. Why? Because, with Two s Complement, we don t need special algorithms (and therefore extra circuitry) for arithmetic operations that involve negative values. CS1313 Fall 2017 12

Advantages of Two s Complement #4 Using Two s Complement to do arithmetic: 1 97 = -97 = 0 = 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 CS1313 Fall 2017 13

Advantages of Two s Complement #4 Using Two s Complement to do arithmetic: 1 45 = -14 = 31 = 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 CS1313 Fall 2017 14

Range of Two s Complement Values #1 When we represent negative integers in Two s Complement notation, the range of numbers that can be represented in b bits is: (2 b 1 )... (+2 b 1 1) For example, the range of numbers that can be represented in 8 bits is: (2 7 )... (+2 7 1) = 128... 127 Likewise, the range of numbers that can be represented in 16 bits is: (2 15 )... (+2 15 1) = 32,768... 32,767 How do we know this? CS1313 Fall 2017 15

Range of Two s Complement Values #2 When we represent negative integers in Two s Complement notation, the range of numbers that can be represented in b bits is: (2 b 1 )... (+2 b 1 1) How do we know this? Here s the biggest number that can be represented in 16 bits: 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 If we add one to it, we get: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 But this number is negative. CS1313 Fall 2017 16

Range of Two s Complement Values #3 Here s the biggest number that can be represented in 16 bits: 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 If we add one to it, we get: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 But this number is negative. If this number were unsigned, then it d be 2 15, which is 2 16-1, which is 2 b-1, where b (the number of bits) is 16. Therefore, the largest number that can be represented in b bits in Two s Complement must be 2 b-1-1. CS1313 Fall 2017 17

Range of Two s Complement Values #4 So what s the smallest negative integer (that is, the negative integer with the greatest absolute value)? Well, can we represent the negative of 2 b 1 1? +(2 b-1 1) = 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ~(2 b-1-1) = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 1 -(2 b-1-1) = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 CS1313 Fall 2017 18

Range of Two s Complement Values #5 We can represent (2 b 1 1). So, can we represent the negative of (2 b 1 )? -(2 b-1-1) = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1-1 -(2 b-1 ) = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Since the sign didn t change, it worked! But, if we tried subtracting again, we d borrow and the sign would change, indicating failure. So, (2 b 1 ) is the lowest number that can be represented (that is, the negative number with the highest absolute value). CS1313 Fall 2017 19

Overflow #1 When we re working with a value that s near the upper limit of what can be represented in Two s Complement for the given number of bits, we sometimes perform an operation that should result in a positive value but instead produces a negative value. Such an event is called overflow. CS1313 Fall 2017 20

Overflow #2 Consider the following addition in 8-bit Two s Complement representation: +127 = + 1 +128? = 0 1 1 1 1 1 1 1 + 1 1 0 0 0 0 0 0 0 Notice that the result should be +128, but because the leftmost bit is 1, it s actually -128. This is overflow: an arithmetic operation that should have a positive result goes over the top to become negative. CS1313 Fall 2017 21

Underflow #1 When we re working with a value that s near the lower limit of what can be represented in Two s Complement for the given number of bits, we sometimes perform an operation that should result in a negative value but instead produces a positive value. Such an event is called underflow. CS1313 Fall 2017 22

Underflow #2 Consider the following addition in 8-bit Two s Complement representation: -128 = - 1-129? = 1 0 0 0 0 0 0 0-1 0 1 1 1 1 1 1 1 Notice that the result should be -129, but because the leftmost bit is 1, it s actually +127. This is underflow: an arithmetic operation that should have a negative result goes under the bottom to become positive. CS1313 Fall 2017 23

17. Inserted debug statements into anomdtb.f90, discovered that a sum-of-squared variable is becoming very, very negative! Key output from the debug statements: OpEn= 16.00, OpTotSq= 4142182.00, OpTot= 7126.00 DataA val = 93, OpTotSq= 8649.00 DataA val = 172, OpTotSq= 38233.00 DataA val = 950, OpTotSq= 940733.00 DataA val = 797, OpTotSq= 1575942.00 DataA val = 293, OpTotSq= 1661791.00 DataA val = 83, OpTotSq= 1668680.00 DataA val = 860, OpTotSq= 2408280.00 DataA val = 222, OpTotSq= 2457564.00 DataA val = 452, OpTotSq= 2661868.00 DataA val = 561, OpTotSq= 2976589.00 DataA val = 49920, OpTotSq=-1799984256.00 DataA val = 547, OpTotSq=-1799684992.00 DataA val = 672, OpTotSq=-1799233408.00 DataA val = 710, OpTotSq=-1798729344.00 DataA val = 211, OpTotSq=-1798684800.00 DataA val = 403, OpTotSq=-1798522368.00 OpEn= 16.00, OpTotSq=-1798522368.00, OpTot=56946.00 forrtl: error (75): floating point exception IOT trap (core dumped) Overflow Example..so the data value is unbfeasibly large, but why does the sum-ofsquares parameter OpTotSq go negative?!! Probable answer: the high value is pushing beyond the singleprecision default for Fortran reals? http://www.blc.arizona.edu/courses/schaffer/182h/climate/harry _Read_Me%20%28Page%20Numbers%29.pdf CS1313 Fall 2017 24

Overflow Example (cont d) 17. Inserted debug statements into anomdtb.f90, discovered that a sum-of-squared variable is becoming very, very negative! Key output from the debug statements: OpEn= 16.00, OpTotSq= 4142182.00, OpTot= 7126.00 DataA val = 93, OpTotSq= 8649.00 DataA val = 172, OpTotSq= 38233.00 DataA val = 950, OpTotSq= 940733.00 DataA val = 797, OpTotSq= 1575942.00 DataA val = 293, OpTotSq= 1661791.00 DataA val = 83, OpTotSq= 1668680.00 DataA val = 860, OpTotSq= 2408280.00 DataA val = 222, OpTotSq= 2457564.00 DataA val = 452, OpTotSq= 2661868.00 DataA val = 561, OpTotSq= 2976589.00 DataA val = 49920, OpTotSq=-1799984256.00 DataA val = 547, OpTotSq=-1799684992.00 DataA val = 672, OpTotSq=-1799233408.00 DataA val = 710, OpTotSq=-1798729344.00 DataA val = 211, OpTotSq=-1798684800.00 DataA val = 403, OpTotSq=-1798522368.00 OpEn= 16.00, OpTotSq=-1798522368.00, OpTot=56946.00 forrtl: error (75): floating point exception IOT trap (core dumped)..so the data value is unbfeasibly large, but why does the sum-ofsquares parameter OpTotSq go negative?!! Probable answer: the high value is pushing beyond the singleprecision default for Fortran reals? Likely actual answer: The code used an integer to do the arithmetic, then multiplied by a floating point number before outputting. http://www.blc.arizona.edu/courses/schaffer/182h/climate/harry _Read_Me%20%28Page%20Numbers%29.pdf CS1313 Fall 2017 25