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 b 2 . . . b N . . . ) p Slide 2 and represent a n p n + a n − 1 p n − 1 + · · · + a 1 p 1 + a 0 p 0 + b 1 p − 1 + b 2 p − 2 + · · · + b N p − N + . . . where the digits a 0 , . . . , a n and b 1 , b 2 , . . . are all in the range 0 , . . . , ( p − 1). 1
The representation a n p n + a n − 1 p n − 1 + · · · + a 1 p 1 + a 0 p 0 + b 1 p − 1 + b 2 p − 1 + · · · + b N p − N + . . . can be used to convert to base 10 using “ordinary” decimal arithmetic. (452 . 03) 6 =4 × 6 2 + 5 × 6 1 + 2 × 6 0 + 0 × 6 − 1 + 3 × 6 − 2 Slide 3 =4 · 36 + 5 · 6 + 2 · 1 + 0 · 1 6 + 3 · 1 36 =176 . 0333333 . . . 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 . Slide 4 This is just ordinary division with remainder. Dividing 1234 by 11 gives 1234 = 112 · 11 + 2 112 is the quotient, 2 the remainder. 2
The Division Algorithm This is used to convert a decimal integer to base p . For integer a , its base p representation ( r m r m − 1 . . . r 1 r 0 ) p is calculated by successive division by p : a = q 0 p + r 0 0 ≤ r 0 < p Slide 5 q 0 = q 1 p + r 1 0 ≤ r 1 < p q 1 = q 2 p + r 2 0 ≤ r 2 < p . . . q m − 1 = q m p + r m 0 ≤ r m < p The algorithm terminates when we get a quotient q m = 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 Slide 6 1 = 0 · 11 + 1 We have reached a quotient of 0, so 2345 = (1842) 11 3
To check this note: 1 · 11 3 + 8 · 11 2 + 4 · 11 + 2 = 2345 Convert 1234 to hexadecimal (base 16) 1234 = 77 · 16 + 2 77 = 4 · 16 + 13 Slide 7 4 = 0 · 16 + 4 So 1234 = (4 D 2) 16 (n.b. D is the hexadecimal representation of 13.) 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 Slide 8 38 = 19 · 2 + 0 19 = 9 · 2 + 1 9 = 4 · 2 + 1 4 = 2 · 2 + 0 4
2 = 1 · 2 + 0 1 = 0 · 2 + 1 So 1234 = (10011010010) 2 Note that once we have converted to binary or hexadecimal, Slide 9 converting to the other is easy using the method of the previous lecture: 1234 = (100 1101 0010) 2 = (4 D 2) 16 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 Slide 10 part of a . The fractional part of a is . 456. 5
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: Slide 11 p · f = i 1 f 1 p · f 1 = i 2 f 2 p · f 2 = i 3 f 3 . . . The base p representation of f is f = (0 .i 1 i 2 . . . i n . . . ) 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 . . . . Slide 12 6
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 Slide 13 To check this (0 . 31) 4 = 3 · 4 − 1 + 1 · 4 − 2 = 3 4 + 1 16 = 0 . 8125 Convert 0 . 1 to binary 2 × 0 . 1 = 0 . 2 2 × 0 . 2 = 0 . 4 2 × 0 . 4 = 0 . 8 2 × 0 . 8 = 1 . 6 2 × 0 . 6 = 1 . 2 Slide 14 2 × 0 . 2 = 0 . 4 2 × 0 . 4 = 0 . 8 7
2 × 0 . 8 = 1 . 6 2 × 0 . 6 = 1 . 2 2 × 0 . 2 = 0 . 4 2 × 0 . 4 = 0 . 8 There is an obvious repetition here so Slide 15 0 . 1 = ( . 00011001100 . . . ) 2 a non-terminating decimal expansion. Convert 156 . 32 to hexadecimal. We do the integer and fractional parts separately: 156 = 9 · 16 + 12 9 = 0 · 16 + 9 Slide 16 So the integer part is (9 C ) 16 . 8
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 Slide 17 16 × 0 . 32 = 5 . 12 16 × 0 . 12 = 1 . 92 and the pattern 5 , 1 , 14 , 11 , 8 , 5 , 1 , . . . repeats. Thus 0 . 32 = (0 . 51 EB 851 EB 8 . . . ) 16 and 156 . 32 = (9 C. 51 EB 851 EB 8 . . . ) 16 Slide 18 9
Recommend
More recommend