Signed Numbers CS208. Signed Numbers Until now we've been concentrating on unsigned numbers. In real life we also need to be able represent signed numbers.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

Computer Engineering (Logic Circuits) Dr. Tamer Samy Gaafar Lec. # 2
EMB1006 The Binary System There is no 2 Jonathan-Lee Jones.
Addition of two binary numbers = = = = 39.
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
James Tam Number Representations You will learn about the binary number system and how subtractions are performed on the computer.
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
Floating Point Numbers
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Signed Numbers Up till now we've been concentrating on unsigned numbers. In real life we have to represent signed numbers ( like: -12, -45, 78). The difference.
Signed Numbers.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Complements: different approaches for representing negative numbers
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
Two’s Complement 1.As an action: (Assume the starting value is 1011) 1.Flip the bits from the starting value => Add one to get the answer.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
3. Representing Integer Data
Binary Representation and Computer Arithmetic
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Chapter 7 Arithmetic Operations and Circuits Binary Arithmetic Addition –When the sum exceeds 1, carry a 1 over to the next-more-significant column.
Representing Integer Data Book : Chapter ( Subject has no point !! ) A99ACF.
Dale & Lewis Chapter 3 Data Representation. Data and computers Everything inside a computer is stored as patterns of 0s and 1s Numbers, text, audio, video,
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 20, 2004 Lecture Number: 23.
Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of.
Lecture 4 Last Lecture –Positional Numbering Systems –Converting Between Bases Today’s Topics –Signed Integer Representation Signed magnitude One’s complement.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Number Representation
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Computer Math CPS120 Introduction to Computer Science Lecture 4.
AEEE2031 Data Representation and Numbering Systems.
Sep 29, 2004Subtraction (lvk)1 Negative Numbers and Subtraction The adders we designed can add only non-negative numbers – If we can represent negative.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Addition and Substraction
Ch3a- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Crunching Numbers Topics we need to explore Representing numbers on a computer.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
Introduction * Binary numbers are represented with a separate sign bit along with the magnitude. * For example, in an 8-bit binary number, the MSB is.
IT1004: Data Representation and Organization Negative number representation.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
ECE 3110: Introduction to Digital Systems Number Systems: signed numbers.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Computer Organization 1 Data Representation Negative Integers.
Nguyen Le CS147.  2.4 Signed Integer Representation  – Signed Magnitude  – Complement Systems  – Unsigned Versus Signed Numbers.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
Computer Math CPS120 Introduction to Computer Science Lecture 7.
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Dr. ClincyLecture 2 Slide 1 CS Chapter 2 (1 of 5) Dr. Clincy Professor of CS Note: Do not study chapter 2’s appendix (the topics will be covered.
Floating Point Numbers Dr. Mohsen NASRI College of Computer and Information Sciences, Majmaah University, Al Majmaah
Addition and Substraction
Digital Logic & Design Lecture 02.
Unit 18: Computational Thinking
CS 101 – Sept. 4 Number representation Integer Unsigned √ Signed √
How are negative and rational numbers represented on the computer?
CPS120: Introduction to Computer Science
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Two’s Complement & Binary Arithmetic
Presentation transcript:

Signed Numbers CS208

Signed Numbers Until now we've been concentrating on unsigned numbers. In real life we also need to be able represent signed numbers ( like: -12, -45, +78). A signed number MUST have a sign (+/-). A method is needed to represent the sign as part of the binary representation. Two signed number representation methods are: Sign/magnitude representation Twos-complement representation

Sign/Magnitude Representation In sign/magnitude (S/M) representation, the leftmost bit of a binary code represents the sign of the value: 0 for positive, 1 for negative; The remaining bits represent the numeric value.

Sign/Magnitude Representation To compute negative values using Sign/Magnitude (S/M) representation: 1) Begin with the binary representation of the positive value 2) Then flip the leftmost zero bit.

Sign/Magnitude Representation Ex 1. Find the S/M representation of So: = (in 8-bit sign/magnitude form) Step 2: If the number you want to represent is negative, flip leftmost bit Step 1: Find binary representation using 8 bits 6 10 =

Sign/Magnitude Representation Ex 2. Find the S/M representation of Step 2: If the number you want to represent is negative, flip left most bit (positive -- no flipping) Step 1: Find binary representation using 8 bits = So: = (in 8-bit sign/magnitude form)

Sign/Magnitude Representation Ex 3. Find the S/M representation of Step 2: If the number you want to represent is negative, flip left most bit Step 1: Find binary representation using 8 bits = So: = (in 8-bit sign/magnitude form)

Sign bit: 0  positive 1  negative Sign bit: 0  positive 1  negative 31 remaining bits for magnitude (i.e. the value) 32-bit example: Sign/Magnitude Representation

Try It Yourself Convert the following decimal numbers to 8- bit sign/magnitude representation: (Answers on NEXT slide)

Answers 35= = =

Two different representations for 0! Inner numbers: Binary representation Seven Positive Numbers and “Positive” Zero Seven Negative Numbers and “Negative” Zero Two discontinuities Problems with Sign/Magnitude

Two’s Complement Representation Another method used to represent negative numbers (used by most modern computers) is two’s complement. The leftmost bit STILL serves as a sign bit: 0 for positive numbers, 1 for negative numbers.

Two’s Complement Representation To compute negative values using Two’s Complement representation: 1) Begin with the binary representation of the positive value 2) Complement (flip each bit -- if it is 0 make it 1 and visa versa) the entire positive number 3) Then add one.

Two’s Complement Representation Ex 1.Find the 8-bit two’s complement representation of –6 10 Step 1: Find binary representation of the positive value in 8 bits 6 10 =

Two’s Complement Representation Ex 1 continued Step 2: Complement the entire positive value Positive Value: Complemented:

Two’s Complement Representation So: = (in 8-bit 2's complement form) Ex 1, Step 3: Add one to complemented value (complemented) -> (add one) ->

Two’s Complement Representation Ex 2.Find the 8-bit two’s complement representation of Step 1: Find binary representation of the positive value in 8 bits = is positive, so STOP after step 1! So:20 10 = (in 8-bit 2's complement form)

Two’s Complement Representation Ex 3.Find the 8-bit two’s complement representation of –80 10 Step 1: Find binary representation of the positive value in 8 bits = is negative, so continue…

Two’s Complement Representation Ex 3 Step 2: Complement the entire positive value Positive Value: Complemented:

Two’s Complement Representation So: = (in 8-bit 2's complement form) Ex 3, Step 3: Add one to complemented value (complemented) -> (add one) ->

Try It Yourself Convert the following decimal numbers to 8-bit 2’s complement representation: (Answers on NEXT slide)

Answers -73= = =

Two’s Complement Representation Alternate method -- replaces previous steps 2-3 Step 2: Scanning the positive binary representation from right to left, find first one bit, from low-order (right) end Step 3: Complement (flip) the remaining bits to the left (left complemented)-->

Two’s Complement Representation Ex 1: Find the Two’s Complement of Step 1: Find the 8-bit binary representation of the positive value =

Two’s Complement Representation Step 2: Find first one bit, from low-order (right) end, and complement the pattern to the left (left complemented)-> So: = (in 8-bit 2's complement form)

Two’s Complement Representation Ex 2: Find the Two’s Complement of Step 1: Find the 8 bit binary representation of the positive value = Steps 2-3: 72 is positive, so STOP after step 1! So: = (in 8-bit 2's complement form)

Two’s Complement Representation Ex 3: Find the Two’s Complement of Step 1: Find the 8-bit binary representation of the positive value =

Two’s Complement Representation Ex 3, Step 2: Find first one bit, from low- order (right) end, and complement the pattern to the left (left complemented)-> So: = (in 8-bit 2's complement form)

Sign bit: 0 --> positive 1 --> negative Sign bit: 0 --> positive 1 --> negative 31 remaining bits for magnitude (i.e. value stored in two’s complement form) 31 remaining bits for magnitude (i.e. value stored in two’s complement form) 32-bit example: Two’s Complement Representation

Try It Yourself Convert the following decimal numbers to 8-bit 2’s complement representation, using method 2: (Answers on NEXT slide)

Answers -36= = =

Two’s Complement to Decimal Ex 1: Find the decimal equivalent of the 8-bit 2’s complement value Step 1: Determine if number is positive or negative: Leftmost bit is 1, so number is negative.

Two’s Complement to Decimal Ex 1, Step 2: Find first one bit, from low-order (right) end, and complement the pattern to the left (left complemented) 

Two’s Complement to Decimal Ex 1, Step 3: Determine the numeric value: = = So: = (8-bit 2's complement form)

Two’s Complement to Decimal Ex 2: Find the decimal equivalent of the 8-bit 2’s complement value Step 1: Determine if number is positive or negative: Leftmost bit is 0, so number is positive. Skip to step 3.

Two’s Complement to Decimal Ex2, Step 3: Determine the numeric value: = = So: = (8-bit 2's complement form)

Two’s Complement to Decimal Ex 3: Find the decimal equivalent of the 8-bit 2’s complement value Step 1: Determine if number is positive or negative: Leftmost bit is 1, so number is negative.

Two’s Complement to Decimal Ex 3, Step 2: Find first one bit, from low- order (right) end, and complement the pattern to the left (left complemented) 

Two’s Complement to Decimal Ex 3, Step 3: Determine the numeric value: = = So: = (8-bit 2's complement form)

Only one discontinuity now Inner numbers: Binary representation Eight Positive Numbers Re-order Negative numbers to eliminate one Discontinuity Note: Negative Numbers still have 1 for the most significant bit (MSB) Only one zero One extra negative number S/M problems solved with 2s complement

Two’s Complement Representation Biggest reason two’s complement used in most systems today? The binary codes can be added and subtracted as if they were unsigned binary numbers, without regard to the signs of the numbers they actually represent.

Two’s Complement Representation For example, to add +4 and -3, we simply add the corresponding binary codes, 0100 and 1101: 0100 (+4) (-3) 0001 (+1) NOTE: A carry to the leftmost column has been ignored. The result, 0001, is the code for +1, which IS the sum of +4 and -3.

Twos Complement Representation Likewise, to subtract +7 from +3: 0011 (+3) (+7) 1100 (-4) NOTE: A “phantom” 1 was borrowed from beyond the leftmost position. The result, 1100, is the code for -4, the result of subtracting +7 from +3.

Try It Yourself Convert the following decimal numbers to 8-bit 2’s complement representation. Then ADD them in binary. Finally, check your results by converting back to decimal: (Answers on NEXT slide)

Answer -33= =  is negative Convert to  = 16 So answer = -16 (which IS the result of – )

Two’s Complement Representation Summary - Benefits of Twos Complements: Addition and subtraction are simplified in the two’s-complement system, -0 has been eliminated, replaced by one extra negative value, for which there is no corresponding positive number.

Valid Ranges For any integer data representation, there is a LIMIT to the size of number that can be stored. The limit depends upon number of bits available for data storage.

Unsigned Integer Ranges Range = 0 to (2 n – 1) where n is the number of bits used to store the unsigned integer. Numbers with values GREATER than (2 n – 1) would require more bits. If you try to store too large a value without using more bits, OVERFLOW will occur.

Unsigned Integer Ranges Example: On a system that stores unsigned integers in 16-bit words: Range = 0 to (2 16 – 1) = 0 to Therefore, you cannot store numbers larger than in 16 bits.

Signed S/M Integer Ranges Range = -(2 (n-1) – 1) to +(2 (n-1) – 1) where n is the number of bits used to store the sign/magnitude integer. Numbers with values GREATER than +(2 (n-1) – 1) and values LESS than -(2 (n-1) – 1) would require more bits. If you try to store too large/too small a value without using more bits, OVERFLOW will occur.

S/M Integer Ranges Example: On a system that stores unsigned integers in 16-bit words: Range = -(2 15 – 1) to +(2 15 – 1) = to Therefore, you cannot store numbers larger than or smaller than in 16 bits.

Two’s Complement Ranges Range = -2 (n-1) to +(2 (n-1) – 1) where n is the number of bits used to store the two-s complement signed integer. Numbers with values GREATER than +(2 (n-1) – 1) and values LESS than -2 (n-1) would require more bits. If you try to store too large/too small a value without using more bits, OVERFLOW will occur.

Two’s Complement Ranges Example: On a system that stores unsigned integers in 16-bit words: Range = to +(2 15 – 1) = to Therefore, you cannot store numbers larger than or smaller than in 16 bits.

Try It Yourself What range of values can be represented by: a) a 6-bit unsigned number b) a 6-bit sign/magnitude number c) a 6-bit 2's complement number (Answers on NEXT slide)

Answers Unsigned = 0 to (2 6 – 1) = 0 to 63 S/M = -(2 5 – 1) to +(2 5 – 1) = -31 to +31 2’s Comp = -2 5 to +(2 5 – 1) = -32 to +31

Using Ranges for Validity Checking Once you know how small/large a value can be stored in n bits, you can use this knowledge to check whether you answers are valid, or cause overflow. Overflow can only occur if you are adding two positive numbers or two negative numbers

Using Ranges for Validity Checking Ex 1: Given the following 2’s complement equations in 5 bits, is the answer valid? 11111(-1) Range = (-3) -16 to (-4)  VALID

Using Ranges for Validity Checking Ex 2: Given the following 2’s complement equations in 5 bits, is the answer valid? 10111(-9) Range = (-11) -16 to (-20)  INVALID

Try It Yourself Perform the following 6-bit 2’s complement addition. Indicate if overflow occurs. Check your answers by converting to decimal (remember, if the leftmost bit is ONE, the value is negative) (Answers on next slide)

Answers (+25) (+16) (+41)  out of (invalid)range (over 31) (-9) (-7) (-16)  OKAY