Computer Arithmetic — Number Representation EPFL – I&C – LAP EPFL – CSDA and UC Davis – ACSEL.

Slides:



Advertisements
Similar presentations
HEXADECIMAL NUMBERS Code
Advertisements

ELEC353 S. al Zahir UBC Sign-Magnitude Representation High order bit is sign: 0 = positive (or zero), 1 = negative Low order bits represent the magnitude:
COE 202: Digital Logic Design Signed Numbers
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Arithmetic See: P&H Chapter 3.1-3, C.5-6.
CHAPTER 4: Representing Integer Data The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
Assembly Language and Computer Architecture Using C++ and Java
Fixed-Point Arithmetics: Part I
Assembly Language and Computer Architecture Using C++ and Java
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 =
Overview Iterative combinational circuits Binary adders
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
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.
Number Systems Lecture 02.
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
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.
General Fixed Radix Number Systems Nonredundant Positive radix, ß n digits in digit set Vector:
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
Basic Arithmetic (adding and subtracting)
#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
Computing Systems Basic arithmetic for computers.
Data Representation and Computer Arithmetic
Data Representation in Computer Systems
Chapter 4 – Arithmetic Functions and HDLs Logic and Computer Design Fundamentals.
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.
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.
CSE 241 Computer Organization Lecture # 9 Ch. 4 Computer Arithmetic Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
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 8053 Introduction to Computer Arithmetic (Website: Course & Text Content: Part 1: Number Representation.
CSC 221 Computer Organization and Assembly Language
Charles Kime & Thomas Kaminski © 2004 Pearson Education, Inc. Terms of Use (Hyperlinks are active in View Show mode) Terms of Use Logic and Computer Design.
ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were.
Computer Arithmetic See Stallings Chapter 9 Sep 10, 2009
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
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=
Unconventional Fixed-Radix Number Systems
Representing Integer Data
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic Herbert G. Mayer, PSU Status 1/14/2016 Copied with Permission from prof. Mark PSU ECE.
Two’s and one’s complement arithmetic CLOCK ARITHMETIC.
Number Representation Part 1 ECE 645: Lecture 4. Required Reading Chapter 1, Numbers and Arithmetic, Sections Chapter 2, Representing Signed Numbers.
Computer Arithmetic — Number Representation EPFL – I&C – LAP EPFL – CSDA and UC Davis – ACSEL.
Integer Operations Computer Organization and Assembly Language: Module 5.
Computer Organization 1 Data Representation Negative Integers.
General Fixed Radix Number Systems Nonredundant Positive radix, ß n digits in digit set Vector:
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Arithmetic Circuits I. 2 Iterative Combinational Circuits Like a hierachy, except functional blocks per bit.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Lecture 4: Digital Systems & Binary Numbers (4)
Addition and Subtraction
Section 2: Integer & Floating Point Numbers
King Fahd University of Petroleum and Minerals
Data Representation in Computer Systems
Arithmetic Logical Unit
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
Overview Part 1 – Design Procedure Part 2 – Combinational Logic
Presentation transcript:

Computer Arithmetic — Number Representation EPFL – I&C – LAP EPFL – CSDA and UC Davis – ACSEL

© Ienne, Oklobdzjia 2004CompArith — Number Representation2 Why Representation?  Need to map numbers to binary bits  Need a representation anyway…  Representation is consequential  Complexity of arithmetic operations depends heavily on the representation Number representation is the heart of computer arithmetic…

© Ienne, Oklobdzjia 2004CompArith — Number Representation3 What Is a Number System?  A number is represented as an ordered n-tuple of symbols (digit vector)  Each symbol is a digit  Digits usually represent integers from a given set—e.g.,

© Ienne, Oklobdzjia 2004CompArith — Number Representation4 Rule of Interpretation  Mapping from set of digit vectors to numbers (e.g., integers, reals) “twelve” digit vectorsN, Z, R,…

© Ienne, Oklobdzjia 2004CompArith — Number Representation5 Redundancy digit vectorsN, Z, R,… Nonredundant digit vectorsN, Z, R,… Redundant

© Ienne, Oklobdzjia 2004CompArith — Number Representation6 Positional Weighted Systems  The rule of interpretation is a scalar product where is the weight vector

© Ienne, Oklobdzjia 2004CompArith — Number Representation7 Radix Systems  Weights are not arbitrary but related to a radix vector in the following way

© Ienne, Oklobdzjia 2004CompArith — Number Representation8 Mixed-Radix Systems  Fixed-radix if all elements of R are identical  A few mixed-radix systems are very common—e.g., time

© Ienne, Oklobdzjia 2004CompArith — Number Representation9 Common Decimal Notation  It is weighted  It is positional  It is fixed-radix

© Ienne, Oklobdzjia 2004CompArith — Number Representation10 Common Decimal Notation  It is nonredundant because canonical  10 n possible digit vectors to represent 10 n values Weighted, positional, fixed-radix, nonredundant  also called conventional systems

© Ienne, Oklobdzjia 2004CompArith — Number Representation11 Digit Set  Canonical set A canonical system is nonredundant  Any other choice is noncanonical

© Ienne, Oklobdzjia 2004CompArith — Number Representation12 Why Redundant Representations?  Remember paper & pencil addition?  In binary:  Slow—critical path contains n full-adders: naïve delay is O(n)  Must be performed right to left (LSB to MSB) FA a n-2 b n-2 s n-2 FA a n-1 b n-1 s n-1 C out C in FA a1a1 b1b1 s1s1 a0a0 b0b0 s0s0

© Ienne, Oklobdzjia 2004CompArith — Number Representation13 Redundancy May Simplify Some Operations  Radix-2, noncanonical {0, 1, 2, 3}, 2 bits per digit digitencoding aiai ai”ai”ai’ai’ AB S 2n FA a i-1 ” b i-1 ” ai’ai’ bi’bi’ si”si”si’si’ Critical path now contains only two full-adders: delay is now O(1)

© Ienne, Oklobdzjia 2004CompArith — Number Representation14 Redundancy and MSB-first?  Now that the carry chain is broken, output digit i depends exclusively on input digits i, i – 1, i – 2  Hence I can receive the operands in serial form and, when I have seen the first three input digits, I can produce the first output digit; afterwards, every new input digit, I can produce a new output digit Online arithmetic or MSDF digit-serial arithmetic FA a i-1 ” b i-1 ” ai’ai’ bi’bi’ si”si”si’si’ FA a i-2 ”a i-1 ’ b i-1 ’

© Ienne, Oklobdzjia 2004CompArith — Number Representation15 Very Large Choice of Weighted Positional Number Systems Source: Parhami, © Oxford 2000

© Ienne, Oklobdzjia 2004CompArith — Number Representation16 Binary Representation and Negative Numbers  Simplest signed system: Sign-and-magnitude  One bit represents the sign (typ. 1   neg.)  Absolute value represented as unsigned  Exactly as humans do in decimal Of course, we could also write 0 for + and 9 for – …

© Ienne, Oklobdzjia 2004CompArith — Number Representation17 Sign-and-Magnitude Representation  Some advantages and disadvantages  Familiar for users  Simple naïve multiplication  Adders are not the most efficient  Redundant zero (+0 and –0) may cause some problems (e.g. testing for a variable =0)

© Ienne, Oklobdzjia 2004CompArith — Number Representation18 Negative Numbers As Result of Paper & Pencil Subtraction  Consider a normal paper-and-pencil subtraction 11 11 11 1 10   ……  1 2727 2626 2525 2424 2323 2020  7 10

© Ienne, Oklobdzjia 2004CompArith — Number Representation19 Intuitive Derivation of the Two’s Complement System  Two’s complement representation  Largest (positive): all ones except a n-1 = 0  Smallest (negative): all zeros except a n-1 = 1  Asymmetric range, non-redundant  Standard in computers for signed integers nowadays

© Ienne, Oklobdzjia 2004CompArith — Number Representation20 Comparison of Binary Representations on 3 Bits Decimal Binary (Unsigned) Sign and Magnitude Two’s Complement /

© Ienne, Oklobdzjia 2004CompArith — Number Representation21 Two’s Complement Representation of Negative Numbers  It “shifts” the second half of the unsigned range into the negative by 2 n  E.g., on 8 bits: Z (signed) N (unsigned)

© Ienne, Oklobdzjia 2004CompArith — Number Representation22 True-and-Complement Systems for Signed Numbers  Transform signed numbers in unsigned and then use conventional systems Z (signed)N (unsigned)digit vectors “minus two”“six”

© Ienne, Oklobdzjia 2004CompArith — Number Representation23 Mapping from Signed to Unsigned  A number A is represented through the unsigned number A R defined through a complementation constant  This is equivalent (for |A| < C ) to  For the representation to be unambiguous, it must be or at most equal… True forms Complement forms

© Ienne, Oklobdzjia 2004CompArith — Number Representation24 Inverse Mapping  Inversely  If A R = C / 2 can be represented, it can be assigned either to A = – C / 2 or to A = C / 2, but the representation is then asymmetric (  system not close under sign change operation)  If A R = C is can be represented, there are two representations for zero

© Ienne, Oklobdzjia 2004CompArith — Number Representation25 Range Complement and Digit Complement Systems  Two particular cases of true-and-complement systems  Range complement: C = r n  Digit complement: C = r n – 1  In radix-2  Two’s complement: C = 2 n  One’s complement: C = 2 n – 1 Two’s complement is asymmetric and nonredundant One’s complement is symmetric with a redundant zero

© Ienne, Oklobdzjia 2004CompArith — Number Representation26 Comparison of More Binary Representations on 3 Bits Decimal Binary (Unsigned) Sign and Magnitude Two’s Complement One’s Complement / /

© Ienne, Oklobdzjia 2004CompArith — Number Representation27 Equivalence of the Two Two’s Complement Definitions  Using the true-and-complement definition, if A R < C / 2  If A R ≥ C / 2  Hence the property

© Ienne, Oklobdzjia 2004CompArith — Number Representation28 Similar Property for One’s Complement  Using the true-and-complement definition, if A R < C / 2  If A R ≥ C / 2  Hence the property

© Ienne, Oklobdzjia 2004CompArith — Number Representation29 Sign  For sign-and-magnitude is clearly trivial…  For true-and-complement too, it is  For all these representations Sign bit

© Ienne, Oklobdzjia 2004CompArith — Number Representation30 Implied Digits in the Unsigned Representation  Unsigned numbers (like naturals in decimal) have infinite leading zeros in front of them:  …  …  Changing the representation from n bits to m bits ( m > n ) is just a matter of padding the number with leading zeros

© Ienne, Oklobdzjia 2004CompArith — Number Representation31 Changing the Number of Bits in One’s and Two’s Complement  Signed numbers can be thought as having infinite replicas of the MSB/sign in front of them  “Minus three” on 4 bits is not represented as “minus three” on 8 bits! bits 2323 2222 2020  bits 2727 2626 2525 2424 2323 2222 2020  3 10

© Ienne, Oklobdzjia 2004CompArith — Number Representation32 Sign Extension and Shift Right  Two’s complement representation on n bits  Sign extended two’s complement representation on m bits ( m > n )  To extend from n to m bits, one adds (for positive #) zero or (for negative #)

© Ienne, Oklobdzjia 2004CompArith — Number Representation33 Addition in True-and-Complement  If there is no overflow (that is, we assume the result of the sum to be representable), S = A + B can be simply computed as  Easy to prove using (X mod Y) mod Y = X mod Y

© Ienne, Oklobdzjia 2004CompArith — Number Representation34 Two’s Complement Addition  Particularly comfortable in two’s complement, because C = 2 n and performing mod 2 n implies just to ignore the carry out bit  Remember the intuitive derivation of the two’s complement representation… + AB S C out C in 0

© Ienne, Oklobdzjia 2004CompArith — Number Representation35 One’s Complement Addition  A little more complex in one’s complement, because C = r n – 1 and the modulo of W R = A R + B R is less easy  Hence

© Ienne, Oklobdzjia 2004CompArith — Number Representation36 One’s Complement Addition  End-around carry + AB S C out C in

© Ienne, Oklobdzjia 2004CompArith — Number Representation37 Negation  Bitwise negation where, as usual in binary,  Example in radix-2 on 4 bits:

© Ienne, Oklobdzjia 2004CompArith — Number Representation38 Complement and Negation in True-and-Complement  Fundamental property in n -digit r -radix systems because a n – 1 a n – 2 …a2a2 a1a1 a0a0 +A a n – 1 a n – 2 …a2a2 a1a1 a0a0 =A r – 1 … + 1=1 100…000r n

© Ienne, Oklobdzjia 2004CompArith — Number Representation39 Change of Sign  In one’s complement  In two’s complement

© Ienne, Oklobdzjia 2004CompArith — Number Representation40 Change of Sign in Two’s Complement  Another way of verifying the property  Use two’s complement property/definition

© Ienne, Oklobdzjia 2004CompArith — Number Representation41 Two’s Complement Subtracter + AB S C out C in 1

© Ienne, Oklobdzjia 2004CompArith — Number Representation42 Two’s Complement Adder/Subtracter Overflow?! + AB S C out C in Sub

© Ienne, Oklobdzjia 2004CompArith — Number Representation43 Overflows for Unsigned Addition  Limited range, thus sum of two numbers can exceed the range  Overflows if carry out is 1—that is, if the first bit which cannot be stored is not null     (> )

© Ienne, Oklobdzjia 2004CompArith — Number Representation44 Overflows for Signed Addition  The situation is somehow different and the same simple rule does not hold anymore  Carry out is 1, but now it is ok because the result is negative (think of sign extension…)     

© Ienne, Oklobdzjia 2004CompArith — Number Representation45 Overflows for Signed Addition  Overflows makes sum of positives look like negative or sum of negatives look like positive = = = = =

© Ienne, Oklobdzjia 2004CompArith — Number Representation46 Overflows Addition and Subtraction  No overflow possible if adding operands of different sign  Overflow only if  Hence OpABResult A  B  0  0 A  B  0  0 A  B  0  0 A  B  0  0

© Ienne, Oklobdzjia 2004CompArith — Number Representation47 Overflows Addition  Considering all possible cases around the last FA a n ̶ 1 b n ̶ 1 c n ̶ 1 cncn s n ̶ 1 Overflow 00000No 00101Yes 01001No Yes 11111No

© Ienne, Oklobdzjia 2004CompArith — Number Representation48 Another View on Overflow for Signed Addition  Sign extend the operands and check if the additional result bit is equal to the pervious one (sign “unextension”…)     

© Ienne, Oklobdzjia 2004CompArith — Number Representation49 Fixed Point  In real life, one does not only need integers…  If one adds a fractional point in a fixed position, hardware for integers works just as well  It’s just a matter of representation!  9 10       

© Ienne, Oklobdzjia 2004CompArith — Number Representation50 Fixed Point Requires Attention from Programmers  Multiplication often introduces the need of arithmetic shifts to the right  Note the low accuracy!      

© Ienne, Oklobdzjia 2004CompArith — Number Representation51 References  M. D. Ercegovac and T. Lang, Digital Arithmetic, Morgan Kaufmann, 2004  I. Koren, Computer Arithmetic Algorithms, Peters, 2002  A. R. Omondi, Computer Arithmetic Systems— Algorithms, Architecture and Implementation, Prentice Hall, 1991  B. Parhami, Computer Arithmetic—Algorithms and Hardware Designs, Oxford, 2000