250 likes | 395 Views
Lecture 2 Binary Arithmetic. CSCE 211 Digital Design. Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement. August 26, 2003. Overview. Last Time: Readings 1.1, 1.2 2.1-2.3 Analog vs Digital Conversion Base-r to decimal
E N D
Lecture 2Binary Arithmetic CSCE 211 Digital Design • Topics • Terminology • Fractions Base-r to decimal • Unsigned Integers • Signed magnitude • Two’s complement August 26, 2003
Overview • Last Time: Readings 1.1, 1.2 2.1-2.3 • Analog vs Digital • Conversion Base-r to decimal • Conversion decimal to Base-r • New: Readings 2.4-2.10, 3.1 • Conversion of Fractions base-r decimal • Unsigned Arithmetic • Signed Magnitude • Two’s Complement • Excess-1023 • VHDL • Introductory Examples
Review Last Time: Base-r to decimal • Converting base-r to decimal by definition • dndn-1dn-2…d 2d 1d 0 = dnrn + dn-1rn-1… d2r2 +d 1r1 + d 0r0 • Example • 4F0C = 4*163 + F*162 + 0*161 + C*160 • = 4*4096 + 15*256 + 0 + 12*1 • = 16384 + 3840 + 12 • = 20236
Review Last Time: decimal to Base-r • Repeated division algorithm • Justification: • dndn-1dn-2…d 2d 1d 0 = dnrn + dn-1rn-1… d2r2 +d 1r1 + d 0r0 • Dividing each side by r yields • (dndn-1dn-2…d 2d 1d 0) / r = dnrn-1 + dn-1rn-2… d2r1+d 1r0 + d 0r-1 • So d 0 is the remainder of the first division • ((q1) / r = dnrn-2 + dn-1rn-3… d3r1+d 2r0 + d 1r-1 • So d 1 is the remainder of the next division • and d 2 is the remainder of the next division • …
Review Last Time: decimal to Base-r • Repeated division algorithm Example • Convert 4343 to hex • 4343/16 = 271 remainder = 7 • 271/16 = 16 remainder = 15 • 16/16 = 1 remainder = 0 • 1/16 = 0 remainder = 1 • So 434310 = 10F716 • To check the answer convert back to decimal • 10F7 = 1*163 + 15*16 + 7*1 = 4096 + 240 + 7 = 4343
Review Last Time: Hex to Binary • One hex digit = four binary digits • Example • 3FAC = 0011 1111 1010 1100 (spaces just for readability) • Binary to hex four digits = one (from right!!!) • Example • 111101001111010 = 0011 1101 0011 1010 • = 3 D A
Hex to Decimal Fractions • .d-1d-2d-3…d –(n-2)d –(n-1)d -n= d-1r-1 + d-2r-2…d-(n-1)r-(n-1) +d 1r-n • Example • .1EF16 = 1*16-1 + E*16-2 + F*16-3 • = 1*.0625 + 14*.003906025 + 15*2.4414e-4 • = .117201… (probably close but not right)
Decimal Fractions to hex • .d-1d-2d-3…d –(n-2)d –(n-1)d -n= d-1r-1 + d-2r-2…d-(n-1)r-(n-1) +d 1r-n • Multiplication by r yields • r *(.d-1r-1 + d-2r-2…d-(n-1)r-(n-1) +d 1r-n • = d-1r0 + d-2r-1…d-(n-1)r-(n-2) +d 1r-(n-1) • Whole number part = d-1r0 • Multiplying again by r yields d-2r0 as the whole number part • … till fraction = 0
Example: Hex Fractions to decimal • Convert .3FA to decimal • .3FA16 = 3*16-1 + F*16-2 + A*16-3 • = 3*.0625 + 15*.00390625 +10* (1/4096) • = .191162109
Unsigned integers • What is the biggest integer representable using n-bits(n digits)? • What is its value in decimal? • Special cases • 8 bits • 16 bits • 32 bits
Arithmetic with Binary Numbers • 10010110 10010110 1101 • +00110111- 00110111x 101 • Problems with 8 bit operations • 10010110 • + 10010110
Signed integers • How do we represent? • Signed-magnitude • Excess representations • w bits 0 <= unsigned_value < 2w • In excess-B we subtract the bias (B) to get the value. • example
Complement Representations of Signed integers • One’s complement • Two’s complement
Two’s Complement Operation • One’s complement + 1 or • Find rightmost 1, complement all bits to the left of it. • Examples • 01001110 00000001 00000000 00000010
Two’s Complement Representation • Consider a two’s complement binary number • dndn-1dn-2…d 2d 1d 0 • If dn , the sign bit = 0 the number is positive and its magnitude is given by the other bits. • If dn , the sign bit = 1 the number is negative and take its two’s complement to get the magnitude. • Weighted Sum Interpretation • 0 1 • 1 2 • … • n-1 2 n-2 • n -2 n-1
Two’s Complement Representation • Consider a two’s complement binary number • dndn-1dn-2…d 2d 1d 0 • If dn , the sign bit = 0 the number is positive and its magnitude is given by the other bits. • If dn , the sign bit = 1 the number is negative and take its two’s complement to get the magnitude. • Weighted Sum • 0 1 Example 10010011 = • 1 2 • … • n-1 2 n-2 • n -2 n-1
Two’s Complement Representation • What is the 2’s complement representation in 16 bits of –5? • +7? • -1? • 0 • -2
Arithmetic with Signed Integers • Signed Magnitude Addition • if the signs are the same add the magnitude • if the signs are different subtract the smaller from the larger and use the sign of the larger • Subtraction? • Two’s complement • Just add signs take care of themselves
Basic Gates • AND • OR • NOT
Basic Gates • NAND • NOR • XOR