AMTH140 Lecture 20 Radix Conversion Slide 1 April 10, 2006 - - PDF document

amth140 lecture 20 radix conversion
SMART_READER_LITE
LIVE PREVIEW

AMTH140 Lecture 20 Radix Conversion Slide 1 April 10, 2006 - - PDF document

AMTH140 Lecture 20 Radix Conversion Slide 1 April 10, 2006 Reading: Lecture Notes 14.2 Radix Conversion Radix conversion means converting a number between one base and another. Base p numbers are written ( a n a n 1 . . . a 1 a 0 .b 1


slide-1
SLIDE 1

Slide 1

AMTH140 Lecture 20 Radix Conversion

April 10, 2006 Reading: Lecture Notes §14.2 Slide 2 Radix Conversion Radix conversion means converting a number between one base and another. Base p numbers are written (anan−1 . . . a1a0.b1b2 . . . bN . . .)p and represent anpn + an−1pn−1 + · · · + a1p1 + a0p0 + b1p−1 + b2p−2 + · · · + bNp−N + . . . where the digits a0, . . . , an and b1, b2, . . . are all in the range 0, . . . , (p − 1). 1

slide-2
SLIDE 2

Slide 3 The representation anpn + an−1pn−1 + · · · + a1p1 + a0p0 + b1p−1 + b2p−1 + · · · + bNp−N + . . . can be used to convert to base 10 using “ordinary” decimal arithmetic. (452.03)6 =4 × 62 + 5 × 61 + 2 × 60 + 0 × 6−1 + 3 × 6−2 =4 · 36 + 5 · 6 + 2 · 1 + 0 · 1 6 + 3 · 1 36 =176.0333333 . . . Slide 4 Remainder Theorem For any positive integers a and b there are integers q and b such that b = qa + r with 0 ≤ r < p The integers r and s are called the quotient and remainder of q divided by p. This is just ordinary division with remainder. Dividing 1234 by 11 gives 1234 = 112 · 11 + 2 112 is the quotient, 2 the remainder. 2

slide-3
SLIDE 3

Slide 5 The Division Algorithm This is used to convert a decimal integer to base p. For integer a, its base p representation (rmrm−1 . . . r1r0)p is calculated by successive division by p: a = q0p + r0 0 ≤ r0 < p q0 = q1p + r1 0 ≤ r1 < p q1 = q2p + r2 0 ≤ r2 < p . . . qm−1 = qmp + rm 0 ≤ rm < p Slide 6 The algorithm terminates when we get a quotient qm = 0. The remainders (in reverse order) constitute the base p digits of a. To convert 2345 to base 11: 2345 = 213 · 11 + 2 213 = 19 · 11 + 4 19 = 1 · 11 + 8 1 = 0 · 11 + 1 We have reached a quotient of 0, so 2345 = (1842)11 3

slide-4
SLIDE 4

Slide 7 To check this note: 1 · 113 + 8 · 112 + 4 · 11 + 2 = 2345 Convert 1234 to hexadecimal (base 16) 1234 = 77 · 16 + 2 77 = 4 · 16 + 13 4 = 0 · 16 + 4 So 1234 = (4D2)16 (n.b. D is the hexadecimal representation of 13.) Slide 8 Convert 1234 to binary (base 2) 1234 = 617 · 2 + 0 617 = 308 · 2 + 1 308 = 154 · 2 + 0 154 = 77 · 2 + 0 77 = 38 · 2 + 1 38 = 19 · 2 + 0 19 = 9 · 2 + 1 9 = 4 · 2 + 1 4 = 2 · 2 + 0 4

slide-5
SLIDE 5

Slide 9 2 = 1 · 2 + 0 1 = 0 · 2 + 1 So 1234 = (10011010010)2 Note that once we have converted to binary or hexadecimal, converting to the other is easy using the method of the previous lecture: 1234 = (100 1101 0010)2 = (4D2)16 Slide 10 Integer and Fractional Parts Any decimal number x can be written a = Integer Part + Fractional Part = ⌊a⌋ + f For example if a = 123.456 then ⌊a⌋ = 123, so that 123 is the integer part of a. The fractional part of a is .456. 5

slide-6
SLIDE 6

Slide 11 Conversion of the Fractional Part Conversion of the fractional part (i.e. the part after the decimal point) of a decimal number to base p is achieved by taking the integer and fractional parts on successive multiplication by p. Start with a fractional part f of a decimal number: p · f = i1 f1 p · f1 = i2 f2 p · f2 = i3 f3 . . . Slide 12 The base p representation of f is f = (0.i1i2 . . . in . . . )p If the fractional part at any step is zero, the algorithm terminates and f has a finite expansion in base p. Otherwise the algorithm can be continued forever and f has an infinite expansion like (1/3)10 = 0.333333 . . .. 6

slide-7
SLIDE 7

Slide 13 Convert 0.8125 to base 4: 4 × 0.8125 = 3 .25 4 × 0.25 = 1 .00 The fractional part is zero, so the algorithm terminates and 0.8125 = (0.31)4 To check this (0.31)4 = 3 · 4−1 + 1 · 4−2 = 3 4 + 1 16 = 0.8125 Slide 14 Convert 0.1 to binary 2 × 0.1 = .2 2 × 0.2 = .4 2 × 0.4 = .8 2 × 0.8 = 1 .6 2 × 0.6 = 1 .2 2 × 0.2 = .4 2 × 0.4 = .8 7

slide-8
SLIDE 8

Slide 15 2 × 0.8 = 1 .6 2 × 0.6 = 1 .2 2 × 0.2 = .4 2 × 0.4 = .8 There is an obvious repetition here so 0.1 = (.00011001100 . . .)2 a non-terminating decimal expansion. Slide 16 Convert 156.32 to hexadecimal. We do the integer and fractional parts separately: 156 = 9 · 16 + 12 9 = 0 · 16 + 9 So the integer part is (9C)16. 8

slide-9
SLIDE 9

Slide 17 The fractional part: 16 × 0.32 = 5 .12 16 × 0.12 = 1 .92 16 × 0.92 = 14 .72 16 × 0.72 = 11 .52 16 × 0.52 = 8 .32 16 × 0.32 = 5 .12 16 × 0.12 = 1 .92 and the pattern 5, 1, 14, 11, 8, 5, 1, . . . repeats. Slide 18 Thus 0.32 = (0.51EB851EB8 . . .)16 and 156.32 = (9C.51EB851EB8 . . .)16 9