COE 202: Digital Logic Design Signed Numbers

Slides:



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

Representing Numbers: Integers
Binary Systems1 DIGITAL LOGIC DESIGN by Dr. Fenghui Yao Tennessee State University Department of Computer Science Nashville, TN.
Radix Conversion Given a value X represented in source system with radix  s, represent the same number in a destination system with radix  d Consider.
Chapter 1 Binary Systems 1-1. Digital Systems
King Fahd University of Petroleum and Minerals
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
VIT UNIVERSITY1 ECE 103 DIGITAL LOGIC DESIGN CHAPTER I NUMBER SYSTEMS AND CODES Reference: M. Morris Mano & Michael D. Ciletti, "Digital Design", Fourth.
COE 308: Computer Architecture (T041) Dr. Marwan Abu-Amara Integer & Floating-Point Arithmetic (Appendix A, Computer Architecture: A Quantitative Approach,
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
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.
COE 202: Digital Logic Design Number Systems Part 1
Mantıksal Tasarım – BBM231 M. Önder Efe
Number Systems Lecture 02.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
The Binary Number System
Arithmetic for Computers
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Computer Arithmetic Nizamettin AYDIN
Logic and Digital System Design - CS 303
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
IT253: Computer Organization
Lecture Overview Introduction Positional Numbering System
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Complement Numbers. Outline  Negative Numbers Representation  Sign-and-magnitude  1s Complement  2s Complement  Comparison of Sign-and-Magnitude.
Number Systems. Why binary numbers? Digital systems process information in binary form. That is using 0s and 1s (LOW and HIGH, 0v and 5v). Digital designer.
CPS120: Introduction to Computer Science Computer Math: Signed Numbers.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
1 Digital Design: Number Systems Credits : Slides adapted from: J.F. Wakerly, Digital Design, 4/e, Prentice Hall, 2006 C.H. Roth, Fundamentals of Logic.
Representation of Data Ma King Man. Reference Text Book: Volume 2 Notes: Chapter 19.
Lecture 4 Last Lecture –Positional Numbering Systems –Converting Between Bases Today’s Topics –Signed Integer Representation Signed magnitude One’s complement.
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.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Operations on Bits Arithmetic Operations Logic Operations
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
Ahmad Almulhem, KFUPM 2009 COE 202: Digital Logic Design Number Systems Part 4 Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
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=
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic Herbert G. Mayer, PSU Status 1/14/2016 Copied with Permission from prof. Mark PSU ECE.
ECE 2110: Introduction to Digital Systems
ECE DIGITAL LOGIC LECTURE 3: DIGITAL COMPUTER AND NUMBER SYSTEMS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 01/19/2016.
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN Lecture 8 Dr. Shi Dept. of Electrical and Computer Engineering.
Lecture 1.2 (Chapter 1) Prepared by Dr. Lamiaa Elshenawy
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
BINARY SYSTEMS ENGR. KASHIF SHAHZAD 1. BINARY NUMBERS 1/2 Internally, information in digital systems is of binary form groups of bits (i.e. binary numbers)
DATA REPRESENTATION Data Types Complements Fixed Point Representations
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Addition and Subtraction
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
BEE1244 Digital System and Electronics BEE1244 Digital System and Electronic Chapter 2 Number Systems.
Data Representation Data Types Complements Fixed Point Representation
Data Representation in Computer Systems
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic
Chapter3 Fixed Point Representation
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
COE 202: Digital Logic Design Number Systems Part 2
Presentation transcript:

COE 202: Digital Logic Design Signed Numbers Courtesy of Dr. Ahmad Almulhem KFUPM

Objectives Number representation in computers Unsigned numbers Signed numbers Signed number representation Signed-magnitude representation Complement representation Signed numbers arithmetic Overflow Shift operations Complements for other bases KFUPM

Machine representation of numbers Digital computers store numbers in a special electronic device (memory) called registers Registers have fixed size = n storage elements; each element holds 0 or 1 The register size is typically a power of 2. e.g. 2, 4, 8, 16,… An n-bit register can represent one of 2n distinct values e.g. for n=4, distinct values = 16 {0000, 0001, ….., 1111} Numbers stored in a register can be signed or unsigned e.g. -9 or +9 are signed numbers. 9 is an unsigned number KFUPM

Example Q. How the value (13)10 (or D in Hexadecimal) is stored in a 4-bit register and in an 8-bit register ZEROS are used to pad Q: Is this a signed or unsigned number? KFUPM

Signed Number Representation A signed number has: Magnitude (or absolute value) Sign (positive or negative) Everything has to be in binary (0s and 1s) Two common techniques for representing signed numbers are: Signed-magnitude representation Complement representation KFUPM

Signed magnitude representation The most significant bit is the sign bit. ‘0’ --> positive number ‘1’ --> negative number The remaining (n-1) bits represent the magnitude Total possible numbers representable by an n-bit register using signed magnitude is equal to: 2n-1, as the MSB is always reserved for the sign Largest representable magnitude, in this method, is (2n-1-1) Range of numbers: -(2n-1-1), … , -1, -0, +0, +1, …, +(2n-1-1) KFUPM

Examples Q: Show the signed magnitude representations of +6, -6, +13 and - 13 using a 4-bit register and an 8-bit register Note that: (6)10 = (110)2 , (13)10 = (1101)2 For a 4-bit register, the leftmost bit is reserved for the sign, which leaves 3 bits only to represent the magnitude The largest magnitude that can be represented = 2(4-1) –1 = 7 Because 13 > 7, the numbers +13 and –13 can NOT be represented using the 4-bit register KFUPM

Examples (cont.) Q: Show the signed magnitude representations of +6, -6, +13 and - 13 using a 4-bit register and an 8-bit register For an 8-bit register, the leftmost bit is a sign bit, which leaves 7 bits to represent the magnitude. The largest magnitude representable in 7-bits =127 =2(8-1)-1 KFUPM

Signed-Magnitude Arithmetic If sign is the same, just add the magnitude KFUPM

Signed-Magnitude Arithmetic KFUPM

Signed-Magnitude Arithmetic KFUPM

Signed-Magnitude Representation Need two separate operations Addition Subtraction Several decisions: Signs same or different? Which operand is larger? What is the sign of final result? Two zeroes (+0, -0) Advantages Easy to understand Disadvantages –Two different 0s – Hard to implement in logic KFUPM

Complement representation Subtraction: A – B = A + (-B) = A + B’ A negative number is represented with its complement Note that B = (B’)’ Two types of complements for each base-r (r-1)’s complement 1’s complement r’s complement 2’s complement KFUPM

1’s Complement A positive number is as in signed-magnitude For a negative number, flip all bits ! Q: What do you observe about this representation? KFUPM

1’s Complement A positive number is as in signed-magnitude For a negative number, flip all bits ! Negative numbers always have a 1 in the MSB Two 0s ! 4 bits represent the range: -7 to +7 KFUPM

1’s Complement Arithmetic Q: What if there is no last carry? A: If there is no carry, the result is negative in 1’s complement KFUPM

Example: Binary subtraction using 1’s complement Regular Approach: M - N M = 0 1 0 1 0 1 0 0 N = 0 1 0 0 0 1 0 0 - ------------------- 0 0 0 1 0 0 0 0 1’s complement: M – N = M + N’ M = 0 1 0 1 0 1 0 0 N = 1 0 1 1 1 0 1 1 + ------------------- 1 0 0 0 0 1 1 1 1 1 + 0 0 0 1 0 0 0 0 End Around Carry Regular Approach: N - M N = 0 1 0 0 0 1 0 0 M = 0 1 0 1 0 1 0 0 - ------------------- - 0 0 0 1 0 0 0 0 1’s complement: N + M’ N = 0 1 0 0 0 1 0 0 M = 1 0 1 0 1 0 1 1 + ------------------- 1 1 1 0 1 1 1 1 Correction Step Required: -(1’s complement of 11101111) = -(00010000) No End Carry KFUPM

2’s Complement A positive number is as in signed-magnitude For a negative number, two way to get the 2’s complement: add 1 to 1’s complement, or Starting from right find first ‘1’, invert all bit to the left of that one Q: What do you observe about this representation? KFUPM

2’s Complement A positive number is as in signed-magnitude For a negative number, two way to get the 2’s complement: add 1 to 1’s complement, or Starting from right find first ‘1’, invert all bit to the left of that one Negative numbers always have a 1 in the MSB One 0s ! 4 bits represent the range: -8 to +7 KFUPM

2’s Complement Arithmetic - If there is an end carry, then discard - If there is no end carry, the result is negative in 2’s complement KFUPM

Example: Binary subtraction using 2’s complement Regular Approach: M - N M = 0 1 0 1 0 1 0 0 N = 0 1 0 0 0 1 0 0 - ------------------- 0 0 0 1 0 0 0 0 2’s complement: M – N = M + N’ M = 0 1 0 1 0 1 0 0 N = 1 0 1 1 1 1 0 0 + ------------------- 1 0 0 0 1 0 0 0 0 Discard End Carry Regular Approach: N - M N = 0 1 0 0 0 1 0 0 M = 0 1 0 1 0 1 0 0 - ------------------- - 0 0 0 1 0 0 0 0 2’s complement: N + M’ N = 0 1 0 0 0 1 0 0 M = 1 0 1 0 1 1 0 0 + ------------------- 1 1 1 1 0 0 0 0 Correction Step Required: -(2’s complement of 11110000) = -(00010000) No End Carry KFUPM

Summary (1’s Complement vs 2’s Complement) Advantages Easy to generate the complement Only one addition process Disadvantages Handling last carry out Two different 0s! Disadvantages Harder to generate the complement Advantages Only One zero ! Only one addition process No end around carry Usually, the 2’s complement is the preferred way KFUPM

Summary (Signed-Magnitude vs Complements) Positive numbers are identical in all representations and have 0 in the MSB 2’s complement has only one 0; the others have 2 0s All negative numbers have 1 in the MSB 2’s complement has one more negative number! KFUPM

Range of values of unsigned and signed integers represented using n bits Unsigned integer values range: 0 to 2n-1 For n = 8 bits  Range: 0 to 28-1 = 0 to 255 Signed-magnitude integer values range: -(2n-1-1) to +(2n-1-1) For n = 8 bits  Range: -27-1 to 27-1 = -127 to +127 Signed 1’s complement integer values range: -(2n-1-1) to +(2n-1-1) Signed 2’s complement integer values range: -(2n-1) to +(2n-1-1) For n = 8 bits  Range: -27 to 27-1 = -128 to +127 KFUPM

Overflow Number’s sizes in computers are fixed Overflows can occur when the result of an operation does not fit. Q: When can an overflow occur? Unsigned numbers Signed numbers Subtracting two numbers Adding a positive number to a negative number Adding two numbers Adding two negative numbers or two positive numbers KFUPM

Overflow (2’s Complement) Q: Add +5 and +4 in 2’s complement. A: An overflow happened. The correct answer +9 (1001) cannot be represented by 4 bits. Detection: Adding two positive numbers result in a negative number! Carry in sign bit is different from carry out of sign bit Solution: Use one more bit, extend the sign bit 0 1 0 0 0101 + 510 + 0100 + 410 0 1001 - 710 ?? 0 0 1 0 00101 + 510 + 00100 + 410 01001 + 910 KFUPM

Overflow (2’s Complement) Q: Add -5 and -4 in 2’s complement. A: An overflow happened. The correct answer -9 (10111) cannot be represented by 4 bits. Detection: Adding two negative numbers result in a positive number! Carry in sign bit is different from carry out of sign bit Solution: Use one more bit, extend the sign bit 1 0 0 0 1011 - 510 + 1100 - 410 1 0111 + 710 ?? 1 1 0 0 11011 - 510 + 11100 - 410 10111 - 910 KFUPM

Range Extensions To extend the representation of a 2’s complement number possibly for storage and use in a larger-sized register If the number is positive, pad 0’s to the left of the integral number (sign bit extension), and 0’s to the right of the fractional number If the number is negative, pad 1’s to the left of the integral number (sign bit extension), and 0’s to the right of the fractional number 0 1 0 0 1 0 0 0 0 0 1 0 0 1 9-bit register (+9) – sign bit extended 5-bit register (+9) 1 0 1 1 1 1 1 1 1 1 0 1 1 1 9-bit register (-9) – sign bit extended 5-bit register (-9) KFUPM

Arithmetic Shift A binary number can be shifted right or left To shift an unsigned numbers (right or left), pad with 0s. Example: Left Shift 0001 110 0010 210 0100 410 1000 810 Q1: What is the effect of left-shifting? Q2: What is the effect of right-shifting? KFUPM

Arithmetic Shift To shift a signed number Example (2’s complement): Left-Shift: pad with 0s Right-Shift: Extend sign bit Example (2’s complement): Right Shift 1000 - 810 1100 - 410 1110 - 210 1111 - 110 In General left-shifting = multiply by r right-shifting = divide by r KFUPM

Arithmetic Shifts KFUPM

Complements for other bases Complements apply to other bases! Two types of complements for each radix (base) Diminished radix (r-1)’s complement 1’s complement Radix r’s complement 2’s complement KFUPM

Diminished Radix (r -1)’s complement In general the (r-1)’s complement of a number Q in base-r is given by Qr-1’ = (r n – r –m) – Q where n is the number of integral digits in Q and m is the number of fractional digits in Q. Examples: - For Q = 12.310, Q’9 = 99.9 – 12.3 = 87.610 - For Q = 228, Q’7 = 77 – 22 = 558 KFUPM

Examples Find the 9’s complement of the following decimal numbers: a. Q = 2357 Here, n = 4, m = 0 [no fractional digits] M9 = 104 - 1 Q9’ = M9 – 2357 = 9999 – 2357 = 7642 [9’s complement of Q] b. Q = 2895.786 Here, n = 4, m = 3 M9 = 104 – 10-3 Q9’ = M9 – 2895.786 = 9999.999 – 2895.786 = 7104.213 KFUPM

Examples Find the 1’s complement of the following binary numbers: a. Q = 110101010 Here, n = 9, m = 0 [no fractional digits] M1 = 29 - 1 Q1’ = M1 – 110101010 = 111111111 - 110101010 = 001010101 [1’s complement of Q] a. Q = 1010.001 Here, n = 4, m = 3 M1 = 24 – 2-3 Q1’ = M1 – 110101010 = 1111.111 – 1010.001 = 0101.110 [1’s complement of Q] Hint: Flip all the bits of a binary number to compute its 1’s complement! KFUPM

Radix (r’s) Complement In general the r’s complement of a number Q in base-r is given by Qr’ = r n – Q where n is the number of integral digits in Q. Examples: - For Q = 12.310, Q’10 = 100 – 12.3 = 87.710 - For Q = 228, Q’8 = 100 – 22 = 568 KFUPM

Examples Find the 10’s complement of the following decimal numbers: a. Q = 2357 Here, n = 4 M10 = 104 Q10’ = M10 – 2357 = 104 – 2357 = 7643 [10’s complement of Q] b. Q = 2895.786 Here, n = 4, m = 3 Q10’ = M10 – 2895.786 = 10000 – 2895.786 = 7104.214 KFUPM

Examples Find the 2’s complement of the following binary numbers: a. Q = 110101010 Here, n = 9 M2 = 29 Q2’ = M2 – 110101010 = 1000000000 - 110101010 = 001010110 [2’s complement of Q] a. Q = 1010.001 Here, n = 4 M2 = 24 Q2’ = M2 – 110101010 = 10000 – 1010.001 = 0101.111 [2’s complement of Q] Hint: Move from rightmost bit to the leftmost and find the first 1, all the remaining bits are flipped until the MSB is reached Example: 1100011000 2’s complement = 0011101000 KFUPM

Octal Examples Find the 7’s and 8’s complement of the following: a. Q = (6770)8 n = 4, m = 0, Q7’ = 84 – 1 – 6770 = (1007)8 Q8’ = 84 – 6770 = (1010)8 b. Q = 541.736 n = 3, m = 3, Q7’ = 83 – 8-3 – 541.736 = 777.777 – 541.736= (236.041)8 Q8’ = 83 – 541.736 = (236.042)8 KFUPM

Hexadecimal Examples Find the F’s and 16’s complement of the following: a. Q = (3FA9)16 n = 4, m = 0, QF’ = 164 – 1 – 3FA9 = (C056)16 Q16’ = 164 – 3FA9 = (C057)16 b. Q = 9B1.C70 n = 3, m = 3, QF’ =163 – 16-3 – 9B1.C70 = FFF.FFF – 9B 1.C70 = (64E.38F)16 Q16’ = 163 – 9B1.C70 = 1000 – 9B1.C70 = (64E.390)16 KFUPM

Decimal Arithmetic Example Q: Compute 4310 – 1210 using 9’s and 10’s complements. SM subtraction 9’s complement 10’s complement 43 43 43 - 12 + 87 + 88 ---------- ----------- ----------- 31 1 30 1 31 + 1 ---------- 31 Q = 12 Q’9 = 99 -12 = 87 Q’10 = 100 - 12 = 88 discard KFUPM

Conclusions Digital computers store numbers in a special electronic device (memory) called registers To represent a “signed” number, you need to specify its: Magnitude (or absolute value) Sign (positive or negative) Two common techniques for representing signed numbers are: Signed magnitude representation Complement representation: r’s complement (known as radix complement) (r-1)’s complement (also known as diminished radix complement) KFUPM