Quick viewing(Text Mode)

Number Systems

Number Systems

E

Number Systems

Hereareonly ratified. —WilliamShakespeare

Naturehas some sort of -geometrical OBJECTIVES coordinate system,because In this appendix you will learn: naturehas all kinds of models.What weexperience I To understand basicnumber systems concepts, suchas of natureis in models,and base,positional valueand symbol value. all of nature’s models are so I To understand how to work withnumbers represented in beautiful. It struckme that nature’s the binary,octaland hexadecimalnumber systems. system must bea realbeauty, I Toabbreviatebinary numbers as octalnumbers or becauseinchemistry wefind hexadecimalnumbers. that the associations are always in beautiful whole I Toconvert octalnumbers and hexadecimalnumbers to numbers—thereareno binary numbers. . I Tocovnert backand forthbetween decimalnumbers and —RichardBuckminster Fuller their binary,octaland hexadecimalequivalents.

I To understand binary arithmeticand how negative binary numbers are represented using two’s complement . 1404 Appendix ENumber Systems

E.1 Introduction E.2 Abbreviating Binary Numbers as Octaland HexadecimalNumbers E.3 Converting Octaland HexadecimalNumbers toBinary Numbers

O ut line E.4 Converting from Binary,Octalor toDecimal E.5 Converting from toBinary,Octalor Hexadecimal E.6 NegativeBinary Numbers:Two’s Complement Notation Summary |Terminology |Self-Review Exercises |Answers toSelf-Review Exercises |Exercises

E.1 Introduction In this appendix, weintroduce the key systems that Javaprogrammers use,espe- cially when they are working on softwareprojects that requirecloseinteraction withma- chine-level hardware. Projects like this include operating systems, networking software,compilers,database systems and applications requiring high performance. When we writeaninteger suchas 227 or –63 in aJavaprogram, the number is assumed tobein the decimal(base10)number system. The digits in the decimalnumber system are 0,1, 2, 3,4,5, 6, 7,8and 9. The lowest digit is 0 and the highest digit is 9— one less than the baseof10.Internally, use the binary (base 2)number system. The system has only twodigits,namely 0 and 1. Its lowest digit is 0 and its highest digit is 1—one less than the baseof 2. As we will see,binary numbers tend tobemuchlonger than their decimalequivalents. Programmers who workinassembly languages and in high-level languages like Java that enable programmers to reachdown to the machine level,find it cumbersome to work with binary numbers.So twoother number systems—the octalnumber system (base8)and the hexadecimalnumber system (base16)—arepopular primarily because they make it con- venient toabbreviatebinary numbers. In the octalnumber system, the digits range from 0 to 7.Becauseboth the binary number system and the octalnumber system havefewer digits than the decimalnumber system, their digits are the same as the corresponding digits in decimal. The hexadecimalnumber system poses aproblem becauseit requires 16 digits—a lowest digit of 0 and ahighest digit witha valueequivalent todecimal15(one less than the baseof16). By convention, we use the letters A through F to represent the hexadecimal digits corresponding todecimal values 10 through 15. Thus in hexadecimal wecanhave numbers like 876 consisting solely of decimal-like digits,numbers like 8A55Fconsisting of digits and letters and numbers like FFE consisting solely of letters.Occasionally,ahexa- decimalnumber spells acommon word suchas FACE or FEED—this canappear strange toprogrammers accustomed to working withnumbers.The digits of the binary,, decimaland hexadecimalnumber systems are summarized in Fig. E.1–Fig. E.2. Eachof thesenumber systems uses positionalnotation—eachposition in whicha digit is written has adifferent positional value. For example,in the decimalnumber 937 (the 9, the 3 and the 7 are referred toas symbol values), we say that the 7 is written in the ones position, the 3 is written in the tens position and the 9is written in the hundreds position. Note that eachof thesepositions is apower of the base(base10)and that these powers begin at 0 and increaseby 1as wemoveleft in the number (Fig. E.3). E.1 Introduction 1405

Binary digit Octaldigit Decimaldigit Hexadecimaldigit

0 0 0 0 1 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 9 9 A (decimal valueof10) B (decimal valueof11) (decimal valueof12) D (decimal valueof13) E (decimal valueof14) F (decimal valueof15)

Fig. E.1 | Digits of the binary,octal,decimaland hexadecimalnumber systems.

Attribute Binary Octal Decimal Hexadecimal

Base 2 8 10 16 Lowest digit 0 0 0 0 Highest digit 1 7 9 F

Fig. E.2 | Comparing the binary,octal,decimaland hexadecimalnumber systems.

Positional values in the decimalnumber system

Decimaldigit 9 3 7 Position name Hundreds Tens Ones Positional value 100 10 1 Positional valueas a 10 2 10 1 10 0 power of the base(10)

Fig. E.3 | Positional values in the decimalnumber system.

For longer decimalnumbers, the next positions to the left would be the thousands position (10 to the 3rdpower), the ten-thousands position (10 to the 4thpower), the hun- 1406 Appendix ENumber Systems dred-thousands position (10 to the 5thpower), the millions position (10 to the 6th power), the ten-millions position (10 to the 7thpower)and soon. In the binary number 101, the rightmost 1is written in the ones position, the 0 is written in the twos position and the leftmost 1is written in the fours position. Note that eachposition is apower of the base(base 2)and that thesepowers begin at 0 and increase by 1as wemoveleft in the number (Fig. E.4). So,101= 22 + 20 =4+1=5. For longer binary numbers, the next positions to the left would be the eights position (2 to the 3rdpower), the sixteens position (2 to the 4thpower), the thirty-twos position (2 to the 5thpower), the sixty-fours position (2 to the 6thpower)and soon. In the octalnumber 425, we say that the 5is written in the ones position, the 2 is written in the eights position and the 4is written in the sixty-fours position. Note that eachof thesepositions is apower of the base(base8)and that thesepowers begin at 0 and increaseby 1as wemoveleft in the number (Fig. E.5). For longer octalnumbers, the next positions to the left would be the five-hundred- and-twelves position (8 to the 3rdpower), the four-thousand-and-ninety-sixes position (8 to the 4thpower), the thirty-two-thousand-seven-hundred-and-sixty-eights position (8 to the 5thpower)and soon. In the hexadecimalnumber 3DA, we say that the Ais written in the ones position, the Dis written in the sixteens position and the 3 is written in the two-hundred-and-fifty- sixes position. Note that eachof thesepositions is apower of the base(base16)and that thesepowers begin at 0 and increaseby 1as wemoveleft in the number (Fig. E.6). For longer hexadecimalnumbers, the next positions to the left would be the four- thousand-and-ninety-sixes position (16 to the 3rdpower), the sixty-five-thousand-five- hundred-and-thirty-sixes position (16 to the 4thpower)and soon.

Positional values in the binary number system

Binary digit 1 0 1 Position name Fours Twos Ones Positional value 4 2 1 Positional valueas a 2 2 2 1 2 0 power of the base(2)

Fig. E.4 | Positional values in the binary number system.

Positional values in the octalnumber system

Decimaldigit 4 2 5 Position name Sixty-fours Eights Ones Positional value 64 8 1 Positional valueas a 8 2 8 1 8 0 power of the base(8)

Fig. E.5 | Positional values in the octalnumber system. E.2 Abbreviating Binary Numbers as Octaland HexadecimalNumbers 1407

Positional values in the hexadecimalnumber system

Decimaldigit 3 D A Position name Two-hundred-and- Sixteens Ones fifty-sixes Positional value 256 16 1 Positional valueas a 16 2 16 1 16 0 power of the base(16)

Fig. E.6 | Positional values in the hexadecimalnumber system.

E.2 Abbreviating Binary Numbers as Octaland HexadecimalNumbers The main usefor octaland hexadecimalnumbers in is for abbreviating lengthy binary representations.FigureE.7 highlights the fact that lengthy binary numbers canbe expressed concisely in number systems withhigher bases than the binary number system.

Decimal Binary Octal Hexadecimal number representation representation representation

0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F 16 10000 20 10

Fig. E.7 | Decimal,binary,octaland hexadecimalequivalents. 1408 Appendix ENumber Systems

Aparticularly important relationship that both the octalnumber system and the hexa- decimalnumber system have to the binary system is that the bases of octaland hexadec- imal(8and 16 respectively)arepowers of the baseof the binary number system (base 2). Consider the following 12-digit binary number and its octaland hexadecimalequivalents. See if you candetermine how this relationship makes it convenient toabbreviatebinary numbers in octalor hexadecimal. The answer follows the numbers. Binary numberOctalequivalentHexadecimalequivalent 10001101000143218D1 To see how the binary number converts easily tooctal, simply break the 12-digit binary number intogroups of three consecutivebits eachand write thosegroups over the corresponding digits of the octalnumber as follows: 100 011 010001 4321 Note that the octaldigit you have written under eachgroupof thee corresponds precisely to the octalequivalent of that 3-digit binary number,as showninFig. E.7. The same kind of relationship canbeobserved in converting from binary tohexadec- imal. Break the 12-digit binary number intogroups of four consecutivebits eachand write thosegroups over the corresponding digits of the hexadecimalnumber as follows: 1000 11010001 8D1 Notice that the hexadecimaldigit you wrote under eachgroupoffour bits corre- sponds precisely to the hexadecimalequivalent of that 4-digit binary number as shownin Fig. E.7.

E.3 Converting Octaland HexadecimalNumbers to Binary Numbers In the previous section, we saw how toconvert binary numbers to their octaland hexadec- imalequivalents by forming groups of binary digits and simply rewriting them as their equivalent octaldigit values or hexadecimaldigit values.This process may be used in re- verse toproduce the binary equivalent of agiven octalor hexadecimalnumber. For example, the octalnumber 653 is converted tobinary simply by writing the 6 as its 3-digit binary equivalent 110, the 5as its 3-digit binary equivalent 101and the 3 as its 3-digit binary equivalent 011 toform the 9-digit binary number 110101011. The hexadecimalnumber FAD5is converted tobinary simply by writing the Fas its 4-digit binary equivalent 1111, the Aas its 4-digit binary equivalent 1010, the Das its 4- digit binary equivalent 1101and the 5as its 4-digit binary equivalent 0101 toform the 16-digit 1111101011010101. E.4 Converting from Binary,Octalor Hexadecimal to Decimal Weareaccustomed to working in decimal,and thereforeit is often convenient toconvert abinary,octal,or hexadecimalnumber todecimal toget a senseof what the number is “really” worth. Our diagrams in Section E.1 express the positional values in decimal. To E.5 Converting from Decimal toBinary,Octalor Hexadecimal 1409 convert anumber todecimalfrom another base,multiply the decimalequivalent of each digit by its positional valueand sum theseproducts.For example, the binary number 110101is converted todecimal53,as showninFig. E.8. Toconvert octal 7614 todecimal 3980, we use the same technique, this time using appropriateoctalpositional values,as showninFig. E.9. Toconvert hexadecimalAD3B todecimal44347, we use the same technique, this time using appropriatehexadecimalpositional values,as showninFig. E.10. E.5 Converting from Decimal toBinary,Octalor Hexadecimal The conversions in Section E.4 follow naturally from the positionalnotation conventions. Converting from decimal tobinary,octal,or hexadecimalalsofollows theseconventions.

Converting abinary number todecimal

Postional values: 32 16 8 4 2 1 Symbol values: 1 1 0 1 0 1 Products: 1*32=32 1*16=16 0*8=0 1*4=4 0*2=0 1*1=1 Sum: = 32 +16 + 0 +4+ 0s +1=53

Fig. E.8 | Converting abinary number todecimal.

Converting anoctalnumber todecimal

Positional values: 512 64 8 1 Symbol values: 7 6 1 4 Products 7*512=3584 6*64=384 1*8=8 4*1=4 Sum: = 3584 + 384 +8+4= 3980

Fig. E.9 | Converting anoctalnumber todecimal.

Converting ahexadecimalnumber todecimal

Postional values: 4096 256 16 1 Symbol values: A D 3 B Products A*4096=40960 D*256=3328 3*16=48 B*1=11 Sum: =40960 + 3328+48+11=44347

Fig. E.10 | Converting ahexadecimalnumber todecimal. 1410 Appendix ENumber Systems

Suppose we wish toconvert decimal57 tobinary.Webegin by writing the positional values of the columns right toleft until we reachacolumn whosepositional valueis greater than the decimalnumber.Wedonot need that column, so wediscardit.Thus, wefirst write: Positional values: 6432 168421 Then wediscard the column withpositional value 64,leaving: Positional values: 32 168421 Next we workfrom the leftmost column to the right.Wedivide 32 into57 and observe that thereis one 32 in 57 witha remainder of 25, so we write1in the 32 column. Wedivide 16 into 25and observe that thereis one 16 in 25 witha remainder of 9and write1in the 16 column. Wedivide 8into9and observe that thereis one 8in9 witha remainder of 1. The next twocolumns eachproducequotients of 0 when their positional values aredivided into1, so we write 0s in the 4and 2 columns.Finally,1into1is 1, so we write1in the 1column. This yields: Positional values: 32 168421 Symbol values: 11 1001 and thus decimal57 is equivalent tobinary 111001. Toconvert decimal103 tooctal, webegin by writing the positional values of the col- umns until we reachacolumn whosepositional valueis greater than the decimalnumber. Wedonot need that column, so wediscardit.Thus, wefirst write: Positional values: 512648 1 Then wediscard the column withpositional value512, yielding: Positional values: 648 1 Next we workfrom the leftmost column to the right.Wedivide 64into103 and observe that thereis one 64in103 witha remainder of 39, so we write1in the 64column. Wedivide 8into 39and observe that therearefour 8s in 39 witha remainder of 7 and write4in the 8column. Finally, wedivide 1into 7 and observe that thereare seven 1s in 7 withno remainder, so we write 7 in the 1column. This yields: Positional values: 648 1 Symbol values: 147 and thus decimal103 is equivalent tooctal147. Toconvert decimal 375 tohexadecimal, webegin by writing the positional values of the columns until we reachacolumn whosepositional valueis greater than the decimal number.Wedonot need that column, so wediscardit.Thus, wefirst write: Positional values: 4096256161 Then wediscard the column withpositional value4096, yielding: Positional values: 256161 Next we workfrom the leftmost column to the right.Wedivide 256 into 375and observe that thereis one 256 in 375 witha remainder of 119, so we write1in the 256 column. Wedivide 16 into119 and observe that thereare seven 16s in 119 witha E.6 NegativeBinary Numbers:Two’s Complement Notation 1411 remainder of 7 and write 7 in the 16 column. Finally, wedivide 1into 7 and observe that thereare seven 1s in 7 withno remainder, so we write 7 in the 1column. This yields: Positional values: 256161 Symbol values: 177 and thus decimal 375is equivalent tohexadecimal177. E.6 NegativeBinary Numbers:Two’s Complement Notation The discussion sofar in this appendix has focused on positivenumbers.In this section, we explain how computers represent negativenumbers using two’s complement notation.First weexplain how the two’s complement of abinary number is formed, then we show why it represents the negative valueof the given binary number. Consider amachine with 32- .Suppose int value=13 ;

The 32-bit representation of value is 00000000 00000000 00000000 00001101

Toform the negativeof value wefirst formits one’s complement by applying Java’s bitwise complement operator ( ~ ): onesComplementOfValue=~value;

Internally, ~value is now value witheachofits bits reversed—ones become zeros and ze- ros become ones,as follows: value : 00000000 00000000 00000000 00001101 ~value (i.e., value’s ones complement): 11111111 11111111 11111111 11110010

Toform the two’s complement of value , we simply add 1 to value ’s one’s complement. Thus

Two’s complement of value : 11111111 11111111 11111111 11110011 Now if this is in fact equal to–13, we should beable toadd it tobinary 13 and obtain a result of 0.Let us try this: 00000000 00000000 00000000 00001101 +11111111 11111111 11111111 11110011 ------00000000 00000000 00000000 00000000 The carry bit coming out of the leftmost column is discarded and weindeed get 0 as a re- sult.If weadd the one’s complement of anumber to the number, the result would beall 1s.The key togetting a result of all zeros is that the twos complement is one more than the one’s complement.The addition of 1causes eachcolumn toadd to 0 withacarry of 1. The carry keeps moving leftward until it is discarded from the leftmost bit,and thus the resulting number is all zeros. 1412 Appendix ENumber Systems

Computers actually performa subtraction, suchas x =a- value; by adding the two’s complement of value to a ,as follows: x =a+(~value+1 ); Suppose a is 27 and value is 13 as before. If the two’s complement of value is actually the negativeof value , then adding the two’s complement of value toa should produce the re- sult 14. Let us try this: a ( i.e., 27) 00000000 00000000 00000000 00011011 +(~value+1)+11111111 11111111 11111111 11110011 ------00000000 00000000 00000000 00001110 whichis indeed equal to14.

Summary •Aninteger suchas 19 or 227 or – 63 in aJavaprogramis assumed tobein the decimal(base10) number system. The digits in the decimalnumber system are 0,1, 2, 3,4,5, 6, 7,8and 9. The lowest digit is 0 and the highest digit is 9—one less than the baseof10. •Internally,computers use the binary (base 2)number system. The binary number system has only twodigits,namely 0 and 1. Its lowest digit is 0 and its highest digit is 1—one less than the baseof 2. •The octalnumber system (base8)and the hexadecimalnumber system (base16)arepopular pri- marily because they make it convenient toabbreviatebinary numbers. •The digits of the octalnumber system range from 0 to 7. •The hexadecimalnumber system poses aproblem becauseit requires 16 digits—alowest digit of 0 and ahighest digit witha valueequivalent todecimal15(one less than the baseof16). By con- vention, we use the letters A through F to represent the hexadecimaldigits corresponding todec- imal values 10 through 15. •Eachnumber system uses positionalnotation—eachposition in whichadigit is written has adif- ferent positional value. •Aparticularly important relationship of both the octalnumber system and the hexadecimalnum- ber system to the binary system is that the bases of octaland hexadecimal(8and 16 respectively) arepowers of the baseof the binary number system (base 2). •Toconvert anoctal toabinary number, replaceeachoctaldigit withits three-digit binary equiv- alent. •Toconvert ahexadecimalnumber toabinary number, simply replaceeachhexadecimaldigit withits four-digit binary equivalent. •Because weareaccustomed to working in decimal,it is convenient toconvert abinary,octalor hexadecimalnumber todecimal toget a senseof the number’s “real” worth. •Toconvert anumber todecimalfrom another base,multiply the decimalequivalent of eachdigit by its positional valueand sum the products. •Computers represent negativenumbers using two’s complement notation. •Toform the negativeofa valueinbinary,first formits one’s complement by applying Java’s bit- wisecomplement operator ( ~ ). This reverses the bits of the value. Toform the two’s complement of a value, simply add one to the value’s one’s complement. Terminology 1413

Terminology base digit base 2 number system hexadecimalnumber system base8number system negative value base10 number system octalnumber system base16 number system one’s complement notation binary number system positionalnotation bitwisecomplement operator ( ~ ) positional value conversions symbol value decimalnumber system two’s complement notation

Self-Review Exercises E.1 The bases of the decimal,binary,octaland hexadecimalnumber systems are , , and respectively. E.2 Ingeneral, the decimal,octaland hexadecimal representations of agiven binary number con- tain (more/fewer)digits than the binary number contains. E.3 ( True/False )Apopular reason for using the decimalnumber system is that it forms aconve- nient notation for abbreviating binary numbers simply by substituting one decimaldigit per groupof four binary bits. E.4 The (octal / hexadecimal / decimal) representation of alarge binary valueis the most concise (of the given alternatives). E.5 ( True/False )The highest digit in any baseis one more than the base. E.6 ( True/False )The lowest digit in any baseis one less than the base. E.7 The positional valueof the rightmost digit of any number in either binary,octal,decimalor hexadecimalis always . E.8 The positional valueof the digit to the left of the rightmost digit of any number in binary, octal,decimalor hexadecimalis always equal to . E.9 Fill in the missing values in this chart of positional values for the rightmost four positions in eachof the indicated number systems: decimal 1000 100 10 1 hexadecimal ... 256...... binary ...... octal 512... 8... E.10 Convert binary 110101011000 tooctaland tohexadecimal. E.11 Convert hexadecimalFACE tobinary. E.12 Convert octal 7316 tobinary. E.13 Convert hexadecimal4FEC tooctal. ( Hint: First convert 4FEC tobinary, then convert that binary number tooctal.) E.14 Convert binary 1101110 todecimal. E.15 Convert octal 317 todecimal. E.16 Convert hexadecimalEFD4 todecimal. E.17 Convert decimal177 tobinary, tooctaland tohexadecimal. E.18 Show the binary representation of decimal417.Then show the one’s complement of 417 and the two’s complement of 417. 1414 Appendix ENumber Systems

E.19 What is the result when anumber and its two’s complement areadded toeachother?

Answers toSelf-Review Exercises E.1 10, 2,8,16. E.2 Fewer. E.3 False. Hexadecimaldoes this. E.4 Hexadecimal. E.5 False. The highest digit in any baseis one less than the base. E.6 False. The lowest digit in any baseis zero. E.7 1(the base raised to the zeropower). E.8 The baseof the number system. E.9 Fill in the missing values in this chart of positional values for the rightmost four positions in eachof the indicated number systems:

decimal 1000 100 101 hexadecimal 4096256161 binary 842 1 octal 512 6481 E.10 Octal 6530; HexadecimalD58. E.11 Binary 1111 1010 1100 1110. E.12 Binary 111 011 001110. E.13 Binary 0 100 111 111 101100; Octal47754. E.14 Decimal 2+4+8+32+64=110. E.15 Decimal 7+1*8+3*64=7+8+192=207. E.16 Decimal4+13*16+15*256+14*4096=61396. E.17 Decimal177 tobinary:

256 128 64 32 16 84 2 1 128 64 32 16 84 2 1 (1*128)+(0*64)+(1*32)+(1*16)+(0*8)+(0*4)+(0*2)+(1*1) 10110001 tooctal:

512 6481 6481 (2*64)+(6*8)+(1*1) 261 tohexadecimal:

256 16 1 16 1 (11*16)+(1*1) (B*16)+(1*1) B1 Exercises 1415

E.18 Binary:

512 256 128 64 32 16 84 2 1 256 128 64 32 16 84 2 1 (1*256)+(1*128)+(0*64)+(1*32)+(0*16)+(0*8)+(0*4)+(0*2)+(1*1) 110100001

One’s complement: 001011110 Two’s complement: 001011111 Check:Originalbinary number +its two’s complement

110100001 001011111 ------000000000

E.19 Zero.

Exercises E.20 Some people argue that many of our calculations would beeasier in the base12 number sys- tem because12 is divisible by somany morenumbers than10 (for base10). What is the lowest digit in base12?What would be the highest symbol for the digit in base12?What are the positional values of the rightmost four positions of any number in the base12 number system? E.21 How is the highest symbol valuein the number systems wediscussed related to the positional valueof the first digit to the left of the rightmost digit of any number in thesenumber systems? E.22 Complete the following chart of positional values for the rightmost four positions in eachof the indicated number systems:

decimal 1000 100 10 1 base6 ...... 6 ... base13 ... 169...... base3 27 ......

E.23 Convert binary 100101111010 tooctaland tohexadecimal. E.24 Convert hexadecimal 3A7D tobinary. E.25 Convert hexadecimal 765F tooctal. ( Hint: First convert 765F tobinary, then convert that bi- nary number tooctal.) E.26 Convert binary 1011110 todecimal. E.27 Convert octal426 todecimal. E.28 Convert hexadecimalFFFF todecimal. E.29 Convert decimal 299 tobinary, tooctaland tohexadecimal. E.30 Show the binary representation of decimal 779. Then show the one’s complement of 779and the two’s complement of 779. E.31 What is the result when the two’s complement of anumber is added toitself? E.32 Show the two’s complement of value – 1onamachine with 32-bit integers.