2 Copyright (c) 2008 Vincent Geddes
3 Copyright (c) 2007 Luca Bruno
5 This file is part of Smalltalk YX.
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the 'Software'), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
30 "Answer the greatest common divisor"
32 num := self max: aNumber.
33 remainder := self min: aNumber.
37 remainder := num \\ remainder.
43 ^ self \\ 2 = self zero!
53 "Answer true if all 1 bits of the receiver are 1 also in the argument"
54 ^ (self bitAnd: anInteger) == anInteger!
58 "Answer true if any of the bits that are 1 in the argument are 1 in the receiver"
59 ^(self bitAnd: anInteger) ~~ anInteger!
63 "Answer true if none of the bits that are 1 in the argument are 1 in the receiver"
64 ^ (self bitAnd: anInteger) == 0!
69 ifFalse: [ self error: 'An integer is required' ].
71 self generality > aNumber generality
72 ifTrue: [ ^ self bitAnd: (self coerce: aNumber) ]
73 ifFalse: [ ^ (aNumber coerce: self) bitAnd: aNumber ]!
78 ifFalse: [ self error: 'An integer is required' ].
80 self generality > aNumber generality
81 ifTrue: [ ^ self bitOr: (self coerce: aNumber) ]
82 ifFalse: [ ^ (aNumber coerce: self) bitOr: aNumber ]!
87 ifFalse: [ self error: 'An integer is required' ].
89 self generality > aNumber generality
90 ifTrue: [ ^ self bitXor: (self coerce: aNumber) ]
91 ifFalse: [ ^ (aNumber coerce: self) bitXor: aNumber ]!
96 ifFalse: [ self error: 'An integer is required' ].
98 ^ self asLargeInteger bitShift: aNumber!
102 "Answer the bit 0 or 1 at the given index"
103 ^ (self bitShift: (index - 1) negated) bitAnd: 1!
107 "Answer an Integer whose bits are inverted"
112 ^ self bitShift: aNumber!
116 ^ self bitShift: aNumber negated!
122 "Returns a string representing the integer with the given base.
123 The base must be between 2 and 16"
127 base isInteger & (base >= 2 ) & (base <= 16)
128 ifFalse: [ self error: 'Argument must be an integer between 2 and 16' ].
132 stream := WriteStream on: (WideString new: 20).
138 cp := $0 value + (num \\ base).
140 ifTrue: [ cp := cp + $A value - $9 value - 1 ].
141 stream nextPut: (Character characterFor: cp).
142 num := num // base ].
145 ifTrue: [ stream nextPut: $- ].
147 ^ stream contents reversed!
151 printStringRadix: base
153 stream := WriteStream on: (WideString new: 20).
154 self printOn: stream base: base.
159 self storeOn: aStream base: 10!
163 self storeOn: aStream base: 10!
166 printOn: aStream base: base
167 aStream nextPutAll: (self radix: base)!
170 storeOn: aStream base: base
171 aStream nextPutAll: (self radix: base)!
200 1 to: self do: [ :ea |