author | Claus Gittinger <cg@exept.de> |
Fri, 19 Apr 1996 12:47:25 +0200 | |
changeset 1221 | 46d72af387e9 |
parent 1210 | b2fbf119dbbd |
child 1295 | 83f594f05c52 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Number subclass:#Integer |
|
1210 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'DefaultDisplayRadix' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
88 | 20 |
!Integer class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
abstract superclass for all integer numbers. |
|
39 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 40 |
|
41 |
Class variables: |
|
42 |
||
43 |
DefaultDisplayRadix the radix in which integers present their |
|
44 |
displayString (which is used in inspectors) |
|
45 |
If you are to look at many hex numbers, bitmasks |
|
46 |
etc. you may set this to 2 or 16. |
|
47 |
(avoids typing printStringRadix:.. all the time |
|
48 |
- I know - I am lazy ;-). Default is 10. |
|
49 |
||
88 | 50 |
" |
51 |
! ! |
|
1 | 52 |
|
92 | 53 |
!Integer class methodsFor:'instance creation'! |
54 |
||
55 |
new:numberOfBytes neg:negative |
|
56 |
"for ST-80 compatibility: |
|
57 |
Return an empty Integer (uninitialized value) with space for |
|
58 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 59 |
negative specifies if the result should be a negative number. |
60 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 61 |
|
132 | 62 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
159 | 63 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
701 | 64 |
! |
65 |
||
66 |
readFrom:aStringOrStream |
|
67 |
"return the next unsigned Integer from the (character-)stream aStream |
|
68 |
as decimal number. |
|
69 |
||
70 |
NOTICE: |
|
71 |
This behaves different from the default readFrom:, in returning |
|
72 |
0 (instead of raising an error) in case no number can be read. |
|
73 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
74 |
- depending on the upcoming ANSI standard, this may change." |
|
75 |
||
76 |
^ self readFrom:aStringOrStream onError:0 |
|
77 |
||
78 |
" |
|
79 |
Integer readFrom:(ReadStream on:'foobar') |
|
80 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
|
81 |
" |
|
82 |
! |
|
83 |
||
84 |
readFrom:aStringOrStream onError:exceptionBlock |
|
85 |
"return the next Integer from the (character-)stream aStream, |
|
86 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
87 |
Also, all initial whitespace is skipped. |
|
88 |
If the string does not represent a valid integer number, |
|
89 |
return the value of exceptionBlock." |
|
90 |
||
91 |
|str nextChar value negative| |
|
92 |
||
93 |
str := aStringOrStream readStream. |
|
94 |
||
95 |
nextChar := str skipSeparators. |
|
96 |
(nextChar == $-) ifTrue:[ |
|
97 |
negative := true. |
|
98 |
nextChar := str nextPeek |
|
99 |
] ifFalse:[ |
|
100 |
negative := false |
|
101 |
]. |
|
102 |
(nextChar isNil or:[nextChar isDigit not]) ifTrue:[ |
|
103 |
" |
|
104 |
the string does not represent an integer |
|
105 |
" |
|
106 |
^ exceptionBlock value |
|
107 |
]. |
|
108 |
value := self readFrom:str radix:10 onError:[^ exceptionBlock value]. |
|
109 |
nextChar := str peek. |
|
110 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
|
111 |
"-xxr<number> is invalid; should be xxr-<val>" |
|
112 |
||
113 |
negative ifTrue:[ |
|
114 |
'invalid (negative) radix ignored' errorPrintNL. |
|
115 |
negative := false |
|
116 |
]. |
|
117 |
str next. |
|
118 |
value := self readFrom:str radix:value onError:[^ exceptionBlock value] |
|
119 |
]. |
|
120 |
negative ifTrue:[ |
|
121 |
^ value negated |
|
122 |
]. |
|
123 |
^ value |
|
124 |
||
125 |
" |
|
126 |
Integer readFrom:'12345' |
|
127 |
Integer readFrom:'-12345' |
|
128 |
Integer readFrom:'+12345' |
|
129 |
Integer readFrom:'16rFFFF' |
|
130 |
Integer readFrom:'12345.1234' |
|
131 |
Integer readFrom:'foo' |
|
132 |
Integer readFrom:'foo' onError:[0] |
|
133 |
" |
|
134 |
||
135 |
"Created: 16.11.1995 / 22:48:59 / cg" |
|
136 |
! |
|
137 |
||
138 |
readFrom:aStringOrStream radix:radix |
|
139 |
"return the next unsigned Integer from the (character-)stream aStream |
|
140 |
in radix; (assumes that the initial XXr has already been read). |
|
141 |
No whitespace-skipping is done. |
|
142 |
Returns 0 if no number available. |
|
143 |
||
144 |
NOTICE: |
|
145 |
This behaves different from the default readFrom:, in returning |
|
146 |
0 (instead of raising an error) in case no number can be read. |
|
147 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
148 |
- depending on the upcoming ANSI standard, this may change." |
|
149 |
||
150 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
151 |
! |
|
152 |
||
153 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
154 |
"return the next unsigned Integer from the (character-)stream aStream |
|
155 |
in radix; (assumes that the initial XXr has already been read). |
|
156 |
No whitespace-skipping is done. |
|
157 |
Returns the value of exceptionBlock, if no number is available." |
|
158 |
||
159 |
|str nextChar value| |
|
160 |
||
161 |
str := aStringOrStream readStream. |
|
162 |
||
163 |
nextChar := str peek. |
|
164 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifFalse:[ |
|
165 |
^ exceptionBlock value |
|
166 |
]. |
|
167 |
||
168 |
value := nextChar digitValue. |
|
169 |
nextChar := str nextPeek. |
|
170 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
|
171 |
value := value * radix + nextChar digitValue. |
|
172 |
nextChar := str nextPeek |
|
173 |
]. |
|
174 |
^ value |
|
175 |
||
176 |
" |
|
177 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
178 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
179 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
180 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
181 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
182 |
" |
|
183 |
! ! |
|
184 |
||
185 |
!Integer class methodsFor:'constants'! |
|
186 |
||
187 |
unity |
|
188 |
"return the neutral element for multiplication" |
|
189 |
||
190 |
^ 1 |
|
191 |
! |
|
192 |
||
193 |
zero |
|
194 |
"return the neutral element for addition" |
|
195 |
||
196 |
^ 0 |
|
92 | 197 |
! ! |
198 |
||
49 | 199 |
!Integer class methodsFor:'misc'! |
200 |
||
201 |
displayRadix:aNumber |
|
54 | 202 |
"being tired of always sending #printStringRadix: in the inspectors, |
49 | 203 |
this allows you to change the default print radix for the displayString |
204 |
method." |
|
205 |
||
206 |
DefaultDisplayRadix := aNumber |
|
132 | 207 |
|
208 |
" |
|
209 |
Integer displayRadix:16. 123456 inspect |
|
210 |
Integer displayRadix:10. 123456 inspect |
|
211 |
" |
|
49 | 212 |
! ! |
213 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
214 |
!Integer methodsFor:'VisualAge compatibility'! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
215 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
216 |
<< aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
217 |
"left shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
218 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
219 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
220 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
221 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
222 |
1 << 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
223 |
64 << -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
224 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
225 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
226 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
227 |
>> aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
228 |
"right shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
229 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
230 |
^ self bitShift:(aNumber negated) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
231 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
232 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
233 |
1 >> -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
234 |
64 >> 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
235 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
236 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
237 |
|
1210 | 238 |
clearBit:index |
239 |
"return a new number where the specified bit is off. |
|
240 |
Bits are counted from 1 starting with the least significant." |
|
241 |
||
242 |
^ self bitAnd:(1 bitShift:index-1) bitInvert |
|
243 |
||
244 |
" |
|
245 |
5 clearBit:3 => 1 |
|
246 |
16rFFFF clearBit:16 => 32767 (16r7FFF) |
|
247 |
16rFFFF clearBit:1 => 65534 (16rFFFE) |
|
248 |
" |
|
249 |
! |
|
250 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
251 |
isBitSet:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
252 |
"return true if the index' bit is set; false otherwise. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
253 |
Bits are counted from 1 starting with the least significant." |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
254 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
255 |
^ (self bitAt:index) ~~ 0 |
1 | 256 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
257 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
258 |
5 isBitSet:3 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
259 |
2r0101 isBitSet:2 => false |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
260 |
2r0101 isBitSet:1 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
261 |
2r0101 isBitSet:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
262 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
263 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
264 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
265 |
setBit:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
266 |
"return a new number where the specified bit is on. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
267 |
Bits are counted from 1 starting with the least significant." |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
268 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
269 |
^ self bitOr:(1 bitShift:index-1) |
1 | 270 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
271 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
272 |
0 setBit:3 => 4 (2r100) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
273 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
274 |
((0 setBit:99) setBit:100) printStringRadix:2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
275 |
" |
1 | 276 |
! ! |
277 |
||
69 | 278 |
!Integer methodsFor:'bit operators'! |
279 |
||
280 |
bitAnd:anInteger |
|
357 | 281 |
"return the bitwise-and of the receiver and the argument, anInteger. |
282 |
This is a general and slow implementation, walking over the bytes of |
|
283 |
the receiver and the argument." |
|
69 | 284 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
285 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
286 |
result byte| |
69 | 287 |
|
92 | 288 |
n := (anInteger digitLength) min:(self digitLength). |
289 |
result := self class basicNew numberOfDigits:n. |
|
69 | 290 |
|
291 |
1 to:n do:[:index | |
|
159 | 292 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
293 |
result digitAt:index put:byte. |
|
69 | 294 |
]. |
357 | 295 |
(byte == 0 or:[n < 5]) ifTrue:[ |
159 | 296 |
^ result normalize |
92 | 297 |
]. |
298 |
^ result |
|
357 | 299 |
|
300 |
" |
|
301 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
|
302 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
303 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
304 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
305 |
" |
|
69 | 306 |
! |
307 |
||
85 | 308 |
bitAt:index |
92 | 309 |
"return the value of the index's bit (index starts at 1). |
310 |
Notice: the result of bitShift: on negative receivers is not |
|
159 | 311 |
defined in the language standard (since the implementation |
312 |
is free to choose any internal representation for integers)" |
|
85 | 313 |
|
314 |
|i "{Class: SmallInteger}"| |
|
315 |
||
316 |
i := index - 1. |
|
317 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
|
92 | 318 |
|
319 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
320 |
1 bitAt:1 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
321 |
1 bitAt:2 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
322 |
1 bitAt:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
323 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
324 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
325 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
326 |
(1 bitShift:1000) bitAt:1000 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
327 |
(1 bitShift:1000) bitAt:1001 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
328 |
(1 bitShift:1000) bitAt:1002 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
329 |
|
92 | 330 |
(1 bitShift:30) bitAt:30 |
331 |
(1 bitShift:30) bitAt:31 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
332 |
(1 bitShift:30) bitAt:32 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
333 |
(1 bitShift:31) bitAt:31 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
334 |
(1 bitShift:31) bitAt:32 |
92 | 335 |
(1 bitShift:31) bitAt:33 |
336 |
(1 bitShift:32) bitAt:32 |
|
337 |
(1 bitShift:32) bitAt:33 |
|
338 |
(1 bitShift:32) bitAt:34 |
|
339 |
(1 bitShift:64) bitAt:64 |
|
340 |
(1 bitShift:64) bitAt:65 |
|
341 |
(1 bitShift:64) bitAt:66 |
|
342 |
" |
|
85 | 343 |
! |
344 |
||
701 | 345 |
bitOr:anInteger |
346 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
347 |
This is a general and slow implementation, walking over the bytes of |
|
348 |
the receiver and the argument." |
|
349 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
350 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
351 |
result byte| |
701 | 352 |
|
353 |
n := (anInteger digitLength) max:(self digitLength). |
|
354 |
result := self class basicNew numberOfDigits:n. |
|
355 |
||
356 |
1 to:n do:[:index | |
|
357 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
|
358 |
result digitAt:index put:byte. |
|
359 |
]. |
|
360 |
"/ no need to normalize - if the operands were correct |
|
361 |
"/ byte == 0 ifTrue:[ |
|
362 |
"/ ^ result normalize |
|
363 |
"/ ]. |
|
364 |
^ result |
|
365 |
! |
|
366 |
||
85 | 367 |
bitShift:shiftCount |
368 |
"return the value of the receiver shifted by shiftCount bits; |
|
369 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 370 |
|
371 |
Notice: the result of bitShift: on negative receivers is not |
|
159 | 372 |
defined in the language standard (since the implementation |
373 |
is free to choose any internal representation for integers)" |
|
85 | 374 |
|
92 | 375 |
|result |
376 |
prev "{ Class: SmallInteger }" |
|
377 |
next "{ Class: SmallInteger }" |
|
378 |
byte "{ Class: SmallInteger }" |
|
359 | 379 |
byte2 "{ Class: SmallInteger }" |
92 | 380 |
bitShift "{ Class: SmallInteger }" |
381 |
revShift "{ Class: SmallInteger }" |
|
382 |
digitShift "{ Class: SmallInteger }" |
|
383 |
n "{ Class: SmallInteger }" |
|
384 |
nn "{ Class: SmallInteger }"| |
|
85 | 385 |
|
386 |
shiftCount > 0 ifTrue:[ |
|
159 | 387 |
"left shift" |
85 | 388 |
|
159 | 389 |
digitShift := shiftCount // 8. |
390 |
bitShift := shiftCount \\ 8. |
|
391 |
n := self digitLength. |
|
92 | 392 |
|
159 | 393 |
" |
394 |
modulu 8 shifts can be done faster ... |
|
395 |
" |
|
396 |
bitShift == 0 ifTrue:[ |
|
397 |
result := self class basicNew numberOfDigits:n + digitShift. |
|
398 |
result sign:self sign. |
|
399 |
result digits replaceFrom:(digitShift + 1) with:self digits. |
|
400 |
" |
|
401 |
no normalize needed, since receiver was already normalized |
|
402 |
" |
|
403 |
^ result |
|
404 |
]. |
|
92 | 405 |
|
159 | 406 |
" |
407 |
less-than-8 shifts can be done faster ... |
|
408 |
" |
|
409 |
digitShift == 0 ifTrue:[ |
|
410 |
n := n + 1. |
|
411 |
result := self class basicNew numberOfDigits:n. |
|
412 |
result sign:self sign. |
|
413 |
prev := 0. |
|
414 |
1 to:n-1 do:[:index | |
|
415 |
byte := self digitAt:index. |
|
416 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
417 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
418 |
prev := byte bitShift:-8. |
|
419 |
]. |
|
420 |
result digitAt:n put:prev. |
|
421 |
" |
|
422 |
might have stored a 0-byte ... |
|
423 |
" |
|
424 |
prev == 0 ifTrue:[ |
|
425 |
^ result normalize |
|
426 |
]. |
|
427 |
^ result. |
|
428 |
]. |
|
92 | 429 |
|
159 | 430 |
" |
431 |
slow case ... |
|
432 |
" |
|
433 |
n := n + digitShift + 1. |
|
434 |
result := self class basicNew numberOfDigits:n. |
|
435 |
result sign:self sign. |
|
359 | 436 |
byte := self digitAt:1. |
437 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
159 | 438 |
result digitAt:(digitShift + 1) put:byte. |
439 |
revShift := -8 + bitShift. |
|
440 |
2 to:(self digitLength) do:[:index | |
|
359 | 441 |
byte := self digitAt:index. |
442 |
byte2 := self digitAt:index-1. |
|
443 |
byte := byte bitShift:bitShift. |
|
444 |
byte2 := byte2 bitShift:revShift. |
|
445 |
byte := byte bitOr:byte2. |
|
159 | 446 |
byte := byte bitAnd:16rFF. |
359 | 447 |
result digitAt:(index + digitShift) put:byte. |
159 | 448 |
]. |
359 | 449 |
byte2 := self digitAt:self digitLength. |
450 |
byte2 := byte2 bitShift:revShift. |
|
451 |
byte2 := byte2 bitAnd:16rFF. |
|
452 |
result digitAt:(self digitLength + digitShift + 1) put:byte2. |
|
159 | 453 |
" |
454 |
might have stored a 0-byte ... |
|
455 |
" |
|
359 | 456 |
byte2 == 0 ifTrue:[ |
159 | 457 |
^ result normalize |
458 |
]. |
|
459 |
^ result |
|
92 | 460 |
]. |
461 |
||
462 |
shiftCount < 0 ifTrue:[ |
|
159 | 463 |
"right shift" |
85 | 464 |
|
159 | 465 |
digitShift := shiftCount negated // 8. |
466 |
bitShift := shiftCount negated \\ 8. |
|
467 |
n := self digitLength. |
|
92 | 468 |
|
159 | 469 |
digitShift >= n ifTrue:[ |
470 |
^ 0 |
|
471 |
]. |
|
92 | 472 |
|
159 | 473 |
" |
474 |
modulu 8 shifts can be done faster ... |
|
475 |
" |
|
476 |
bitShift == 0 ifTrue:[ |
|
477 |
n := n-digitShift. |
|
478 |
result := self class basicNew numberOfDigits:n. |
|
479 |
result sign:self sign. |
|
480 |
result digits replaceFrom:1 with:self digits startingAt:(digitShift + 1) . |
|
481 |
n <= 4 ifTrue:[ |
|
482 |
^ result normalize |
|
483 |
]. |
|
484 |
^ result |
|
485 |
]. |
|
85 | 486 |
|
159 | 487 |
" |
488 |
less-than-8 shifts can be done faster ... |
|
489 |
" |
|
490 |
digitShift == 0 ifTrue:[ |
|
491 |
result := self class basicNew numberOfDigits:n. |
|
492 |
result sign:self sign. |
|
493 |
prev := 0. |
|
494 |
bitShift := bitShift negated. |
|
495 |
revShift := 8 + bitShift. |
|
496 |
n to:1 by:-1 do:[:index | |
|
497 |
byte := self digitAt:index. |
|
498 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
499 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
500 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
501 |
prev := next. |
|
502 |
]. |
|
503 |
(n <= 5) ifTrue:[ |
|
504 |
^ result normalize |
|
505 |
]. |
|
506 |
^ result |
|
507 |
]. |
|
92 | 508 |
|
159 | 509 |
" |
510 |
slow case ... |
|
511 |
" |
|
512 |
nn := n-digitShift. |
|
513 |
result := self class basicNew numberOfDigits:nn. |
|
514 |
result sign:self sign. |
|
85 | 515 |
|
159 | 516 |
prev := 0. |
517 |
bitShift := bitShift negated. |
|
518 |
revShift := 8 + bitShift. |
|
519 |
n to:(1 + digitShift) by:-1 do:[:index | |
|
520 |
byte := self digitAt:index. |
|
521 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
522 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
523 |
result digitAt:(index - digitShift) put:byte. |
|
524 |
prev := next. |
|
525 |
]. |
|
526 |
"the last stored byte ..." |
|
527 |
^ result normalize |
|
85 | 528 |
]. |
92 | 529 |
|
530 |
^ self "no shift" |
|
701 | 531 |
! |
1 | 532 |
|
701 | 533 |
bitXor:anInteger |
534 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
535 |
This is a general and slow implementation, walking over the bytes of |
|
536 |
the receiver and the argument." |
|
537 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
538 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
539 |
result byte| |
701 | 540 |
|
541 |
n := (anInteger digitLength) max:(self digitLength). |
|
542 |
result := self class basicNew numberOfDigits:n. |
|
1 | 543 |
|
701 | 544 |
1 to:n do:[:index | |
545 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
|
546 |
result digitAt:index put:byte. |
|
547 |
]. |
|
548 |
byte == 0 ifTrue:[ |
|
549 |
^ result normalize |
|
550 |
]. |
|
551 |
^ result |
|
1 | 552 |
|
701 | 553 |
" |
554 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
|
555 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
|
556 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
557 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
|
558 |
" |
|
1 | 559 |
! |
560 |
||
701 | 561 |
highBit |
562 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
563 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
564 |
||
565 |
|byteNr highByte| |
|
566 |
||
567 |
byteNr := self digitLength. |
|
568 |
highByte := self digitAt:byteNr. |
|
569 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 570 |
|
701 | 571 |
" |
572 |
1 highBit |
|
573 |
(1 bitShift:1) highBit |
|
574 |
(1 bitShift:30) highBit |
|
575 |
(1 bitShift:31) highBit |
|
576 |
(1 bitShift:32) highBit |
|
577 |
(1 bitShift:33) highBit |
|
578 |
(1 bitShift:64) highBit |
|
579 |
(1 bitShift:1000) highBit |
|
580 |
((1 bitShift:64)-1) highBit |
|
581 |
" |
|
582 |
! ! |
|
583 |
||
584 |
!Integer methodsFor:'coercing and converting'! |
|
585 |
||
586 |
asFraction |
|
587 |
"return a Fraction with same value as receiver" |
|
588 |
||
589 |
^ Fraction numerator:self denominator:1 |
|
1 | 590 |
! |
591 |
||
701 | 592 |
asInteger |
1 | 593 |
"return the receiver truncated towards zero - |
594 |
for integers this is self" |
|
595 |
||
596 |
^ self |
|
597 |
! ! |
|
598 |
||
701 | 599 |
!Integer methodsFor:'double dispatching'! |
600 |
||
601 |
differenceFromFraction:aFraction |
|
602 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 603 |
|
701 | 604 |
|d| |
1 | 605 |
|
701 | 606 |
d := aFraction denominator. |
607 |
^ (Fraction numerator:(aFraction numerator - (self * d)) |
|
608 |
denominator:d) reduced |
|
1 | 609 |
! |
610 |
||
701 | 611 |
productFromFraction:aFraction |
612 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 613 |
|
701 | 614 |
^ (Fraction numerator:(self * aFraction numerator) |
615 |
denominator:aFraction denominator) reduced |
|
1 | 616 |
! |
617 |
||
701 | 618 |
quotientFromFraction:aFraction |
619 |
"sent when a fraction does not know how to divide the receiver, an integer" |
|
620 |
||
621 |
^ (Fraction numerator:aFraction numerator |
|
622 |
denominator:(self * aFraction denominator)) reduced |
|
623 |
! |
|
1 | 624 |
|
701 | 625 |
sumFromFraction:aFraction |
626 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
627 |
||
628 |
|d| |
|
629 |
||
630 |
d := aFraction denominator. |
|
631 |
^ (Fraction numerator:(aFraction numerator + (self * d)) |
|
632 |
denominator:d) reduced |
|
1 | 633 |
! ! |
634 |
||
635 |
!Integer methodsFor:'misc math'! |
|
636 |
||
701 | 637 |
acker:n |
638 |
"return the value of acker(self, n)" |
|
639 |
||
640 |
(self == 0) ifTrue:[^ n + 1]. |
|
641 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
642 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
643 |
||
644 |
" |
|
645 |
3 acker:2 |
|
646 |
" |
|
647 |
! |
|
648 |
||
1 | 649 |
factorial |
650 |
"return 1*2*3...*self" |
|
651 |
||
356 | 652 |
(self >= 2) ifTrue:[ |
159 | 653 |
^ self * (self - 1) factorial |
1 | 654 |
]. |
356 | 655 |
^ 1 |
212 | 656 |
|
657 |
" |
|
658 |
10 factorial |
|
659 |
1000 factorial |
|
660 |
" |
|
1 | 661 |
! |
662 |
||
701 | 663 |
fastFib |
664 |
"this method just to show how a changed algorithm can |
|
665 |
change things much more drastic than tuning ... |
|
666 |
(compare 30 fib with 30 fastFib / dont even try 60 fib)" |
|
667 |
||
668 |
^ self fibUsingCache:(OrderedCollection new) |
|
669 |
||
670 |
" |
|
671 |
Transcript showCr:(Time millisecondsToRun:[30 fib]) |
|
672 |
Transcript showCr:(Time millisecondsToRun:[30 fastFib]) |
|
673 |
Transcript showCr:(Time millisecondsToRun:[60 fastFib]) |
|
674 |
Transcript showCr:(Time millisecondsToRun:[200 fastFib]) |
|
675 |
" |
|
676 |
! |
|
677 |
||
678 |
fib |
|
679 |
"dont use this method if you need fibionacci numbers - |
|
680 |
this method is for benchmarking purposes only. |
|
681 |
(use fastFib instead and dont ever try 60 fib ...)" |
|
682 |
||
683 |
(self > 1) ifTrue:[ |
|
684 |
^ (self - 1) fib + (self - 2) fib |
|
685 |
]. |
|
686 |
^ 1 |
|
687 |
||
688 |
" |
|
689 |
10 fib |
|
690 |
20 fib |
|
691 |
Transcript showCr:(Time millisecondsToRun:[30 fib]) |
|
692 |
" |
|
693 |
||
694 |
" |
|
695 |
ds3100 486/50 Indy(no cache) |
|
696 |
20804 4800 2145 |
|
697 |
" |
|
698 |
! |
|
699 |
||
700 |
fibUsingCache:fibCache |
|
701 |
"actual fastfib worker; keep track of already computed fib |
|
702 |
numbers in fibCache, which is passed around. Take value from |
|
703 |
the cache if already present. Doing so, the double recursive |
|
704 |
fib actually becomes a single recursive algorithm." |
|
705 |
||
706 |
|fib| |
|
707 |
||
708 |
self <= 1 ifTrue:[^ 1]. |
|
709 |
||
710 |
fibCache size >= self ifTrue:[ |
|
711 |
^ fibCache at:self |
|
712 |
]. |
|
713 |
fib := ((self - 2) fibUsingCache:fibCache) + ((self - 1) fibUsingCache:fibCache). |
|
714 |
||
715 |
fibCache grow:self. |
|
716 |
fibCache at:self put:fib. |
|
717 |
^ fib |
|
718 |
! |
|
719 |
||
1 | 720 |
gcd:anInteger |
721 |
"return the greatest common divisor (Euclid's algorithm)" |
|
722 |
||
723 |
|ttt selfInteger temp| |
|
724 |
||
725 |
ttt := anInteger. |
|
726 |
selfInteger := self. |
|
727 |
[ttt ~~ 0] whileTrue:[ |
|
159 | 728 |
temp := selfInteger \\ ttt. |
729 |
selfInteger := ttt. |
|
730 |
ttt := temp |
|
1 | 731 |
]. |
732 |
^ selfInteger |
|
52 | 733 |
|
92 | 734 |
" |
735 |
65 gcd:15 |
|
736 |
3 gcd:15 |
|
737 |
132 gcd:55 |
|
738 |
" |
|
1 | 739 |
! |
740 |
||
741 |
lcm:anInteger |
|
52 | 742 |
"return the least common multiple (using gcd:)" |
743 |
||
744 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
745 |
||
92 | 746 |
" |
747 |
65 lcm:15 |
|
748 |
3 lcm:15 |
|
749 |
" |
|
1 | 750 |
! ! |
751 |
||
752 |
!Integer methodsFor:'printing & storing'! |
|
753 |
||
49 | 754 |
displayString |
755 |
"return a string for displaying in a view (as in inspector). |
|
756 |
The output radix is usually 10, but can be changed by setting |
|
757 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
758 |
||
54 | 759 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
159 | 760 |
^ self printString |
54 | 761 |
]. |
49 | 762 |
^ self radixPrintStringRadix:DefaultDisplayRadix |
763 |
||
92 | 764 |
" |
765 |
Integer displayRadix:16. 12345 |
|
766 |
Integer displayRadix:2. 12345 |
|
767 |
Integer displayRadix:10. 12345 |
|
768 |
" |
|
49 | 769 |
! |
770 |
||
701 | 771 |
errorPrintHex |
772 |
"print the receiver as a hex number on the standard error stream" |
|
773 |
||
774 |
(self printStringRadix:16) errorPrint |
|
775 |
! |
|
1 | 776 |
|
701 | 777 |
hexPrintString |
778 |
"return a hex string representation of the receiver" |
|
779 |
||
780 |
^ self printStringRadix:16 |
|
781 |
! |
|
782 |
||
783 |
printHex |
|
784 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 785 |
|
701 | 786 |
(self printStringRadix:16) print |
787 |
! |
|
788 |
||
789 |
printOn:aStream |
|
790 |
"append a printed description of the receiver to aStream" |
|
791 |
||
792 |
aStream nextPutAll:(self printStringRadix:10) |
|
793 |
! |
|
794 |
||
795 |
printOn:aStream radix:base |
|
796 |
"append a printed description of the receiver to aStream" |
|
797 |
||
798 |
aStream nextPutAll:(self printStringRadix:base) |
|
799 |
! |
|
800 |
||
801 |
printString |
|
802 |
"return a string representation of the receiver" |
|
803 |
||
804 |
^ self printStringRadix:10 |
|
1 | 805 |
! |
806 |
||
807 |
printStringRadix:aRadix |
|
808 |
"return a string representation of the receiver in the specified |
|
809 |
radix (without the initial XXr)" |
|
810 |
||
92 | 811 |
|num s "rx leftPart"| |
1 | 812 |
|
54 | 813 |
(aRadix between:2 and:36) ifFalse:[ |
159 | 814 |
self error:'invalid radix'. |
815 |
^ self printStringRadix:10 |
|
54 | 816 |
]. |
817 |
||
1 | 818 |
(self = 0) ifTrue:[^ '0']. |
819 |
(self < 0) ifTrue:[ |
|
159 | 820 |
^ '-' , (self negated printStringRadix:aRadix) |
1 | 821 |
]. |
92 | 822 |
|
823 |
" |
|
824 |
claus: changed it from a recursive algorithm; |
|
825 |
(it used to trigger stack-overflow exceptions when printing |
|
826 |
3000 factorial ...) |
|
827 |
" |
|
828 |
"/ leftPart := self // aRadix. |
|
829 |
"/ (leftPart ~= 0) ifTrue:[ |
|
830 |
"/ ^ (leftPart printStringRadix:aRadix) copyWith:(Character digitValue:(self \\ aRadix)) |
|
831 |
"/ ]. |
|
832 |
"/ ^ (Character digitValue:self) asString |
|
833 |
||
834 |
num := self. |
|
835 |
s := (Character digitValue:(num \\ aRadix)) asString. |
|
836 |
num := num // aRadix. |
|
837 |
[num ~= 0] whileTrue:[ |
|
159 | 838 |
s := (Character digitValue:(num \\ aRadix)) asString , s. |
839 |
num := num // aRadix. |
|
1 | 840 |
]. |
92 | 841 |
^ s |
1 | 842 |
! |
843 |
||
844 |
printStringRadix:aRadix size:sz fill:fillCharacter |
|
845 |
"return a string representation of the receiver in the specified |
|
846 |
radix. The string is padded on the left with fillCharacter to make |
|
847 |
its size as specified in sz." |
|
848 |
||
92 | 849 |
|s| |
1 | 850 |
|
851 |
s := self printStringRadix:aRadix. |
|
852 |
s size < sz ifTrue:[ |
|
159 | 853 |
s := ((String new:(sz - s size)) atAllPut:fillCharacter) , s |
1 | 854 |
]. |
855 |
^ s |
|
856 |
||
92 | 857 |
" |
858 |
1024 printStringRadix:16 size:4 fill:$0 |
|
859 |
1024 printStringRadix:2 size:16 fill:$. |
|
860 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
861 |
" |
|
216 | 862 |
! |
863 |
||
701 | 864 |
radixPrintStringRadix:aRadix |
865 |
"return a string representation of the receiver in the specified |
|
866 |
radix; prepend XXr to the string" |
|
867 |
||
868 |
^ (aRadix printString) , 'r', (self printStringRadix:aRadix) |
|
216 | 869 |
|
701 | 870 |
" |
871 |
31 radixPrintStringRadix:2 |
|
872 |
31 radixPrintStringRadix:3 |
|
873 |
31 radixPrintStringRadix:36 |
|
874 |
" |
|
1 | 875 |
! ! |
876 |
||
701 | 877 |
!Integer methodsFor:'queries'! |
202 | 878 |
|
701 | 879 |
digitAt:n |
880 |
"return the n-th byte of the binary representation. |
|
881 |
This is a very stupid implementation, and should be redefined in |
|
882 |
concrete subclasses." |
|
202 | 883 |
|
701 | 884 |
|num count| |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
885 |
|
701 | 886 |
num := self. |
887 |
count := n. |
|
888 |
[count > 1] whileTrue:[ |
|
889 |
num := num // 256. |
|
890 |
count := count - 1 |
|
202 | 891 |
]. |
701 | 892 |
^ num \\ 256 |
159 | 893 |
|
894 |
" |
|
701 | 895 |
16r44332211 digitAt:1 |
896 |
16r44332211 digitAt:2 |
|
897 |
16r44332211 digitAt:3 |
|
898 |
16r44332211 digitAt:4 |
|
899 |
16r44332211 digitAt:5 |
|
900 |
16r00332211 digitAt:4 |
|
901 |
16r00332211 digitAt:5 |
|
159 | 902 |
" |
1 | 903 |
! |
904 |
||
701 | 905 |
digitLength |
906 |
"return the number of bytes needed for the binary representation |
|
907 |
of the receiver" |
|
908 |
||
909 |
^ (self log:256) ceiling asInteger |
|
910 |
! |
|
911 |
||
912 |
isInteger |
|
913 |
"return true, if the receiver is some kind of integer number" |
|
914 |
||
915 |
^ true |
|
916 |
! ! |
|
202 | 917 |
|
701 | 918 |
!Integer methodsFor:'truncation & rounding'! |
919 |
||
920 |
ceiling |
|
921 |
"I am my ceiling" |
|
345 | 922 |
|
701 | 923 |
^ self |
924 |
! |
|
345 | 925 |
|
701 | 926 |
compressed |
927 |
"this is ST-80's name for ST/X's normalize. |
|
928 |
ST/X may be changed for full compatibility." |
|
929 |
||
930 |
^ self normalize |
|
202 | 931 |
! |
932 |
||
701 | 933 |
floor |
934 |
"I am my floor" |
|
935 |
||
936 |
^ self |
|
937 |
! |
|
202 | 938 |
|
701 | 939 |
normalize |
940 |
"this is ST/X's name for ST-80's compressed. |
|
941 |
ST/X may be changed for full compatibility." |
|
1 | 942 |
|
701 | 943 |
^ self |
944 |
! |
|
1 | 945 |
|
701 | 946 |
rounded |
947 |
"return the receiver rounded toward the next Integer - |
|
948 |
for integers this is self" |
|
949 |
||
950 |
^ self |
|
951 |
! |
|
69 | 952 |
|
701 | 953 |
truncated |
954 |
"return the receiver truncated towards zero - |
|
955 |
for integers this is self" |
|
159 | 956 |
|
701 | 957 |
^ self |
958 |
! ! |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
959 |
|
701 | 960 |
!Integer class methodsFor:'documentation'! |
961 |
||
962 |
version |
|
1210 | 963 |
^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.37 1996-04-18 09:54:21 cg Exp $' |
1 | 964 |
! ! |