230 likes | 239 Views
Understand base conversions, arithmetic operations, and methods for converting numbers between different bases in digital logic design. Learn through examples and varying conversion techniques.
E N D
EEL 3705 / 3705LDigital Logic Design Fall 2006Instructor: Dr. Michael Frank Lecture Module #8:Radix Conversion M. Frank, EEL3705 Digital Logic, Fall 2006
Topics for Today (Fri. 1/19) • Administrative announcements: • Example of a design document handed out • Textbook due date pushed back to next Tue. • Lab #2 pushed back a week – • Next week’s labs are to catch up & get started • Topic: Arbitrary-radix (base-b) number systems • Base-b number representations • Commonly used bases in digital logic: 2, 8, 16 • Arithmetic in base-b number systems • Conversion between different bases M. Frank, EEL3705 Digital Logic, Fall 2006
Base Conversion Methods M. Frank, EEL3705 Digital Logic, Fall 2006
Base Conversions • Some procedures for converting a number N from an old base b to a new base B: • Method 0: If one of the bases is a power of the other, can do direct substitution of (groups of) digits. • E.g., octal ↔ binary↔ hex conversions work like this • Otherwise: • Two methods for conversion using arithmetic in the new base B: • Method 1: Directly evaluate the polynomial expression for Nb. • Method 2: (Horner’s rule) Repeatedly multiply by b and add digits of N, reading from left to right. • Two methods for conversion using arithmetic in the old base b: • Method 3: Repeatedly divide N by B, the remainders are the new digits, reading from right to left. • Method 4: Repeatedly divide N by the largest power of B less than N; the quotients are the new digits, reading from left to right. Naturalif new baseis 10 Naturalif old baseis 10 M. Frank, EEL3705 Digital Logic, Fall 2006
Example of Method 0 • Convert 3F616 to octal. • We’ll first convert to binary, then binaryoctal. • Hex to binary groups of 4 bits: • 3 00112, F 11112, 6 01102. • The complete binary representation is: • 11111101102 • Binary groups of 3 bits to octal: • 1 1, 111 7, 110 6, 110 6 • The complete octal representation is: • 17668 M. Frank, EEL3705 Digital Logic, Fall 2006
Examples of Method 1 • Convert 1011010112 to base 10. • 1011010112 = (taking the sum from right to left) = 1×20 + 1×21 + 0×22 + 1×23 + 0×24 + 1×25 + 1×26 + 0×27 + 1×28 = 1·1 + 1·2 + 0·4 + 1·8 + 0·16 + 1·32 + 1·64 + 0·128 + 1·256 = 1 + 2 + 8 + 32 + 64 + 256 = 36310. • Convert 3D9616 to base 10. • 3D9616 = 3×163 + 13×162 + 9×161 + 6×160 = 3×4,096 + 13×256 + 9×16 + 6×1 = 12,288 + 3,328 + 144 + 6 = 15,76610 M. Frank, EEL3705 Digital Logic, Fall 2006
Examples of Method 2 • Convert 1011010112 to base 10. • 1×2 + 0 = 2, ×2 + 1 = 5, ×2 + 1 = 11,×2 + 0 = 22, ×2 + 1 = 45, ×2 + 0 = 90,×2 + 1 = 181, ×2 + 1 = 36310. • Convert 3D9616 to base 10. • 3×16 = 48; +13 = 61;×16 = 976; +9 = 985;×16 = 15,760; +6 = 15,76610. M. Frank, EEL3705 Digital Logic, Fall 2006
Example of Method 3 (Dec. Binary) • Convert 36310 to binary. • 363 ÷ 2 = 181 r 1181 ÷ 2 = 90 r 190 ÷ 2 = 45 r 045 ÷ 2 = 22 r 122 ÷ 2 = 11 r 011 ÷ 2 = 5 r 15 ÷ 2 = 2 r 12 ÷ 2 = 1 r 01 ÷ 2 = 0 r 1. 1011010112 M. Frank, EEL3705 Digital Logic, Fall 2006
Method 3 Variation: Tower Drawing Style • Same algorithm,but withoutrewriting ofquotients: 1011010112 0 r 1 2 1 r 0 2 2 r 1 2 5 r 1 2 11 r 0 2 22 r 1 2 45 r 0 2 90 r 1 2 181 r 1 2 363 M. Frank, EEL3705 Digital Logic, Fall 2006
Example of Method 3 (Dec. Hex) • Convert 15,76610 to hexadecimal. • 15,766 ÷ 16 = 985 r 6;985 ÷ 16 = 61 r 9;61 ÷ 16 = 3 r 13;3 ÷ 16 = 0 r 3. 3D9616 M. Frank, EEL3705 Digital Logic, Fall 2006
Example of Method 4 (Dec. Binary) • Convert 36310 to binary. • 363 ÷ 256 = 1 r 107107 ÷ 128 = 0 r 107107 ÷ 64 = 1 r 4343 ÷ 32 = 1 r 1111 ÷ 16 = 0 r 1111 ÷ 8 = 1 r 33 ÷ 4 = 0 r 33 ÷ 2 = 1 r 11 ÷ 1 = 1 r 0 1011010112 M. Frank, EEL3705 Digital Logic, Fall 2006
Method 4 Variation • When converting to binary, the divides in method 4 can be replaced by simple subtraction of the powers of 2. Example: • Convert 36310 to binary. • 363 256 = 107 • 107 64 = 43 • 43 32 = 11 • 11 8 = 3 • 3 2 = 1 • 1 1 = 0 256 128 64 32 16 8 4 2 1 1 0 1 0 1 1 1 0 1 M. Frank, EEL3705 Digital Logic, Fall 2006
Example Where Neither Base is 10 or a Power of the Other • Convert 2103 ___5 using each of methods 1-4. • Method 1: • N = 2·32 + 1·31 + 0·30 = 2·145 + 1·3 + 0 • 2·145 = 2·105 + 2·4 = 205 + 135 = 335 • N = 335 + 3 = 305 + 115 = 415. • Method 2: • 2 · 3 = 115, + 1 = 125, · 3 = 305 + 115 = 415. • Method 3: • 2103 / 123 = 113 r 1, 113 / 123 = 0 r 113 415 • Method 4: • 2103 / 123 = 113 r 1, 1 / 1 = 1 r 0 415 Doing additionand multiplicationin base 5 1112 21012020121 Doing division in base 3 M. Frank, EEL3705 Digital Logic, Fall 2006
Another Way (Method 5) • When converting between two bases neither of which is base 10 or a power of the other, you might find the following strategy easier than using any of methods 1-4 directly: • First, convert from the old base to base 10 using either method 1 or 2 • Next, convert from base 10 to the new base using either method 3 or 4. M. Frank, EEL3705 Digital Logic, Fall 2006
Example of Method 5 • Convert 2103 ___5 using method 5. • First use method 1 to convert to base 10: • 2103 = 2·32 + 1·31 + 0·30 = 2·9 + 3 + 0 =18+3= 2110 • Next use method 4 to convert to base 5: • 2110 ÷ 5 = 4 r 1 415 M. Frank, EEL3705 Digital Logic, Fall 2006
A Modular Circuit to Convert fromBase 2 to Base 10 (Binary to BCD) We do division in old base (base 2). This can be viewed as using method 3 or 4. (Pull up bin_to_bcd project in Quartus to see details better.) M. Frank, EEL3705 Digital Logic, Fall 2006
A different circuit for converting base 2 to base 10, using method 1 • We can do the arithmetic in “base 10” by using special BCD adders… • Add up powers of 2 represented in binary-coded decimal… Constants encoding the various powers of two in BCD 64 32 16 08 04 02 01 n6..0 n6 n5 n4 n3 n2 n1 n0 BCD + BCD + BCD + BCD + Each of these buses holds two 4-bit BCD digits Each of these adders adds two 2-digit BCD numbers BCD + bcd13..0, bcd03..0 M. Frank, EEL3705 Digital Logic, Fall 2006
Fixed-Width Binary Arithmetic and Overflow M. Frank, EEL3705 Digital Logic, Fall 2006
Fixed-Width Binary Arithmetic • In digital systems, usually there is only a fixed number n of bits of storage immediately available to represent a given arithmetic result. • What happens if the numeric value of the desired result doesn’t fit in the given number of bits? • The usual behavior of hardware is just to truncate the result, in other words to discard some of the extra bits, usually on the left… • This is equivalent to using “modulo 2n” arithmetic. • We should be aware of when this procedure will lead to a different numeric result than regular arithmetic. • In other words, we should know the overflow conditions. M. Frank, EEL3705 Digital Logic, Fall 2006
Modulo-m arithmetic • In modulo-m arithmetic, all numeric results are projected onto a value in the range [0,m). • Namely, the remainder when dividing by m. • Example: 4+3 = 7 ≡ 2 (mod 5) • Since 7/5 = 1 r 2. • We can visualize the ordinary number line as laid out along a spiral straddling lines representing the different possible mod-m values. ≡ 0(mod 5) 20 15 ≡ 1(mod 5) 10 ≡ 4(mod 5) 21 19 5 14 16 9 11 0 4 6 1 3 2 8 7 13 12 18 17 22 ≡ 2(mod 5) ≡ 3(mod 5) Visualization of number line in modulo-5 arithmetic M. Frank, EEL3705 Digital Logic, Fall 2006
Modulo 2n arithmetic • Fixed-width binary arithmetic with n bits is equivalent to modulo 2n arithmetic. • Discarding the leftmost bits projects us onto one of the arms of the spiral. • An “overflow” happens when we wrap around and leave the innermost arm of the spiral. • Crossing the dotted red line… • It’s like how your car odometer rolls over after 100,000 miles. 00 100 00 11 111 11 01 101 01 10 110 10 2-bit truncation spiral M. Frank, EEL3705 Digital Logic, Fall 2006
Number Wheel Diagrams • We can represent modulo arithmetic using a “number wheel” picture • Think of this as similar to a roulette wheel, or the face of a clock • As you wrap around past the overflow marker, you start repeating the same numbers over again. overflowmarker 0 1 7 000 111 001 6 2 110 010 011 101 3 5 100 4 M. Frank, EEL3705 Digital Logic, Fall 2006
Unsigned Overflow Example • Suppose we are doing fixed-width, unsigned binary arithmetic in three bits, and we add the numbers 5 and 3. What is the result, and is there an overflow? • Expressed in 3 bits, 5 = 1012, 3 = 0112. • Carry out the addition shown at right. • The result is 10002 = 8, which does not fit in 3 bits. • The leftmost 1 gets truncated away, leaving 000 as the result. • So yes, there is an overflow! • Addition causes an overflow whenever there is a carry out of the highest-order bit position. 1 1 1 101+ 0111000 M. Frank, EEL3705 Digital Logic, Fall 2006