# Computer Science XI

## Number Conversions

Decimal to Binary Conversion To convert a binary number to a decimal number, it is required to multiply each binary digit by the appropriate power of 2 and add the results. There are two
approaches for converting a decimal number into binary format. 2.6.1 Repeated Division by 2 Any decimal number divided by 2 will leave a remainder of 0 or 1. Repeated division by 2 will leave a string of 0s and 1s that become the binary equivalent of the decimal number. Suppose it is required to convert the decimal number M into
binary form, dividing M by 2 in the decimal system, we will obtain a quotient M1 and a
remainder r1, where r1 can have a value of either 0 or 1. ie., M = 2 * M1 + r1 r1 = 0 or 1 Next divide the quotient M1 by 2. The new quotient will be M2 and the new
remainder r2. i.e., M1 = 2 * M2 + r2 r2 = 0 or 1 so that M = 2 (2 * M2 + r2) + r1 = 22M2 + r2 * 21+r1*20 Next divide the quotient M2 by 2. The new quotient will be M3 and the new
remainder r3. i.e., M2 = 2 * M3 + r3 so that M = 2 (2 * (2 * M3 + r3) + r2) + r1 = 22 (2 * M3 + r3) + r2 * 21 + r1 * 20 = 23 M3 + r3 * 22 + r2 * 21 + r1 * 20
The above process is repeated until the quotient becomes 0, then M = 1 * 2k + rk * 2k-1 + ... + r3 * 22 + r2 * 21 + r1 * 20 Example: Convert 2310 into its equivalent binary number. Quotient Remainder
23
To write the binary equivalent of the decimal number, read the remainders from the bottom upward as: 2310 = 101112 The number of bits in the binary number is the exponent of the smallest power of 2 that is larger than the decimal number. Consider a decimal number 23. Find the exponent of the smallest power of 2 that is larger than 23. 16 < 23 < 32 24 < 23 < 25 Hence, the number 23 has 5 bits as 10111. Consider another example. Find the number of bits in the binary representation of the decimal number 36 without actually converting into its binary equivalent. The next immediate large number than 36 that can be represented in powers of 2 is 64. 32 < 36 < 64 25 < 36 < 26 Hence, the number 36 should have 6 bits in its binary representation. 2.6.2 Sum of Powers of 2 A decimal number can be converted into a binary number by adding up the
powers of 2 and then adding bits as needed to obtain the total value of the number. For example, to convert 3610 to binary: a. Find the largest power of 2 that is smaller than or equal to 36 3610 > 3210 b. Set the 32’s bit to 1 and subtract 32 from the original number. 36 – 32 = 4 c. 16 is greater than the remaining total. Therefore, set the 16’s bit to 0 d. 8 is greater than the remaining total. Hence, set the 8’s bit to 0 e. As the remaining value is itself in powers of 2, set 4’s bit to 1 and
subtract 4 4 – 4 = 0 Conversion is complete when there is nothing left to subtract. Any remaining bits should be set to 0. Hence 36 = 1001002
24
The conversion steps can be given as follows: 32 16 8 4 2 1 1 36 – 32 = 4 32 16 8 4 2 1 1 0 0 1 4 – 4 = 0 32 16 8 4 2 1 1 0 0 1 0 0 3610 = 1001002 Example: Convert 9110 to binary using the sum of powers of 2 method. The largest power of 2 that is smaller than or equal to 91 is 64. 64 32 16 8 4 2 1 1 91-64 = 27 64 32 16 8 4 2 1 1 0 1 91-(64 + 16) = 11 (Since 32 > 27, set the 32’s bit 0 and 16 < 27. set the 16’s bit 1) 64 32 16 8 4 2 1 1 0 1 1 91-(64+16+8) = 3 64 32 16 8 4 2 1 1 0 1 1 0 1 91-(64+16+8+2) = 1 64 32 16 8 4 2 1 1 0 1 1 0 1 1 91-(64+16+8+2+1) = 0 Hence 9110 = 10110112 2.7 Conversion of fractional decimal to binary The decimal fractions like 1/2, 1/4, 1/8 etc., can be converted into exact binary fractions. Sum of powers method can be applied to these fractions. 0.510 = 1 * 2-1 = 0.12 0.2510 = 0 * 2-1 + 1 * 2-2 = 0.012 0.12510 = 0 * 2-1 + 0 * 2-2 + 1* 2 + 1 * 2-3 = 0.0012
25
The fraction 5/8 = 4/8 + 1/8 = 1/2 + 1/8 has the binary equivalent: 5/8 = 1 * 2-1 + 0 * 2-2 + 1* 2-3 = 0.1012 Exact conversion is not possible for the decimal fractions that cannot be
represented in powers of 2. For example, 0.210 cannot be exactly represented by a sum of negative powers of 2. A method of repeated multiplication by 2 has to be used to convert such kind of decimal fractions. The steps involved in the method of repeated multiplication by 2: Multiply the decimal fraction by 2 and note the integer part. The integer part is either 0 or 1. Discard the integer part of the previous product. Multiply the fractional part of the previous product by 2. Repeat the first step until the fraction repeats or terminates. The resulting integer part forms a string of 0s and 1s that become the binary equivalent of the decimal fraction. Example:

Integer part 0.2 * 2 = 0.4 0 0.4 * 2 = 0.8 0 0.8 * 2 = 1.6 1 0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
(Fraction repeats, the product is the same as in the first step) Read the integer parts from top to bottom to obtain the equivalent fractional binary number. Hence 0.210 = 0.00110011...2 2.8 Conversion of Decimal to Hexadecimal Decimal numbers’ conversion to hexadecimal is similar to binary conversion. Decimal numbers can be converted into hexadecimal format by the sum of weighted hex digits method and by repeated division by 16. The sum of weighted hex digits method is suitable for small decimal numbers of maximum 3 digits. The method of
repeated division by 16 is preferable for the conversion of larger numbers.
26
The exponent of the smallest power of 16 that is greater than the given decimal number will indicate the number of hexadecimal digits that will be present in the
converted hexadecimal number. For example, the decimal number 948, when converted into hexadecimal number has 3 hexadecimal digits. (163 = 4096) > 948 > (162 = 256) Hence, the hexadecimal representation of 948 has 3 hex digits. The conversion process is as follows: 162 161 160 3 948 – (3 * 256) = 180 162 161 160 3 B 948 – (3 * 256 + 11 * 16) = 4 162 161 160 3 B 4 948 – (3 * 256 + 11 * 16 + 4) = 0 Hence 94810 = 3B416 The steps involved in the repeated division by 16 to obtain the hexadecimal equivalent are as follows: Divide the decimal number by 16 and note the remainder. Express the remainder as a hex digit. Repeat the process until the quotient is zero Example: Process quotient remainder
948 / 16 = 59 4 (LSB)
59 / 16 = 3 11 (B) 3 / 16 = 0 3 (MSB) 94810 = 3B416 2.9 Octal Representation An octal number is represented using base 8. Octal representation is just a simple extension of binary and decimal representations but using only the digits 0 to7. To convert an octal number to a decimal number, it is required to multiply each octal digit by the appropriate power of 8 and add the results.
27
Example What is the decimal value of the octal number 7118? 7 * 82 + 1 * 81 + 1 * 80 = 45710 The steps involved in the repeated division by 8 to obtain the octal equivalent are as follows: Divide the decimal number by 8 and note the remainder. Express the remainder as an octal digit. Repeat the process until the quotient is zero What is the octal representation of the decimal number 6410?
Quotient Remainder 64/8 8 0 (LSB) 8/8 1 0 1/8 0 1 (MSB)
Hence 6410 = 1008