Jones on Binary to Decimal Conversion.html 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <!-- saved from url=(0052)http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html -->
  3. <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>Jones on Binary to Decimal Conversion</title>
  5. <meta name="Author" content="Douglas W. Jones">
  6. <meta name="Language" content="English">
  7. <meta name="editor" content="/usr/bin/vi">
  8. <meta name="Description" content="A tutorial on binary to decimal conversion using limited precision">
  9. <style type="text/css">
  10. BODY { margin-left: 3%; margin-right: 3%; }
  11. H2.SQUAT { margin-top: 0.4em; margin-bottom: 0.25em; }
  12. H3.SQUAT { margin-top: 0.3em; margin-bottom: 0.2em; }
  13. H4.SQUAT { margin-top: 0.2em; margin-bottom: 0.15em; }
  14. H5.SQUAT { margin-top: 0.17em; margin-bottom: 0.15em; }
  15. * { line-height: 1.1 }
  16. P.SQUAT { margin-top: 0.25em; margin-bottom: 0.15em; }
  17. UL.SQUAT { margin-top: 0.25em; margin-bottom: 0.15em; }
  18. EM.O { font-style: normal; text-decoration: overline; }
  19. EM.U { font-style: normal; text-decoration: underline; }
  20. EM.S { font-style: normal; text-decoration: line-through; }
  21. A { text-decoration: none; }
  22. A.I { font-style: italic; text-decoration: none; }
  23. SUP { vertical-align: 0; position: relative; bottom: 1ex; }
  24. SUB { vertical-align: 0; position: relative; top: 0.8ex; }
  25. TABLE.BOXY { border: 0; padding: 0; border-spacing: 0;
  26. border-collapse: collapse; }
  27. TD.BOX { border: solid; border-width: thin; border-color: DimGray;
  28. text-align: center; }
  29. TD.BOXTT { border: solid; border-width: thin; border-color: DimGray;
  30. font-family: monospace; text-align: center; }
  31. TD.TT { font-family: monospace; text-align: left; }
  32. TD.TTSPACE { font-family: monospace; text-align: left; color: white }
  33. TD.SHADE { background: Silver; text-align: center; }
  34. CAPTION { padding-top: 6px; }
  35. DIV.HEADBOX { border: groove; border-width: 2px; background: #F0F0E0; padding-top: 1%; padding-bottom: 1%; padding-left: 5px; }
  36. DIV.HEADBOX P { margin-top: 0.8em; margin-bottom: 0.8em; }
  37. DIV.HEADBOX H1 { margin-top: 0.2em; margin-bottom: 0.4em; }
  38. DIV.HEADBOX H2 { margin-top: 0.2em; margin-bottom: 0.4em; }
  39. DIV.INDENT { border: none; padding-left: 1em }
  40. DIV.INVISIBLE { font-size: 3px; letter-spacing: -5px; color: white; background: white; }
  41. DIV.INVISIBLE A:link { color: white; }
  42. DIV.INVISIBLE A:visited { color: white; }
  43. DIV.INVISIBLE A:active { color: white; }
  44. DIV.invisible A:active { color: white; background: white; }
  45. </style>
  46. <meta name="viewport" content="width=device-width, initial-scale=1"></head>
  47. <body bgcolor="#FFFFFF" text="#000000" link="#0000CC" vlink="#880088" alink="#880088">
  48. <div class="HEADBOX" data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="197.5">
  49. <table border="0" cellspacing="0" cellpadding="0" data-pf_style_display="table" data-pf_style_visibility="visible" data-pf_rect_width="1716" data-pf_rect_height="159"><tbody data-pf_style_display="table-row-group" data-pf_style_visibility="visible" data-pf_rect_width="1716" data-pf_rect_height="159"><tr data-pf_style_display="table-row" data-pf_style_visibility="visible" data-pf_rect_width="1716" data-pf_rect_height="159"><td data-pf_style_display="table-cell" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="159">&nbsp;&nbsp;</td><td data-pf_style_display="table-cell" data-pf_style_visibility="visible" data-pf_rect_width="1708" data-pf_rect_height="158.78125">
  50. <h1 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1708" data-pf_rect_height="35">Binary to Decimal Conversion in Limited Precision</h1>
  51. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1708" data-pf_rect_height="51">
  52. Part of <a href="http://www.cs.uiowa.edu/~jones/bcd/" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="217.328125" data-pf_rect_height="17">
  53. the Arithmetic Tutorial Collection
  54. </a>
  55. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  56. by
  57. <a href="http://www.cs.uiowa.edu/~jones/" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="114.234375" data-pf_rect_height="17">Douglas W. Jones</a>
  58. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  59. <a href="http://www.uiowa.edu/" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="174.21875" data-pf_rect_height="17">
  60. T<small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="17.78125" data-pf_rect_height="15">HE</small> U<small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="70.359375" data-pf_rect_height="15">NIVERSITY</small>
  61. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="17.046875" data-pf_rect_height="15">OF</small> I<small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="30.359375" data-pf_rect_height="15">OWA</small></a>
  62. <a href="http://www.cs.uiowa.edu/" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="214.59375" data-pf_rect_height="17">Department&nbsp;of&nbsp;Computer&nbsp;Science</a>
  63. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1708" data-pf_rect_height="28">
  64. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="1674.484375" data-pf_rect_height="29">
  65. Copyright ©
  66. 1999, Douglas. W. Jones, with major revisions made in 2002 and 2010.
  67. This work may be transmitted or stored in electronic form on any
  68. computer attached to the Internet or World Wide Web so long as this
  69. notice is included in the copy. Individuals may make single copies
  70. for their own use. All other rights are reserved.
  71. </small>
  72. </p></td></tr></tbody></table>
  73. </div>
  74. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26">Index</h2>
  75. <ul data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="136">
  76. <li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#intro" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="79.09375" data-pf_rect_height="17">Introduction</a>
  77. </li><li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#basic" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="95.953125" data-pf_rect_height="17">The Basic Idea</a>
  78. </li><li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#reduce" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="119.984375" data-pf_rect_height="17">Reduction to Code</a>
  79. </li><li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#signed" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="194.203125" data-pf_rect_height="17">Dealing with Signed Numbers</a>
  80. </li><li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#precision" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="131.96875" data-pf_rect_height="17">Living Within 8 Bits</a>
  81. </li><li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#division" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="297.234375" data-pf_rect_height="17">Doing Without Hardware Multiply and Divide</a>
  82. </li><li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#radices" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="126.625" data-pf_rect_height="17">Alternative Radices</a>
  83. </li><li data-pf_style_display="list-item" data-pf_style_visibility="visible" data-pf_rect_width="1685.84375" data-pf_rect_height="17"> <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="249.296875" data-pf_rect_height="17">64-bit Conversion on a 32-bit Machine</a>
  84. </li></ul>
  85. <table border="1" width="100%" data-pf_style_display="table" data-pf_style_visibility="visible" data-pf_rect_width="1725" data-pf_rect_height="24">
  86. <tbody data-pf_style_display="table-row-group" data-pf_style_visibility="visible" data-pf_rect_width="1723" data-pf_rect_height="22"><tr data-pf_style_display="table-row" data-pf_style_visibility="visible" data-pf_rect_width="1723" data-pf_rect_height="18">
  87. <td align="CENTER" data-pf_style_display="table-cell" data-pf_style_visibility="visible" data-pf_rect_width="1719" data-pf_rect_height="18">
  88. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="337.453125" data-pf_rect_height="15">
  89. Rated as a top 50 site by
  90. <a href="http://www.programmersheaven.com/" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="118.265625" data-pf_rect_height="15">Programmer's Heaven</a>
  91. as of Feb 2002.
  92. </small>
  93. </td>
  94. </tr>
  95. </tbody></table>
  96. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="intro" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="130.265625" data-pf_rect_height="26"> Introduction </a> </h2>
  97. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  98. Programmers on binary computers have always faced computational problems
  99. when dealing with textual input-output because of our convention that printed
  100. numeric data should be represented in base 10. These problems have never
  101. been insurmountable, but they are particularly annoying when writing programs
  102. for machines with limited capacity in their arithmetic units. Suppose, for
  103. example, you have an unsigned binary number that you wish to print out in
  104. decimal form. The following C code will do this:
  105. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="84"> void putdec( unsigned int n ) {
  106. unsigned int rem = n % 10;
  107. unsigned int quot = n / 10;
  108. if (quot &gt; 0) putdec( quot );
  109. putchar( rem + '0' );
  110. }
  111. </pre>
  112. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  113. The problem with this C code is that it assumes that the computer's arithmetic
  114. unit is sufficiently large to handle the entire number n, and it assumes that
  115. the arithmetic unit can conveniently do integer division yielding both a
  116. quotient and a remainder. These assumptions are not always reasonable, both
  117. because many commercially successful computers do not include division
  118. hardware and because, even when such hardware is available, it is of little
  119. direct use when dividing numbers larger than the ALU can handle.
  120. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="51">
  121. The focus of this tutorial is on writing fast code to convert unsigned 16 bit
  122. binary numbers to decimal on a machine with an 8-bit ALU and no divide
  123. instruction. This situation is common on small microcontrollers, and the
  124. techniques generalize in useful ways, for example, to the problem of doing
  125. 64 bit binary to decimal conversion using a 32 bit ALU. A final section
  126. of this tutorial examines how these methods can be extended to larger
  127. numbers, for example, converting a 64-bit binary number to decimal on
  128. a machine with a 32-bit ALU.
  129. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  130. On machines with no divide instruction where speed is no issue, it may
  131. be better to simply use repeated subtraction instead of the fast but
  132. algebraically complex code given in the body of this tutorial.
  133. Consider the following 16-bit conversion code as an example.
  134. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="224"> void putdec( int n ) {
  135. char a, b, c, d;
  136. if (n &lt; 0) {
  137. putchar( '-' );
  138. n = -n;
  139. }
  140. for ( a = '0' - 1; n &gt;= 0; n -= 10000 ) ++a;
  141. for ( b = '9' + 1; n &lt; 0; n += 1000 ) --b;
  142. for ( c = '0' - 1; n &gt;= 0; n -= 100 ) ++c;
  143. for ( d = '9' + 1; n &lt; 0; n += 10 ) --d;
  144. putchar( a );
  145. putchar( b );
  146. putchar( c );
  147. putchar( d );
  148. putchar( n + '0' );
  149. }
  150. </pre>
  151. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  152. Variants on the clever algorithm given above have been very popular on
  153. 8-bit microprocessors and microcontrollers, despite the fact that conversions
  154. can require over 30 iterations of the for loops in this code. As given here,
  155. this code does not work for the extreme negative value -32768; fixing this
  156. bug without introducing a special case is an interesting exercise.
  157. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="0">
  158. </p><hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  159. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="basic" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="152.015625" data-pf_rect_height="26"> The Basic Idea </a> </h2>
  160. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  161. To do the conversion faster, what we'll do is look at our binary number
  162. as a base 16 number and then do the conversion in terms of the base 16
  163. digits.
  164. Consider a 16 bit number <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i>. This can be broken into 4 fields of
  165. 4 bits each, the base 16 digits; call them <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>,
  166. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> and <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>.
  167. </p><pre width="70" data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="70"> n
  168. |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|
  169. |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
  170. | n | n | n | n |
  171. 3 2 1 0
  172. </pre>
  173. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  174. The value of the number can be expressed in terms of these 4 fields as
  175. follows:
  176. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  177. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  178. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> = 4096 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 256 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  179. 16 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  180. </p></blockquote>
  181. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  182. In the same way, if the value of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i>, expressed in decimal, is
  183. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>, where each of
  184. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub>, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>,
  185. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> and <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> are decimal digits, the
  186. value of n can be expressed as:
  187. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  188. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  189. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> = 10000 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> + 1000 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  190. 100 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> + 10 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> +
  191. 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  192. </p></blockquote>
  193. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  194. Our problem then, is to compute the <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> from the
  195. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> without dealing directly with the larger
  196. number <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i>.
  197. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  198. To do this, we first note that the factors used in combining the values of
  199. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> can themselves be expressed as sums of multiples of
  200. powers of 10. Therefore, we can rewrite the original expression as:
  201. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="85">
  202. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="85">
  203. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> =
  204. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>( 4×1000 + 0×100 + 9×10 + 6×1 ) +
  205. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>( 2×100 + 5×10 + 6×1 ) +
  206. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>( 1×10 + 6×1 ) +
  207. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>( 1×1 )
  208. </p></blockquote>
  209. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  210. If distribute the <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> over the expressions for each factor,
  211. then factor out the multiples of 100, we get the following:
  212. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="85">
  213. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="85">
  214. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> =
  215. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1000 (4<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>) +
  216. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 100 (0<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 2<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>) +
  217. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 10 (9<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 5<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  218. 1<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>) +
  219. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1 (6<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 6<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  220. 6<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> +
  221. 1<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>)
  222. </p></blockquote>
  223. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  224. We can use this to arrive at first approximations <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub>
  225. for <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> through <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>:
  226. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  227. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  228. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> = 4 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>
  229. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> = 0 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 2 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>
  230. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> = 9 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 5 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  231. 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  232. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> = 6 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 6 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  233. 6 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  234. </p></blockquote>
  235. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  236. The values of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> are not proper decimal digits because
  237. they are not properly bounded in the range
  238. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> ≤ 9.
  239. Instead, given that each
  240. of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> is bounded in the range
  241. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> ≤ 15,
  242. the <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub>
  243. are bounded as follows:
  244. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  245. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  246. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> ≤ 60
  247. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> ≤ 30
  248. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> ≤ 225
  249. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> ≤ 285
  250. </p></blockquote>
  251. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  252. This is actually quite promising because <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> through
  253. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> are less than 256 and may therefore be computed using
  254. an 8 bit ALU, and even <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> may be computed in 8 bits if we
  255. can use the carry-out bit of the ALU to hold the high bit. Furthermore,
  256. if our interest is 2's complement arithmetic where the high bit is the
  257. sign bit, the first step in the output process is to print a minus sign
  258. and then negate, so the constraint on <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> becomes
  259. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> ≤ 8
  260. and we can conclude naively that
  261. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  262. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  263. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> ≤ 243
  264. </p></blockquote>
  265. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  266. Note that we said
  267. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> ≤ 8
  268. and not
  269. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> &lt; 8;
  270. this is because of the one negative number in the 2's complement system
  271. that cannot be properly negated, -32768. If we exclude this number from
  272. the range of legal values, the upper bound
  273. is reduced to 237; in fact, this is the overall upper bound, because
  274. in the one case where
  275. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> is 8, all of the other
  276. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> are zero, so we can assert globally that
  277. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  278. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  279. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> ≤ 237
  280. </p></blockquote>
  281. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  282. Even with our naive bound, however, it is easy to see that we can
  283. safely use 8 bit arithmetic to accumulate all of the
  284. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub>.
  285. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  286. Given that we have computed the <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub>, we can push them
  287. into the correct ranges by a series of 8-bit divisions and one 9-bit division,
  288. as follows:
  289. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="221">
  290. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="221">
  291. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> = <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> / 10
  292. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> = <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> mod 10
  293. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  294. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>) / 10
  295. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>) mod 10
  296. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  297. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>) / 10
  298. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>) mod 10
  299. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  300. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>) / 10
  301. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>) mod 10
  302. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  303. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> = <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub>
  304. </p></blockquote>
  305. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  306. In the above, the <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> terms represent carries propagated
  307. from one digit position to the next. The upper bounds on each of the
  308. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> can be computed from the bounds given above for the
  309. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub>:
  310. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  311. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  312. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> ≤ 28 [ = 285 / 10 ]
  313. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> ≤ 25 [ = (28 + 225) / 10
  314. = 253 / 10 ]
  315. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> ≤ 5 [ = (25 + 30) / 10 = 55 / 10 ]
  316. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> ≤ 6 [ = (5 + 60) / 10 = 65 / 10 ]
  317. </p></blockquote>
  318. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  319. In computing the bound on <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>, we came within 2 of
  320. to 255, the maximum that can be represented in 8 bits, so an 8 bit
  321. ALU is just barely sufficient for this computation. Again, if we
  322. negate any negative numbers prior to output, so that the maximum
  323. value of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> is 8, the bound on <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  324. becomes 23 instead of 28.
  325. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="0">
  326. </p><hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  327. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="reduce" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="190.671875" data-pf_rect_height="26">Reduction to Code</a> </h2>
  328. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  329. A first hack at reducing the above idea to code might include named temporaries
  330. for all of the terms used above, but we can avoid this if we reorganize
  331. things slightly and note that, after computing each digit of the number, we
  332. no-longer need one of the <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub>. This leads to the
  333. following C code:
  334. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="462"> void putdec( uint16_t n ) {
  335. uint8_t d4, d3, d2, d1, q;
  336. uint16_t d0;
  337. d0 = n &amp; 0xF;
  338. d1 = (n&gt;&gt;4) &amp; 0xF;
  339. d2 = (n&gt;&gt;8) &amp; 0xF;
  340. d3 = (n&gt;&gt;12) &amp; 0xF;
  341. d0 = 6*(d3 + d2 + d1) + d0;
  342. q = d0 / 10;
  343. d0 = d0 % 10;
  344. d1 = q + 9*d3 + 5*d2 + d1;
  345. q = d1 / 10;
  346. d1 = d1 % 10;
  347. d2 = q + 2*d2;
  348. q = d2 / 10;
  349. d2 = d2 % 10;
  350. d3 = q + 4*d3;
  351. q = d3 / 10;
  352. d3 = d3 % 10;
  353. d4 = q;
  354. putchar( d4 + '0' );
  355. putchar( d3 + '0' );
  356. putchar( d2 + '0' );
  357. putchar( d1 + '0' );
  358. putchar( d0 + '0' );
  359. }
  360. </pre>
  361. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  362. In the above,
  363. we use the standard C types <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="50.046875" data-pf_rect_height="15">uint8_t</tt> and <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="57.1875" data-pf_rect_height="15">uint16_t</tt>
  364. to represent unsigned 8 and 16-bit values. By convention, these should
  365. be defined in <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="71.484375" data-pf_rect_height="15">&lt;stdint.h&gt;</tt>.
  366. Of course, this code prints all 5 digits of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> instead of only
  367. printing the significant digits, but this can be fixed in many ways. The
  368. following fix is perhaps the cleanest, making effective use of partial
  369. results as soon as those results can be used to predict that digits will
  370. be zero. Even so, adding these features to the code obscures the
  371. computation being performed.
  372. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="574"> void putdec( uint16_t n ) {
  373. uint8_t d4, d3, d2, d1, q;
  374. uint16_t d0;
  375. d0 = n &amp; 0xF;
  376. d1 = (n&gt;&gt;4) &amp; 0xF;
  377. d2 = (n&gt;&gt;8) &amp; 0xF;
  378. d3 = (n&gt;&gt;12);
  379. d0 = 6*(d3 + d2 + d1) + d0;
  380. q = d0 / 10;
  381. d0 = d0 % 10;
  382. d1 = q + 9*d3 + 5*d2 + d1;
  383. if (d1 != 0) {
  384. q = d1 / 10;
  385. d1 = d1 % 10;
  386. d2 = q + 2*d2;
  387. if ((d2 != 0) || (d3 != 0)) {
  388. q = d2 / 10;
  389. d2 = d2 % 10;
  390. d3 = q + 4*d3;
  391. if (d3 != 0) {
  392. q = d3 / 10;
  393. d3 = d3 % 10;
  394. d4 = q;
  395. if (d4 != 0) {
  396. putchar( d4 + '0' );
  397. }
  398. putchar( d3 + '0' );
  399. }
  400. putchar( d2 + '0' );
  401. }
  402. putchar( d1 + '0' );
  403. }
  404. putchar( d0 + '0' );
  405. }
  406. </pre>
  407. <hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  408. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="signed" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="26"></a>Dealing with Signed Numbers</h2>
  409. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  410. To print signed values, we can replace the first few lines of the above
  411. code with the following:
  412. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="196"> void putdec( int16_t n ) {
  413. uint8_t d4, d3, d2, d1, d0, q;
  414. if (n &lt; 0) {
  415. putchar( '-' );
  416. n = -n;
  417. }
  418. d0 = n &amp; 0xF;
  419. d1 = (n&gt;&gt;4) &amp; 0xF;
  420. d2 = (n&gt;&gt;8) &amp; 0xF;
  421. d3 = (n&gt;&gt;12) &amp; 0xF;
  422. /* the remainder of the code is unchanged */
  423. </pre>
  424. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  425. In the above, we use the standard C type <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="50.046875" data-pf_rect_height="15">int16_t</tt>
  426. to represent signed 16-bit values and <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="50.046875" data-pf_rect_height="15">uint8_t</tt>
  427. to represent unsigned 8-bit values. By convention, these should
  428. be defined in <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="71.484375" data-pf_rect_height="15">&lt;stdint.h&gt;</tt>.
  429. As mentioned earlier, forcing <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> positive prior to breaking it down
  430. into nibbles allows us to use an 8-bit type for <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="14.296875" data-pf_rect_height="15">d0</tt> instead
  431. of the 16-bit type used previously (only 9 bits were actually needed).
  432. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  433. Note that the computation of <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="14.296875" data-pf_rect_height="15">d3</tt> has also changed as a result of
  434. the change from unsigned to signed arithmetic. This is because the
  435. right-shift operator shifts zeros into the high bit when applied to
  436. unsigned numbers, but it replicates the sign bit when applied to signed
  437. numbers. There is one case in 16 bit 2's complement arithmetic where
  438. both <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> and -<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> are negative, that is, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> = -32768.
  439. The change we have made gives us the correct output in this case.
  440. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="0">
  441. </p><hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  442. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="precision" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="207.859375" data-pf_rect_height="26">Living Within 8 Bits</a> </h2>
  443. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  444. In the code given above for unsigned printing, the fact that
  445. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> could be as large as 285 forced us to declare the
  446. variable <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="14.296875" data-pf_rect_height="15">d0</tt> as a 16-bit integer instead of just 8 bits.
  447. We can avoid this by observing that the final addition in the sum
  448. used to compute <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> is the only one that will push
  449. this sum over 255, and therefore, we need only check the carry bit once
  450. to detect overflow. Having detected overflow, we can account for it in
  451. the computation that follows:
  452. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="308"> void putdec( uint16_t n ) {
  453. uint8_t d4, d3, d2, d1, d0, q;
  454. d0 = n &amp; 0xF;
  455. d1 = (n&gt;&gt;4) &amp; 0xF;
  456. d2 = (n&gt;&gt;8) &amp; 0xF;
  457. d3 = (n&gt;&gt;12) &amp; 0xF;
  458. if ( (6*(d3 + d2 + d1) + d0) &gt; 255 ) { /* overflow */
  459. d0 = (6*(d3 + d2 + d1) + d0) &amp; 0xFF;
  460. q = d0/10 + 25;
  461. d0 = d0 % 10 + 6;
  462. if (d0 &gt;= 10) {
  463. d0 = d0 - 10;
  464. q = q + 1;
  465. }
  466. } else { /* no overflow */
  467. d0 = 6*(d3 + d2 + d1) + d0;
  468. q = d0 / 10;
  469. d0 = d0 % 10;
  470. }
  471. </pre>
  472. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  473. In the above code, after having noted an overflow in the computation of
  474. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>, we retain the least significant bits of the sum
  475. in <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="14.296875" data-pf_rect_height="15">d0</tt> and then approximately correct the quotient by
  476. adding 256/10, or 25; correcting this and computing the correct
  477. remainder is a bit more interesting.
  478. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  479. Given some integer <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> ≥ 256, if we want to compute
  480. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> mod 10 from (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> - 256) mod 10, we need to note that
  481. the modulus operator is distributive in an interesting sense:
  482. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  483. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  484. (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">j</i>) mod <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="11.5625" data-pf_rect_height="17">m</i> =
  485. ((<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> mod <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="11.5625" data-pf_rect_height="17">m</i>) + (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">j</i> mod <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="11.5625" data-pf_rect_height="17">m</i>)) mod <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="11.5625" data-pf_rect_height="17">m</i>
  486. </p></blockquote>
  487. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  488. Using this, we can conclude that:
  489. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="34">
  490. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="34">
  491. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> mod 10 = (((<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> - 256) mod 10) + (256 mod 10)) mod 10
  492. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"> = (((<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> - 256) mod 10) + 6) mod 10
  493. </p></blockquote>
  494. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  495. In the case where ((<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i> - 256) mod 10) + 6 is greater than 10,
  496. the truncation of the quotient was also incorrect, so we must add 1
  497. to the approximate quotient as well. This justifies the code given above,
  498. and this code has also been tested exhaustively for all integers from
  499. 0 to 65535.
  500. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="0">
  501. </p><hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  502. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="division" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="475.40625" data-pf_rect_height="26">Doing Without Hardware Multiply and Divide</a> </h2>
  503. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  504. If flat-out speed is not of the essence, the easiest way to do the
  505. required divisions is to use the trivial repeated subtraction algorithm.
  506. The largest dividend we must consider is only 285, so it will take no
  507. more than 28 iterations for this division.
  508. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="154"> /* quotient and remainder returned by div10 */
  509. unsigned char q, r;
  510. void div10( uint8_t i ) {
  511. q = 0;
  512. r = i;
  513. while (r &gt; 9) {
  514. q = q + 1;
  515. r = r - 10;
  516. }
  517. }
  518. </pre>
  519. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  520. This can be optimized in several ways. Most notably, termination conditions
  521. that compare with zero are almost always significantly faster than those
  522. that compare with other constants, so we might be tempted to write something
  523. like this:
  524. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="168"> /* quotient and remainder returned by div10 */
  525. uint8_t q, r;
  526. void div10( uint8_t i ) {
  527. q = -1;
  528. r = i;
  529. do {
  530. q = q + 1;
  531. r = r - 10;
  532. } while (r &gt;= 0); /* see note below */
  533. r = r + 10;
  534. }
  535. </pre>
  536. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  537. The above C code is wrong because <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.15625" data-pf_rect_height="15">r</tt> is declared to be unsigned leading
  538. to the fact that the comparison <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="28.59375" data-pf_rect_height="15">r&gt;=0</tt> will always return true.
  539. Note that in assembly language, this code can be fixed by terminating the loop
  540. when the condition codes indicate a borrow after the <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="28.59375" data-pf_rect_height="15">r-10</tt> subtraction.
  541. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  542. <a href="http://homepage.cs.uiowa.edu/~jones/bcd/divide.html" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="196.40625" data-pf_rect_height="17">Another section of this tutorial</a>
  543. covers the problem of doing multiplicaton and division using only shift
  544. and add instructions. We use the results presented there, taking specific
  545. advantage of the limited ranges of values occupied by our dividends, to
  546. completely eliminate iteration from our code.
  547. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  548. There are two ways to get an exact quotient and remainder from
  549. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="4.453125" data-pf_rect_height="17">i</i>/10 using reciprocal multiplication in 8 bits.
  550. Either we multiply by 0.00011001 to get an approximate quotient and then
  551. compute the remainder and correct the quotient, or we multiply by
  552. 0.00011001101 to get an exact quotient from which we can compute the
  553. remainder.
  554. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  555. The former method, yielding an approximation, can be reduced to the
  556. following C code:
  557. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="252"> /* quotient and remainder returned by div10 */
  558. uint8_t q, r;
  559. void div10( uint8_t i ) {
  560. /* approximate the quotient as q = i*0.000110011 (binary) */
  561. q = ((i&gt;&gt;1) + i) &gt;&gt; 1; /* times 0.11 */
  562. q = ((q&gt;&gt;4) + q) &gt;&gt; 3; /* times 0.000110011 */
  563. /* compute the reimainder as r = i - 10*q */
  564. r = ((q&lt;&lt;2) + q) &lt;&lt; 1; /* times 1010. */
  565. r = i - r;
  566. /* fixup if approximate remainder out of bounds */
  567. if (r &gt;= 10) {
  568. r = r - 10;
  569. q = q + 1;
  570. }
  571. }
  572. </pre>
  573. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  574. In first two right-shift-and-add statements used above, we have
  575. assumed that the carry out of the high bit of the add operation
  576. is shifted into the high bit of the second of the two right-shift
  577. operations used in each statement. This can be done efficiently
  578. in many machine instruction sets, but even good optimizing compilers
  579. may insist on using a 16 bit accumulator for all intermediate
  580. results of arithmetic operations.
  581. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  582. The second approach, using a more elaborate multiplier to give an
  583. exact quotient, may be coded as follows:
  584. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="196"> /* quotient and remainder returned by div10 */
  585. uint8_t q, r;
  586. void div10( uint8_t i ) {
  587. /* approximate the quotient as q = i*0.00011001101 (binary) */
  588. q = ((i&gt;&gt;2) + i) &gt;&gt; 1; /* times 0.101 */
  589. q = ((q ) + i) &gt;&gt; 1; /* times 0.1101 */
  590. q = ((q&gt;&gt;2) + i) &gt;&gt; 1; /* times 0.1001101 */
  591. q = ((q ) + i) &gt;&gt; 4; /* times 0.00011001101 */
  592. /* compute the reimainder as r = i - 10*q */
  593. r = ((q&lt;&lt;2) + q) &lt;&lt; 1; /* times 1010. */
  594. r = i - r;
  595. }
  596. </pre>
  597. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  598. The particular choice of one or the other of these schemes can only be
  599. made after compiling them or hand translation to machine code. Depending
  600. on the specific features offered by the target architecture, either of the
  601. above may be the fastest or most compact, and careful use of small
  602. optimizations may tilt the balance one way or the other.
  603. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  604. Depending on the space-time tradeoffs of the application, the above
  605. code may be expanded in-line as an open macro, or it may be called as a
  606. closed function. If in-line expansion is appropriate, it should be noted
  607. that the computations of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> and <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub>
  608. involve dividends no greater than 65. For dividends up to 68, the
  609. multiplier 0.0001101 gives exact results, suggesting the following version
  610. of the print routine:
  611. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="476"> void putdec( int16_t n ) {
  612. uint8_t d4, d3, d2, d1, d0, q;
  613. if (n &lt; 0) {
  614. putchar( '-' );
  615. n = -n;
  616. }
  617. d1 = (n&gt;&gt;4) &amp; 0xF;
  618. d2 = (n&gt;&gt;8) &amp; 0xF;
  619. d3 = (n&gt;&gt;12) &amp; 0xF;
  620. d0 = 6*(d3 + d2 + d1) + (n &amp; 0xF);
  621. q = (d0 * 0xCD) &gt;&gt; 11;
  622. d0 = d0 - 10*q;
  623. d1 = q + 9*d3 + 5*d2 + d1;
  624. q = (d1 * 0xCD) &gt;&gt; 11;
  625. d1 = d1 - 10*q;
  626. d2 = q + 2*d2;
  627. q = (d2 * 0x1A) &gt;&gt; 8;
  628. d2 = d2 - 10*q;
  629. d3 = q + 4*d3;
  630. d4 = (d3 * 0x1A) &gt;&gt; 8;
  631. d3 = d3 - 10*d4;
  632. putchar( d4 + '0' );
  633. putchar( d3 + '0' );
  634. putchar( d2 + '0' );
  635. putchar( d1 + '0' );
  636. putchar( d0 + '0' );
  637. }
  638. </pre>
  639. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  640. All multiplications in the above code can be reduced to short sequences
  641. of shift and add instructions, and it is straightforward to add the code
  642. discussed earlier to suppress leading zeros from this code.
  643. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  644. All of these details are incorporated into the attached
  645. <a href="http://homepage.cs.uiowa.edu/~jones/bcd/decimalpic.html" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="428.34375" data-pf_rect_height="17">
  646. decimal print routine for the 14-bit family of PIC microcontrollers.</a>
  647. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="0">
  648. </p><hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  649. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="radices" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="201.953125" data-pf_rect_height="26">Alternative Radices</a> </h2>
  650. <blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  651. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14"><small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="378.484375" data-pf_rect_height="15">
  652. <b data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="31.09375" data-pf_rect_height="15">Note:</b> The following section was added between
  653. August 6 and 8, 2012.
  654. </small>
  655. </p></blockquote>
  656. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  657. All of the above work was based on dividing the 16-bit number to be printed
  658. into four 4-bit chunks. That is, we worked in radix 16. In fact, there is
  659. no reason to work in a fixed radix. Consider, for example, breaking a
  660. 16-bit number into 4 chunks as follows:
  661. </p><pre width="70" data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="70"> n
  662. |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|
  663. |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
  664. | n | n | n | n |
  665. 3 2 1 0
  666. </pre>
  667. <blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  668. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  669. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> = 8192 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 512 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  670. 32 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  671. </p></blockquote>
  672. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  673. This leads to the following initial approximations for the decimal digits
  674. of the number:
  675. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  676. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  677. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> = 8 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>
  678. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> = 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 5 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>
  679. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> = 9 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  680. 3 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  681. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> = 2 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 2 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  682. 2 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  683. </p></blockquote>
  684. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  685. Shifting to a mixed radix system instead of pure base 16 has the net effect
  686. of changing the range of values in each of initial approximations for the
  687. decimal digits:
  688. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  689. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  690. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> ≤ 65
  691. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> ≤ 95
  692. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> ≤ 133
  693. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> ≤ 105
  694. </p></blockquote>
  695. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  696. Enlarging <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> to 5 bits had no major effect because the
  697. multiplier on that term is 1. Shifting the other terms over one place
  698. changed 3 multiplers from 6 to 12, resulting in smaller terms.
  699. As a result, 8-bit arithmetic now suffices for all of the computations.
  700. Reducing this to C code, we get:
  701. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="420"> void putdec( uint16_t n ) {
  702. uint8_t d4, d3, d2, d1, d0, q;
  703. d0 = n &amp; 0x1F;
  704. d1 = (n&gt;&gt;5) &amp; 0xF;
  705. d2 = (n&gt;&gt;9) &amp; 0xF;
  706. d3 = (n&gt;&gt;13) &amp; 0x7;
  707. d0 = 2*(d3 + d2 + d1) + d0;
  708. q = (d0 * (uint16_t)0x67) &gt;&gt; 10;
  709. d0 = d0 - 10*q;
  710. d1 = 9*d3 + d2 + 3*d1 + q;
  711. q = (d1 * (uint16_t)0x67) &gt;&gt; 10;
  712. d1 = d1 - 10*q;
  713. d2 = d3 + 5*d2 + q;
  714. q = (d2 * (uint16_t)0x67) &gt;&gt; 10;
  715. d2 = d2 - 10*q;
  716. d3 = 8*d3 + q;
  717. q = (d3 * (uint16_t)0x1A) &gt;&gt; 8;
  718. d3 = d3 - 10*q;
  719. putchar(q + '0');
  720. putchar(d3 + '0');
  721. putchar(d2 + '0');
  722. putchar(d1 + '0');
  723. putchar(d0 + '0');
  724. }
  725. </pre>
  726. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  727. Enlarging <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> to 5 bits had a second consequence. Because
  728. the upper bounds on the larger terms have been reduced, shortened
  729. approximations for one tenth can be used. On small
  730. microcontrollers without hardware support for multiplication, this can
  731. result in savings of a few instructions per multiply.
  732. The multiplier 67<sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="13.34375" data-pf_rect_height="15">16</sub> works because 0.0001100111 approximates one
  733. tenth to sufficient precision to divide any number less than 179 by ten.
  734. The final digit, <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="14.296875" data-pf_rect_height="15">d3</tt>, can still be computed
  735. using the even shorter approximation we used previously.
  736. </p><hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  737. <h2 data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="26"> <a name="sixtyfour" data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="394.015625" data-pf_rect_height="26">64-bit Conversion on a 32-bit Machine</a> </h2>
  738. <blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  739. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14"><small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="1248.890625" data-pf_rect_height="15">
  740. <b data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="31.09375" data-pf_rect_height="15">Note:</b> The following section was added on
  741. June 8, 2010. As of June 10, the constants given in this section
  742. and the code at the end have been checked
  743. and appear to be correct. As of August 13, this code has
  744. undergone extensive testing.
  745. </small>
  746. </p></blockquote>
  747. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="51">
  748. The techniques explored above are applicable wherever a large integer
  749. must be converted to decimal on a machine with a smaller ALU. For
  750. example, consider the problem of converting 64-bit integers to decimal
  751. on a machine with a 32-bit ALU. This practical problem comes up, for
  752. example, in the BIOS code for Intel-architecture machines, where the
  753. code must not assume a 64-bit CPU but must still be able to report
  754. 64-bit values when it reports on such things as the sizes of the
  755. attached disk drives. When 64-bit arithmetic is supported on a 32-bit
  756. machine, it is slow enough that significant speedups can be achieved
  757. by avoiding it.
  758. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  759. In general, 64-bit values may have up to 20 decimal digits:
  760. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  761. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  762. 2<sup data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="13.34375" data-pf_rect_height="15">64</sup> = 18,446,744,073,709,551,616
  763. </p></blockquote>
  764. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  765. Normally, we group decimal digits in groups of 3 using commas, as
  766. illustrated above. This is equivalent to writing the number in base 1000,
  767. where each base 1000 digit is represented with 3 consecutive decimal digits.
  768. Because there are 20 digits, grouping them as 5 blocks of 4 makes sense,
  769. suggesting that we should work in base 10,000. In addition, 10,000
  770. is the largest power of 10 that can be represented in 16 bits
  771. (half the precision of our ALU). Therefore, we will write:
  772. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  773. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  774. 2<sup data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="13.34375" data-pf_rect_height="15">64</sup> = 1844,6744,0737,0955,1616
  775. </p></blockquote>
  776. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  777. We begin by breaking our 64-bit number into 16-bit chunks, just as we
  778. began the 16-bit conversion by breaking the number into 4-bit chunks.
  779. Again, we will the number <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> and we will call the 16-bit chunks
  780. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  781. and <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>.
  782. </p><pre width="70" data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="70"> n
  783. |___ ___ ___ ___ ___ ___ ___ ___| 64-bits
  784. |___|___|___|___|___|___|___|___| 8 bytes
  785. | n | n | n | n | 4 16-bit chunks
  786. 3 2 1 0
  787. </pre>
  788. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  789. The value of the number can be expressed in terms of these 4 fields as
  790. follows:
  791. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="118">
  792. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="17">
  793. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> = 2<sup data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="13.34375" data-pf_rect_height="15">48</sup><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  794. 2<sup data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="13.34375" data-pf_rect_height="15">32</sup><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  795. 2<sup data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="13.34375" data-pf_rect_height="15">16</sup><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> +
  796. 2<sup data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sup><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  797. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="85">
  798. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> =
  799. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 281,474,976,710,656 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  800. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 4,294,967,296 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  801. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 65,536 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> +
  802. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  803. </p></blockquote>
  804. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  805. Because we are interested in working in base 10,000, we will regroup the
  806. digits above and continue with the following version:
  807. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="85">
  808. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> =
  809. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 281,4749,7671,0656 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  810. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 42,9496,7296 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  811. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 6,5536 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> +
  812. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  813. </blockquote>
  814. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  815. Our goal is to express <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> in base 10,000 as
  816. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>.
  817. If each of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub>, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>, <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>,
  818. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> and <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> is a single base 10,000
  819. digit, the value of n can be expressed as:
  820. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="102">
  821. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="102">
  822. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> =
  823. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1,0000,0000,0000,0000 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> +
  824. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1,0000,0000,0000 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  825. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1,0000,0000 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  826. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1,0000 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> +
  827. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  828. </p></blockquote>
  829. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  830. Our initial approximations of the decimal digits comes from rearranging
  831. the base 10,000 digits of the multipliers for the 16-bit chunks to conform
  832. with the above decimal presentation:
  833. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="0">
  834. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="85">
  835. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i> =
  836. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1,0000,0000,0000 ( 281 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> ) +
  837. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1,0000,0000 ( 4749 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  838. 42 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> ) +
  839. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1,0000 ( 7671 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 9496 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  840. 6 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> ) +
  841. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">&nbsp; 1 ( 0656 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 7296 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  842. 5536 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> )
  843. </blockquote>
  844. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  845. The approximations for the four least significant base 10,000 digits fall out
  846. of the above:
  847. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  848. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  849. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> = 281 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>
  850. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> = 4749 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + 42 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>
  851. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> = 7671 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  852. 9496 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> + 6 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  853. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> = 0656 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> +
  854. 7296 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> +
  855. 5536 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + 1 <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  856. </p></blockquote>
  857. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  858. At this point, just as we did with the 4-bit example, we should check the
  859. range of values we might encounter in each approximation. Here, our
  860. concern is that some of the approximations might exceed the capacity of a
  861. 32-bit ALU. The maximum value of each term will occur when all of the
  862. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> are at their maximum value, which is 65,535.
  863. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  864. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  865. 0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>
  866. ≤ 18,415,335
  867. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>
  868. ≤ 313,978,185
  869. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  870. ≤ 1,125,432,555
  871. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">0 ≤ <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  872. ≤ 884,001,615
  873. </p></blockquote>
  874. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  875. All of our approximations are comfortably less than 2 billion, which means
  876. that we can safely use 32-bit signed division, if that is all that is
  877. available to us.
  878. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  879. We are now ready to compute the final values of the digits by a
  880. series of 32-bit divisions, as follows:
  881. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="221">
  882. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="221">
  883. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  884. = <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> / 10,000
  885. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub>
  886. = <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> mod 10,000
  887. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  888. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>
  889. = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>) / 10,000
  890. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>
  891. = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>) mod 10,000
  892. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  893. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>
  894. = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>) / 10,000
  895. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>
  896. = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>) mod 10,000
  897. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  898. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub>
  899. = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>) / 10,000
  900. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>
  901. = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>) mod 10,000
  902. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17">
  903. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">d</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> = <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub>
  904. </p></blockquote>
  905. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  906. In the above, the <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> terms represent carries propagated
  907. from one digit position to the next. The upper bounds on each of the
  908. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub> can be computed from the bounds given above for the
  909. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="3.71875" data-pf_rect_height="15">i</sub>:
  910. </p><blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  911. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="68">
  912. <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> ≤ 88,400
  913. [ = <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">0</sub> / 10,000 = 884,001,615 / 10,000 ]
  914. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> ≤ 112,552
  915. [ = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">1</sub>) / 10,000
  916. = 1,125,520,955 / 10,000 ]
  917. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> ≤ 31,409
  918. [ = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">2</sub>) / 10,000
  919. = 314,090,737 / 10,000 ]
  920. <br data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="0" data-pf_rect_height="17"><i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> ≤ 1,844
  921. [ = (<i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">a</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub> + <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">3</sub>) / 10,000
  922. = 18,446,744 / 10,000 ]
  923. </p></blockquote>
  924. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="17">
  925. Note something encouraging: The largest intermediate value encountered
  926. above is 1,125,520,955, which is safely less than 2 billion and therefore
  927. safe to represent as either a signed or unsigned integer on a 32-bit machine.
  928. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  929. Note also that the bound on <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="7.109375" data-pf_rect_height="17">c</i><sub data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="6.671875" data-pf_rect_height="15">4</sub> is a small but useful
  930. check on our arithmetic. The value, 1,844, is identical to the first 4 decimal
  931. digits of 2**64, which is exactly what we expect. This does not eliminate
  932. the possibility that the above might contain many small clerical errors.
  933. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  934. The code to print the 64-bit number ends up essentially identical to the
  935. code given previouly to print a 16-bit number, except for the sizes of the
  936. variables, the values of the various constants, and the code to print each
  937. digit of the result.
  938. If we have not made any errors in computing the various constants,
  939. the following C code should work:
  940. </p><pre data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="448"> void putdec( uint64_t n ) {
  941. uint32_t d4, d3, d2, d1, d0, q;
  942. d0 = n &amp; 0xFFFF;
  943. d1 = (n&gt;&gt;16) &amp; 0xFFFF;
  944. d2 = (n&gt;&gt;32) &amp; 0xFFFF;
  945. d3 = (n&gt;&gt;48) &amp; 0xFFFF;
  946. d0 = 656 * d3 + 7296 * d2 + 5536 * d1 + d0;
  947. q = d0 / 10000;
  948. d0 = d0 % 10000;
  949. d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
  950. q = d1 / 10000;
  951. d1 = d1 % 10000;
  952. d2 = q + 4749 * d3 + 42 * d2;
  953. q = d2 / 10000;
  954. d2 = d2 % 10000;
  955. d3 = q + 281 * d3;
  956. q = d3 / 10000;
  957. d3 = d3 % 10000;
  958. d4 = q;
  959. printf( "%4.4u", d4 );
  960. printf( "%4.4u", d3 );
  961. printf( "%4.4u", d2 );
  962. printf( "%4.4u", d1 );
  963. printf( "%4.4u", d0 );
  964. }
  965. </pre>
  966. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  967. In the above code, we used the standard types
  968. <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="57.1875" data-pf_rect_height="15">uint32_t</tt> and <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="57.1875" data-pf_rect_height="15">uint64_t</tt> to declare
  969. 32 and 64-bit unsigned integer variables. By convention, these should
  970. be defined in <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="71.484375" data-pf_rect_height="15">&lt;stdint.h&gt;</tt>. Note that
  971. if you are working on a 32-bit machine with no support for 64-bit arithemtic,
  972. there is no guarantee that you will be able to declare the 64-bit type
  973. and you may have to fake it, for example, by representing each 64-bit
  974. value as a pair of 32-bit components.
  975. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="34">
  976. This code uses <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="57.1875" data-pf_rect_height="15">printf()</tt> to print each 4-digit base 10,000 digit
  977. of the result. Of course, this code prints all 20 digits of <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="8" data-pf_rect_height="17">n</i>.
  978. Suppressing leading zeros takes
  979. a bit more work than it did with the 4-bit nibble code because of the
  980. complexity of suppressing leading zeros within the printed representation
  981. of the first nonzero base 10,000 digit.
  982. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="0">
  983. </p><hr data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1725.84375" data-pf_rect_height="2">
  984. <blockquote data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="254">
  985. <p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  986. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="1286.265625" data-pf_rect_height="15">
  987. I don't know where I first encountered the basic idea for the 8-bit
  988. code presented here. This tutorial began as an attempt to reinvent
  989. that code after I found myself working on a machine with an 8-bit ALU
  990. and needing to do 16-bit conversions.
  991. </small>
  992. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  993. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="754.4375" data-pf_rect_height="15">
  994. Thanks to Mark Ramsey, who, on January 22, 2007 pointed out two small typos
  995. with big consequences in the section on <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="106.28125" data-pf_rect_height="15">Living Within 8 Bits</i>.
  996. </small>
  997. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  998. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="1136.6875" data-pf_rect_height="15">
  999. Thanks to Dennis Vlasenko, who, on June 15, 2007 pointed out more typos in
  1000. the section on <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="188.828125" data-pf_rect_height="15">Doing Without Multiply and Divide</i>, and for his work
  1001. integrate the 16-bit code into the Linux distribution of <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="59.546875" data-pf_rect_height="13">vsprintf()</tt>.
  1002. </small>
  1003. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  1004. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="484.625" data-pf_rect_height="15">
  1005. Thanks to Joe Zbiciak, who, on August 23, 2007 pointed out a small error
  1006. the introduction.
  1007. </small>
  1008. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  1009. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="1216.828125" data-pf_rect_height="15">
  1010. Thanks to Derick Moore, who, on June 10, 2010, reported that he had
  1011. checked the constants and tested the code in the section
  1012. on <i data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="204" data-pf_rect_height="15">64-bit conversion on a 32-bit machine</i> and verified that it
  1013. worked in the BIOS he was maintaining.
  1014. </small>
  1015. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  1016. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="892.5" data-pf_rect_height="15">
  1017. Thanks to Michal Nazarewicz for additional testing reported on August 13, 2010
  1018. and for his work to integrate the 64-bit code into the Linux distribution
  1019. of <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="59.546875" data-pf_rect_height="13">vsprintf()</tt>.
  1020. </small>
  1021. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  1022. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="987.078125" data-pf_rect_height="15">
  1023. Thanks to George Spelvin, who on August 3, 2012, suggested that there is no
  1024. reason that all of the chunks should be the same size, leading to the
  1025. addition of the section on other radices.
  1026. </small>
  1027. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  1028. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="811.6875" data-pf_rect_height="15">
  1029. Thanks to Ralph Corderoy, who on Septeber 10, 2014, pointed out the weakness
  1030. of the repeated subtraction code when presented with the value -32768.
  1031. </small>
  1032. </p><p data-pf_style_display="block" data-pf_style_visibility="visible" data-pf_rect_width="1645.84375" data-pf_rect_height="14">
  1033. <small data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="915.734375" data-pf_rect_height="15">
  1034. Thanks to Ben Boldt, who on April 9, 2018,
  1035. pointed out an error the first take on optimising <tt data-pf_style_display="inline" data-pf_style_visibility="visible" data-pf_rect_width="29.78125" data-pf_rect_height="13">div10</tt> at the start
  1036. of the section on doing without hardware multiply and divide.
  1037. </small>
  1038. </p></blockquote>
  1039. </body></html>