| 1 | <html><head> |
| 2 | |
| 3 | <title>Octal Integer Numbers</title> |
| 4 | |
| 5 | <style type="text/css"> |
| 6 | .example { |
| 7 | color: #000000; |
| 8 | background-color: #F5F5F5; |
| 9 | padding: 8px; |
| 10 | border: #808080; |
| 11 | border-style: solid; |
| 12 | border-width: 1px; |
| 13 | width:auto; |
| 14 | } |
| 15 | .button { |
| 16 | color: #000000; |
| 17 | background-color: #F5F5F5; |
| 18 | padding-top: 1px; |
| 19 | padding-bottom: 1px; |
| 20 | padding-left: 4px; |
| 21 | padding-right: 8px; |
| 22 | border: #808080; |
| 23 | border-style: solid; |
| 24 | border-width: 1px; |
| 25 | white-space: pre; |
| 26 | } |
| 27 | .box { |
| 28 | color: #000000; |
| 29 | padding-top: 4px; |
| 30 | padding-bottom: 4px; |
| 31 | padding-left: 16px; |
| 32 | padding-right: 16px; |
| 33 | border: #808080; |
| 34 | border-style: solid; |
| 35 | border-width: 1px; |
| 36 | } |
| 37 | </style> |
| 38 | |
| 39 | </head> |
| 40 | |
| 41 | <body> |
| 42 | |
| 43 | <a href="../start.htm">Nyquist / XLISP 2.0</a> - |
| 44 | <a href="../manual/contents.htm">Contents</a> | |
| 45 | <a href="../tutorials/tutorials.htm">Tutorials</a> | |
| 46 | <a href="examples.htm">Examples</a> | |
| 47 | <a href="../reference/reference-index.htm">Reference</a> |
| 48 | |
| 49 | <hr> |
| 50 | |
| 51 | <h1>Octal Integer Numbers</h1> |
| 52 | |
| 53 | <hr> |
| 54 | |
| 55 | <p>XLISP provides the <a href="../manual/xlisp.htm#octal">#o</a> |
| 56 | <nobr>read-macro</nobr> for octal numbers:</p> |
| 57 | |
| 58 | <pre class="example"> |
| 59 | #o0 => 0 #o10 => 8 #o20 => 16 |
| 60 | #o1 => 1 #o11 => 9 #o21 => 17 |
| 61 | #o2 => 2 #o12 => 10 #o22 => 18 |
| 62 | #o3 => 3 #o13 => 11 #o23 => 19 |
| 63 | #o4 => 4 #o14 => 12 #o24 => 20 |
| 64 | #o5 => 5 #o15 => 13 #o25 => 21 |
| 65 | #o6 => 6 #o16 => 14 #o26 => 22 |
| 66 | #o7 => 7 #o17 => 15 #o27 => 23 |
| 67 | </pre> |
| 68 | |
| 69 | <p><div class="box"> |
| 70 | |
| 71 | <dl> |
| 72 | <dt>(<b>oct-string</b> <i>integer</i> [<i>all</i>])</dt> |
| 73 | <dd><i>integer</i> - an integer expression<br> |
| 74 | <i>all</i> - a boolean expression<br> |
| 75 | returns - the <i>integer</i> in octal form as string</dd> |
| 76 | </dl> |
| 77 | |
| 78 | </div></p> |
| 79 | |
| 80 | <pre class="example"> |
| 81 | (defun <font color="#0000CC">oct-string</font> (integer &optional all) |
| 82 | (if (integerp integer) |
| 83 | (let ((fmt (if all |
| 84 | (or (dolist (bits '(16 32 64 128) nil) |
| 85 | (let ((fixnum (round (expt 2.0 (1- bits))))) |
| 86 | (and (plusp (1- fixnum)) |
| 87 | (minusp fixnum) |
| 88 | (return (format nil <font color="#880000">"%.~ao"</font> |
| 89 | (1+ (/ bits 3))))))) |
| 90 | (error <font color="#880000">"integer limit not found"</font>)) |
| 91 | <font color="#880000">"%o"</font>))) |
| 92 | (progv '(<font color="#AA5500">*integer-format*</font>) (list fmt) |
| 93 | (format nil <font color="#880000">"~a"</font> integer))) |
| 94 | (error <font color="#880000">"not an integer"</font> integer))) |
| 95 | </pre> |
| 96 | |
| 97 | <p>The '<nobr>oct-string</nobr>' function converts the 'integer' argument |
| 98 | into octal form and returns is as a string. <nobr>If the</nobr> |
| 99 | optional 'all' argument is not given or |
| 100 | <a href="../reference/nil.htm">NIL</a>, leading zeros are not included in |
| 101 | the string. <nobr>If the</nobr> optional 'all' argument is |
| 102 | <nobr>non-<a href="../reference/nil.htm">NIL</a></nobr>, all digits of the |
| 103 | internal representation of the 'integer' argument, including leading zeros, |
| 104 | are contained in the string. This is useful for debugging integer overflow |
| 105 | and <nobr>bit-wise</nobr> functions.</p> |
| 106 | |
| 107 | <p><div class="box"> |
| 108 | |
| 109 | <dl> |
| 110 | <dt>(<b>oct</b> <i>integer</i> [<i>all</i>])</dt> |
| 111 | <dd><i>integer</i> - an integer expression<br> |
| 112 | <i>all</i> - a boolean expression<br> |
| 113 | prints - the <i>integer</i> in octal form<br> |
| 114 | returns - the <i>integer</i> argument</dd> |
| 115 | </dl> |
| 116 | |
| 117 | </div></p> |
| 118 | |
| 119 | <pre class="example"> |
| 120 | (defun <font color="#0000CC">oct</font> (integer &optional all) |
| 121 | (if (integerp integer) |
| 122 | (format t <font color="#880000">"#o~a~%"</font> (oct-string integer all)) |
| 123 | (format t <font color="#880000">";; not an integer~%"</font>)) |
| 124 | integer) |
| 125 | </pre> |
| 126 | |
| 127 | <p>The 'oct' function prints the 'integer' argument in octal form on |
| 128 | the screen. Together with the |
| 129 | <a href="../manual/xlisp.htm#octal">#o</a> <nobr>read-macro</nobr> |
| 130 | this can be used for interactive octal computations.</p> |
| 131 | |
| 132 | <pre class="example"> |
| 133 | > (oct 12345678) |
| 134 | #o57060516 |
| 135 | 12345678 |
| 136 | </pre> |
| 137 | |
| 138 | <p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 139 | |
| 140 | <hr> |
| 141 | |
| 142 | <a href="../start.htm">Nyquist / XLISP 2.0</a> - |
| 143 | <a href="../manual/contents.htm">Contents</a> | |
| 144 | <a href="../tutorials/tutorials.htm">Tutorials</a> | |
| 145 | <a href="examples.htm">Examples</a> | |
| 146 | <a href="../reference/reference-index.htm">Reference</a> |
| 147 | |
| 148 | </body></html> |