You are on page 1of 5

Lab work no.

1 Conversion and operations in different numeration bases


Object of laboratory The object of this laboratory is understanding the way an integer or decimal number is converted from a base to another. This paper will focus on converting integer and decimal numbers from base 10 to a random base, especially bases 16, 2 and 8, and also on the reverse conversion, from a random base to base 10, especially from bases 16, 2 and 8. irect conversion from base 16 to bases 2 or 8, and vice versa, will also be studied. !imple operations "add, subtract# in different numeration bases will be presented later. Theoretical considerations $ numeration system consists of all the number representation rules by means of certain symbols, called digits. %or any numeration system, the number of distinct symbols for the system&s digits is e'ual with the base "b#. !o, if the base b(2 "the numbers are written in binary#, the symbols will be the 0 and 1 digits. %or base b(16 "he)adecimal# the symbols will be 0, 1, 2, *, +, ,, 6, -, 8, ., $, /, 0, , 1, %. 2ne can notice that for numbers written in a base higher than 10, other symbols "letters# are used besides the common digits for base 10. Therefore, in the case of numbers written in he)adecimal, the letters $, /, 0, , 1, % have the associated values of 10, 11, 12, 1*, 1+, 1,. To ma3e an easier distinction between numbers written in a certain base, at the the end of the number another letter is written, which represents the base, for instance4 / for numbers written in binary "base 2# 5 for numbers written in octal "base 8# for numbers written in decimal "base 10# 6 for numbers written in he)adecimal "base 16# 7sually, numbers written in decimal don&t necessarily have to be followed by the 8 9 symbol, because this base is considered to be implicit. There are others ways to write a number, li3e writing the base at the end of the number in brac3ets, for instance4 100101001 "2# or 1-$6/ "16#. :f a number in a certain base 8b9 is given, as an integer and a decimal part4 ;r "b# ( 0n 0n<1 0n<2 = 0201 00 , 1 2 2 *= , than its value in base 10 will be4 ;r "10# ( 0n > bn ? 0 n<1 > bn<1 ? = ? 0 2 > b2 ? 0 1 > b1? 0 0 > b0 ? 1 > b<1 ? 2 > b @2 ? ? * > b @* ? = Number conversion from base 10 to a random base %irst of all, it must be pointed out that for converting a number that consists of both integer and decimal part, both parts have to be converted separately.

Converting the integer part The simplest algorithm consists of successively dividing the number written in decimal to the base that the conversion will be made to "the number is divided by the base, then the 'uotient is divided by the base and so on, until the 'uotient becomes 0#, after which the rests that were obtained are ta3en, reversing the order they appeared in, which represents the value of the number in that base. 1)amples4 0onverting the integer numbers *+- and +*8 from base 10 to bases 16, 2 and 8. %irst, the numbers will be converted to base 16 because this operation is done with fewer divisions than the conversions to bases 2 and 8. *+- 16 *2 21 16 2- 16 1 16 16 5 0 0 11 1 "which is 8B9# Therefore, reversing the order of the rests we have 1,/ "6#. %urther on, one can convert to bases 2 and 8 in the same manner, but there is also a 'uic3er way to convert numbers between bases 2, 8 and 16, given the fact that for each he)a digit there is a corresponding + binary digit number, and that for each digit in octal there are * corresponding binary digits, as seen in the table below4 Aalue in decimal 0 1 2 * + , 6 8 . 10 11 12 1* 1+ 1, Aalue in he)adecimal 0 1 2 * + , 6 8 . $ / 0 1 % /inary number corresponding to he)adecimal 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 /inary number corresponding to octal 000 001 010 011 100 101 110 111

There also has to be considered that when a number is passed between bases 2, 8, 16, grouping the digits is done 8from the comma towards the e)tremities9, meaning that for

integer numbers it is done from the right to the left "by completing the number with Beros on the left side in case this is re'uired, that is on the side that doesn&t affect its value#, and for decimal numbers the grouping is done after the comma, from the left side to the right, by filling in with Beros at the right side of the number. Therefore, *+- " # ( 1,/ "6# ( 1 0101 1011 "/# ( ,** "2# +*8 " # ( 1/6 "6# ( 1 1011 0110 "/# ( 666 "2# Converting the decimal part To convert a sub<unitary number "that is the fractionary part of a number# from base 10 to a random base, a series of successive multiplications of the fractionary parts has to be done, until the fractionary part becomes null, a period is found or the representation capacity is surpassed "a sufficient number of digits has been obtained, although the algorithm hasn&t finished#. The digit that surpasses the decimal part at each multiplication is a digit of that number, in the base that the conversion is made to. $s an e)ample, it is easy to use the following scheme, in which the digits of the representation are more clearly represented with the help of the two lines. $lso, the position of the comma is more obvious "the numbers below the first multiplication, that is the ones under the line, are positioned after the comma#. 2ne must notice that only what is on the right side of the comma is multiplied. 1)ample4 0onversion of the number 0, +- " # to binary, octal and he)adecimal4 0, +->2 0 .+ 1 88 1 -6 1 ,2 1 0+ 0 08 0 16 0 *2 0 6+ 1 28 0 ,6 1 12 0 2+ 0 +8 0 .6 1 ... !o4 0, +- " # ( 0, 0111 1000 0101 0001 "/# ( 0, -8,1 "6# ( 0, *60, "2# 0onverting a number that has both an integer and a decimal part is done by successively converting the integer part and the decimal one.

1)ample4 Cepresentation in bases 2 and 16 of the real number 1+, -, De obtain4 1+ " # ( 1110 "/# ( 1 "6# and 0, -, " # ( 0, 11 "/# ( 0 "6#. Therefore 1+, -, " # ( 1110, 11 "/# ( 1, 0 "6# Converting a number from a random base to base 10 %or converting a number from a random base to base 10 one can use the formula that was defined in the first part of this lab, meaning that if a number is written in a random base 8b9 as an integer and decimal part4 ;r "b# ( 0n 0n<1 0n<2 = 0201 00 , 1 2 2 *= than its value in base 10 will be4 ;r "10# ( 0n > bn ? 0 n<1 > bn<1 ? = ? 0 2 > b2 ? 0 1 > b1? 0 0 > b0 ? 1 > b<1 ? 2 > b @2 ? ? * > b @* ? = 1)ample4 The number *$8 "6# is given in he)adecimal and its value in decimal is re'uired4 ; ( *>162 ? 10>16 1 ? 8 ( *>2,6 ? 160 ? 8 ( .*6 " # The fractionary number 0, *+1 "5# written in base 8 is given and its value in decimal is re'uired4 ; ( *>8 @1 ? +>8 @2 ?1>8 @* ( *E8 ?+E6+ ? 1E,12 ( 0. +*.+ " # The binary number 110, 11 "/# is given and its values in he)adecimal and decimal are re'uired4 ; ( 110, 11 "/# ( 6, 0 "6# ( 6, -, " #

Simple operations with numbers written in different bases %urther, the adding and subtracting of integer unsigned numbers written in binary, octal and he)adecimal is presented. Adding $dding is done by the same rules that e)ist in decimal, with the remar3 that the highest digit in a base 8b9 will be b<1 ". in decimal, - in octal, 1 in binary and % in he)adecimal#. Therefore, when a result higher than b<1 is obtained by adding two digits of ran3 8i9, a carry will appear to the digit having the 8i9?1 ran3. 2n the position ran3ed 8i9 will be the rest of the division of the result obtained by adding the ran3 8i9 digits, by the base. The carry to the 8i9?1 ran3 digit will become a new unit to the adding of the ran3 8i9?1 digits, meaning that a 1 will be added "the carry#. 1)amples4 1111 1 11 11 01010110 "/#? 1*6+ "5#? 6 8$*2 "6#? 10110101 "/# *-21 "5# **1+08 "6# 100001011 "/# ,*0, "5# $161%$ "6#

The 1 unit carry to the higher ran3ed digit was mar3ed by writing a 1 above the superior ran3ed digit to which the carry was made. The adding operation in binary is useful for representing a number in complement by 2 , when one chooses to add a 1 to the number&s representation in complement by 1 "see lab about data representation#. 1)amples4 The students have to add the 2 integer numbers *+- " # and +*8 " # that were converted earlier in the lab to bases 16 and 8, and to chec3 the result by converting it to base 10. *+- " #? +*8 " # ( -8, " # 1,/ "6# ? 1/6 "6# ( *11 "6#. 0hec3ing4 *11 "6# ( *>2,6?1>16?1 ( -8, ,** "5# ? 666 "5# ( 1+21 "5#. 0hec3ing41+21 "5# ( 1>,12?+>6+?2>8?1 ( -8, Subtraction The subtraction rules in decimal are also used for other bases4 if one can&t subtract two digits of ran3 8i9 "if the minuend&s digit is lower than the subtrahend&s#, then a unit is 8borrowed9 from the ne)t ran3ed digit "8i9?1#. :f the digit from which borrowing is desired is 0, than one borrows further from the ne)t ran3ed digit. 1)amples4 .. 01011010 "/# @ 01001100 "/# 00001110 "/# . . $* + "6# @ -,1/ "6# 21/. "6#

The students have to subtract the two integer numbers *+- " # Fi +*8 " # that were converted earlier in the lab to the numeration bases 16 and 8, and to chec3 the result by converting it to decimal.

+*8 @ *+- ( .1 " # 1/6 "6# @ 1,/ "6# ( ,/ "6#. 0hec3ing4 ,/ "6# ( ,>16?11 ( .1 666 "5# @ ,** "5# ( 1** "5#. 0hec3ing4 1** "5# ( 1>6+?*>8?* ( .1 !ubtraction is useful when it is desired to represent numbers in complement by 2 and a subtraction from 2noGofGrepreBGdigits ? 1 of the absolute value of the number is made. Lab tas s The students will do the presented conversions4 0onverting the numbers from base 10 to bases 2, 8 and 16 0onverting a number between bases 2, 8 and 16 0onverting from bases 2, 8 and 16 to base 10 0onverting decimal numbers from base 10 to bases 2 and 16 The add and subtract operations for numbers in bases 2 and 16