VHDL Design and Simulation Dr. Anthony D. Johnson radix_conversions7.fm - 1 p30 1.3.Converting Positional Between two Bases Number Representations An often encountered practical situation is that a number N is available as represented in one of the positional representations, but some processing step that is to be performed needs a different positional representation of N. This creates a conversion task that can be formally specified as, Problem: Conversion from One to Another Positional Number Representation Problem description: Given is: NrA representation of a positive mixed number N in radix rA representation, rA, rB two radixes (bases), (rB)rA representation of the radix rB in radix rA, (rA)rB representation of the radix rA in radix rB. Determine: NrB p31 representation of the number N radix rB representation. 1.3.1Conversion Methods To improve the clarity and simplicity of presentation, thefollowing descriptions of conversion techniques will assume that conversions are done: - from a given radix rA representation NrA, to a desired radix rB representation NrB. Three basic techniques applied in conversions between positional number representations are listed here together with their areas of application where they are preferable for providing a “one step” conversion using decimal arithmetic: (a) expanding positional notation technique is preferable for: - conversion from number representations in any radix rA to decimal representation, (b) division by (rB)rA (radix rB in rA representation) technique is preferable for: - conversion of integer numbers from decimal to any radix rB representation, (c) multiplication by (rB)rA technique is preferable for. - conversion of fractional numbers from decimal to any radix rB representation. This preference list shows that when one of the radixes is ten, any representation conversion can be accomplished using only decimal arithmetic in at most two conversion steps: - from any radix rA to decimal representation in a single application of technique (a), - from decimal to any radix rB representation: - for integer and fractional numbers, in a single application of technique (b) or (c) respectively, for mixed numbers, in one application of each of the techniques (b) and (c) to the integer and fractional part separately, 8/25/14 VHDL Design and Simulation Dr. Anthony D. Johnson radix_conversions7.fm - 2 One more special conversion technique exists that avoids using non decimal arithmetic when conversions between radixes 210, 810, and 1610 representations are needed: (d) grouping of digits when rA = (rB)k technique is applicable for: - conversions between representations in radixes 210, 810, and 1610: - in one step between representations in radixes 210 and 810, and 210 and 1610, - in two steps between representations in radixes 810 and 1610. For all other combinations of radixes, two variations are available of the general case method: (e) general conversion method technique is applicable for any combination od radixes: - indirect, two step method uses only decimal arithmetic: - first step: from radix rA to radix 10, - second step: from radix 10 to radix rB, - direct, one step, method uses non decimal arithmetic in the application of basic techniques (a), (b), and (c). p31 1.3.1_1 Conversion Method: Series Substitution Series Substitution conversion method is also called Expanding Positional Notation conversion method. Positional Representation of Numbers has been introduced by Equation (1.3). Nr = (an-1an-2... a2a1ao.a-1a-2 ... a-m)r = (1-3) Nr= an-1⋅rn-1+an-2⋅rn-2+... a2⋅r2+a1⋅r1+a0⋅r0+a-1⋅r-1+a-2⋅r-2+ ... a-m⋅r-m Expanding Positional Notation expands the NrA representation by suming up the products, of the powers of base rA and their corresponding digits ai; which is always conveniently doable in radix 10 arithmetic. This is practical for hand calculations, but its computer implementation would be slow due to the involved exponentiations. p32 1.3.1_2 Conversion Method: Division by Radix (rB)rA When Division by Radix technique is used, conversion of integer numbers from decimal to any other radix representation applies only decimal arithmetic . As described by the algorithm DRM, division by radix method applies a succession of divisions by the radix (rB)rA, : DRM-1: with counter i set to i=0, the initial division is applied to the fraction NrA/(rB)rA, and the quotient Q0 and remainder R0 of this division are written on the same line to the right of the fractional expression; DRM-2: incrementing the counter i by one at each successive division, divisions is applied to 8/25/14 VHDL Design and Simulation Dr. Anthony D. Johnson radix_conversions7.fm - 3 the fraction Qi-1/(rB)rA, followed by the new quotient Qi and remainder Ri written again on the same line to the right of the fractional expression; DRM-3: the last division by (rB)rA will be the one that results in a quotient whose value equals zero; the remainder Ri of this last division then becomes the MSD of NrB, and the other remainders are appended to Ri in the reverse chronological order so that: NrB = (Ri, Ri-1,..., R1, R0)rB is the result of the conversion. Examples of the application of division by radix method, from decimal representation of 2310 and 1910 to octal and binary representation are, quot. remain. NrA = 2310 23/8 2/8 (rB)rA = 810 2 0 7 2 NrB = 2 78 quot. remain. NrA = 1910 19/2 9 1 (rB)rA = 210 9/2 4 1 4/2 2 0 2/2 1 0 1/2 0 1 0 0 1 12= 1910 The apparent “magic” supporting the division by radix (rB)rA technique is more easily understood when one rewrites the explicit positional number representation of equation (1-3) as, NrA = ((( an-1⋅rA+ an-2)⋅rA+...+ a2)⋅rA+ a1)⋅rA + ao. (1-4) The form (1-4), being equivalent to radix rA expression of the number N, shows why the remainders of the successive divisions by radix (rB)rA (starting with the division of N itself, and continuing by the divisions of resulting quotients of preceding divisions) constitute the series of digits of the representation NrA. The initial division by rA is applied to the right hand side of the equation (1-4), and yields the quotient and remainder as, Q0 = (( an-1⋅rA + an-2)⋅rA+...+ a2)⋅rA+ a1 R0=ao. The second division by radix rA is applied to the quotient Q0, and yields the quotient and the remainder as, Q1 = (( an-1⋅r A+ an-2)⋅rA+...+ a3)⋅rA+ a2 R 1= a 1. And so on, until the n-th division yields the quotient dn-1, which is not divisible by rA (an-1<rA), so that the (n+1)-st division yields the quotient 0 (which is the condition for ending the division process) and the remainder an-1. 8/25/14 VHDL Design and Simulation Dr. Anthony D. Johnson radix_conversions7.fm - 4 Look up the textbook also.! p33 1.3.1_3Conversion Method: Multiplication by Radix (rB)rA When Multiplication by Radix technique is used, conversion of fractional numbers from decimal representation to any radix rB representation applies only decimal arithmetic. The information needed for the execution of MRM technique consists of: - a fractional number in radix rA representation, NrA = (a-1a-2 ...a-m)rA - radix (rB)rA of the representation NrB which should be created, - number of digits nd to be calculated for NrB. Multiplication by radix technique applies a succession of nd multiplications by the radix (rB)rA, as described by the algorithm MRM: MRM-1: Execute two initialization settings: - set counter i to i = 0, - set parameter P0 to P0 = NrA. MRM-2: Execute the following steps nd times, for i = 0 to i = nd -1: - increment i to i = i+1, calculate the product Pi-1⋅(rB)rA, and assign its value to the new parameter Pi, - set a-i = In{Pi}, - set Pi = Fr{Pi}, MRM-3: after the step MRM-2 has been applied nd times, all nd digits of the representation NrB have been calculated, and the result of conversion is: NrB = (.a-1a-2a-3 ... a-(nd-1)a-nd)rB The following two conversions are examples of the application of multiplication by radix technique. to conversions of fractional numbers 0.351610 and 0.7350510 to octal and ternary representation respectively. Conversion info: NrA = 0.351610 (rB)rA = 810 ndB = 6 Conversion process i = 1: 0.3516 ⋅8 = 2.8128 b-1 = 2 Conversion result i = 2: 0.8128 ⋅8 = 6.5024 b-2 = 6 i = 3: 0.5024 ⋅8 = 4.0192 b-3 = 4 i = 4: 0.0192 ⋅8 = 0.1536 b-4 = 0 i = 5: 0.1536 ⋅8 = 1.2288 b-5 = 1 i = 6: 0.2288 ⋅8 = 1.8304 b-6 = 1 8/25/14 NrB = 0.2640118 = 0.3515610 VHDL Design and Simulation Dr. Anthony D. Johnson radix_conversions7.fm - 5 Conversion info: Conversion process NrA = 0.7350510 i = 1: 0.73505 ⋅3 = 2.20515 b-1 = 2 (rB)rA = 310 i = 2: 0.20515 ⋅3 = 0.61545 b-2 = 0 ndB = 8 i = 3: 0.61545 ⋅3 = 1.84635 b-3 = 1 Conversion result i = 4: 0.84635 ⋅3 = 2.53905 b-4 = 2 i = 5: 0.53905 ⋅3 = 1.61715 b-5 = 1 i = 6: 0.61715 ⋅3 = 1.85145 b-6 = 1 i = 7: 0.85145 = 2.55435 b-7 = 2 i = 8: 0.55435 ⋅3 = 1.66305 b-8 = 1 NrB = 0.201211213 = 0.73494810 Understanding how the MRM technique works takes a different kind of consideration than the one used for DRM technique. p35 1.3.2 General Conversion Algorithms Algorithm 1.1 Works directly between any two bases, but may require performing arithmetic in an unfamiliar base. Algorithm 1.2Uses only decimal arithmetic, but works indirectly, in two conversions, the first to base 10, and then the second from base 10. p36 1.3.3 Conversion between radixes rA and rB when (rA)k= rB, or rA= (rB)k Bit grouping (more precisely, digit grouping) conversion method is applicable to conversion between representations in radixes rA and rB, iff rA= (rB)k or (rA)k= rB, where k is a positive integer. Practical cases: rB k rA 2 2 3 4 8 16 When rA= (rB)k and k > 0 is an integer: 1o r A > r B, 2o when the digits of rA and rB representations are denoted by DA and DB, then each DA is representable by k DB digits, 3o the following conversions are not possible by this method: - conversions involving base 10 representation, 8/25/14 VHDL Design and Simulation Dr. Anthony D. Johnson - radix_conversions7.fm - 6 direct conversions between base 8 and base 16 because, generally: lg(rA) = k⋅ lg(rB) ⇐ specifically: for rA=16 and rB=8, k= k= lg(rA) lg(rB) lg(rA) lg(rB) ≠ integer lb16 = lb8 = 4 3 ≠ integer 4o indirect conversion between base 8 and base 16 by this method involves two successive conversions: - the first to a representation in base 2, and - the second from the representation in base 2. Bit grouping conversion method is performed separately on the integer and fractional parts. When (rA)k = rB, the digits of radix rA representation of a number N are separated into groups of k consecutive digits to the left and to the right of the fractional point. This is then followed by expansion of each separate group into one radix rB digit. When rA = (rB)k, the digits of radix rA representation of a number N are converted individually into rB representation, followed by a concatenation of obtained groups of k rB digits. Algorithm 1.3 calculates only in base 10 arithmetic, but requires a longer procedure. p36 Algorithm 1.3(a) from base rB to base rA for rA= (rB)k Example 1.24 Binary to Octal p36 Algorithm 1.3(b) from base rA to base rB for rA= (rB)k Example 1.25 Hexadecimal to Octal 8/25/14 VHDL Design and Simulation Dr. Anthony D. Johnson radix_conversions7.fm - 7 CONVERSION BETWEEN PRACTICALLY SIGNIFICANT NUMBER REPRESENTATIONS CONVERSION TECHNIQUES Division & Multiplication by Radix Bit Grouping ⇔ OCTAL ⇔ BINARY ⇐ ⇐ ⇐ ⇐ ⇐ DECIMAL Expanding Positional Notation ⇐ HEXADECIMAL Calculation using Decimal Arithmetic 8/25/14 DECIMAL