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.
46 "Answer true if all 1 bits of the receiver are 1 also in the argument"
47 ^ (self bitAnd: anInteger) == anInteger!
51 "Answer true if any of the bits that are 1 in the argument are 1 in the receiver"
52 ^(self bitAnd: anInteger) ~~ anInteger!
56 "Answer true if none of the bits that are 1 in the argument are 1 in the receiver"
57 ^ (self bitAnd: anInteger) == 0!
62 ifFalse: [ self error: 'An integer is required' ].
64 self generality > aNumber generality
65 ifTrue: [ ^ self bitAnd: (self coerce: aNumber) ]
66 ifFalse: [ ^ (aNumber coerce: self) bitAnd: aNumber ]!
71 ifFalse: [ self error: 'An integer is required' ].
73 self generality > aNumber generality
74 ifTrue: [ ^ self bitOr: (self coerce: aNumber) ]
75 ifFalse: [ ^ (aNumber coerce: self) bitOr: aNumber ]!
80 ifFalse: [ self error: 'An integer is required' ].
82 self generality > aNumber generality
83 ifTrue: [ ^ self bitXor: (self coerce: aNumber) ]
84 ifFalse: [ ^ (aNumber coerce: self) bitXor: aNumber ]!
88 aNumber isSmallInteger
89 ifFalse: [ self error: 'A small integer is required' ].
91 ^ self asLargeInteger bitShift: aNumber!
95 "Answer the bit 0 or 1 at the given index"
96 ^ (self bitShift: (index - 1) negated) bitAnd: 1!
100 "Answer an Integer whose bits are inverted"
105 ^ self bitShift: aNumber!
109 ^ self bitShift: aNumber negated!
115 "Returns a string representing the integer with the given base.
116 The base must be between 2 and 16"
120 base isInteger & (base >= 2 ) & (base <= 16)
121 ifFalse: [ self error: 'Argument must be an integer between 2 and 16' ].
125 stream := StringStream new.
131 cp := $0 value + (num \\ base).
133 ifTrue: [ cp := cp + $A value - $9 value - 1 ].
134 stream nextPut: (Character new: cp).
135 num := num // base ].
138 ifTrue: [ stream nextPut: $- ].
140 ^ stream contents reversed!
144 printStringRadix: base
146 stream := StringStream new.
147 self printOn: stream base: base.
152 self storeOn: aStream base: 10!
156 self storeOn: aStream base: 10!
159 printOn: aStream base: base
160 aStream nextPutAll: (self radix: base)!
163 storeOn: aStream base: base
164 aStream nextPutAll: (self radix: base)!
192 1 to: self do: [ :ea |