Squeak Class Documentation category index | class index  
 
Integer
  category: Kernel-Numbers
  superclass: Number
  subclasses: SmallInteger LargePositiveInteger

I am a common abstract superclass for all Integer implementations. My implementation subclasses are SmallInteger, LargePositiveInteger, and LargeNegativeInteger.

Integer division consists of:
/ exact division, answers a fraction if result is not a whole integer
// answers an Integer, rounded towards negative infinity
\\ is modulo rounded towards negative infinity
quo: truncated division, rounded towards zero

instance methods
  arithmetic
  *
+
-
/
//
\\\
alignedTo:
quo:

  benchmarks
  benchFib
benchmark
tinyBenchmarks

  bit manipulation
  <<
>>
allMask:
anyBitOfMagnitudeFrom:to:
anyMask:
bitAnd:
bitClear:
bitInvert
bitInvert32
bitOr:
bitShift:
bitShiftMagnitude:
bitXor:
highBit
highBitOfMagnitude
lowBit
noMask:

  comparing
  <
=
>
hash

  converting
  adaptToFraction:andSend:
asCharacter
asColorOfDepth:
asFloat
asFraction
asHexDigit
asInteger

  enumerating
  timesRepeat:

  mathematical functions
  degreeCos
degreeSin
factorial
gcd:
lcm:
raisedTo:modulo:
take:

  printing
  asStringWithCommas
asStringWithCommasSigned
asTwoCharacterString
asWords
byteEncode:base:
destinationBuffer:
digitBuffer:
hex
hex8
isLiteral
printOn:base:
radix:
romanString

  private
  copyto:
digitAdd:
digitCompare:
digitDiv:neg:
digitLogic:op:length:
digitLshift:
digitMultiply:neg:
digitRshift:bytes:lookfirst:
digitSubtract:
growby:
growto:
romanDigits:for:on:

  system primitives
  lastDigit
replaceFrom:to:with:startingAt:

  testing
  even
isInteger
isPowerOfTwo

  tiles
  asPrecedenceName

  truncation and round off
  asLargerPowerOfTwo
asPowerOfTwo
asSmallerPowerOfTwo
atRandom
atRandom:
ceiling
floor
normalize
rounded
truncated

class methods
  examples
  exampleFor:

  instance creation
  basicNew
byte1:byte2:byte3:byte4:
initializedInstance
new
new:neg:
readFrom:
readFrom:base:
testBitLogic

  prime numbers
  largePrimesUpTo:
largePrimesUpTo:do:
primesUpTo:
primesUpTo:do:
verbosePrimesUpTo:
verbosePrimesUpTo:do:

  testing
  testTwoComplementRightShift

instance methods
  arithmetic top  
 

*

Refer to the comment in Number *


 

+

Refer to the comment in Number +


 

-

Refer to the comment in Number -


 

/

Refer to the comment in Number /


 

//

Integer quotient defined by division with truncation toward negative
infinity. 9//4 = 2, -9//4 = -3. -0.9//0.4 = -3. \\ answers the remainder
from this division.


 

\\\

a modulo method for use in DSA. Be careful if you try to use this elsewhere


 

alignedTo:

Answer the smallest number not less than receiver that is a multiple of anInteger.


 

quo:

Refer to the comment in Number quo:


  benchmarks top  
 

benchFib

Handy send-heavy benchmark


 

benchmark

Handy bytecode-heavy benchmark


 

tinyBenchmarks

Report the results of running the two tiny Squeak benchmarks.
ar 9/10/1999: Adjusted to run at least 1 sec to get more stable results


  bit manipulation top  
 

<<

left shift


 

>>

right shift


 

allMask:

Treat the argument as a bit mask. Answer whether all of the bits that
are 1 in the argument are 1 in the receiver.


 

anyBitOfMagnitudeFrom:to:

Tests for any magnitude bits in the interval from start to stopArg.


 

anyMask:

Treat the argument as a bit mask. Answer whether any of the bits that
are 1 in the argument are 1 in the receiver.


 

bitAnd:

Answer an Integer whose bits are the logical AND of the receiver's bits
and those of the argument, n.


 

bitClear:

Answer an Integer equal to the receiver, except with all bits cleared that are set in aMask.


 

bitInvert

Answer an Integer whose bits are the logical negation of the receiver's bits.
Numbers are interpreted as having 2's-complement representation.


 

bitInvert32

Answer the 32-bit complement of the receiver.


 

bitOr:

Answer an Integer whose bits are the logical OR of the receiver's bits
and those of the argument, n.


 

bitShift:

Answer an Integer whose value (in twos-complement representation) is
the receiver's value (in twos-complement representation) shifted left by
the number of bits indicated by the argument. Negative arguments
shift right. Zeros are shifted in from the right in left shifts.


 

bitShiftMagnitude:

Answer an Integer whose value (in magnitude representation) is
the receiver's value (in magnitude representation) shifted left by
the number of bits indicated by the argument. Negative arguments
shift right. Zeros are shifted in from the right in left shifts.


 

bitXor:

Answer an Integer whose bits are the logical XOR of the receiver's bits
and those of the argument, n.


 

highBit

Answer the index of the high order bit of the receiver, or zero if the
receiver is zero. Raise an error if the receiver is negative, since
negative integers are defined to have an infinite number of leading 1's
in 2's-complement arithmetic. Use >>highBitOfMagnitude if you want to
get the highest bit of the magnitude.


 

highBitOfMagnitude

Answer the index of the high order bit of the magnitude of the
receiver, or zero if the receiver is zero.


 

lowBit

Answer the index of the low order bit of this number.


 

noMask:

Treat the argument as a bit mask. Answer whether none of the bits that
are 1 in the argument are 1 in the receiver.


  comparing top  
 

<

Answer whether the receiver is less than the argument.


 

=

Compare the receiver with the argument and answer with true if the
receiver is equal to the argument. Otherwise answer false.


 

>

Answer whether the receiver is greater than the argument.


 

hash

Hash is reimplemented because = is implemented.


  converting top  
 

adaptToFraction:andSend:

If I am involved in arithmetic with a Fraction, convert me to a Fraction.


 

asCharacter

Answer the Character whose value is the receiver.


 

asColorOfDepth:

Return a color value representing the receiver as color of the given depth


 

asFloat

Answer a Float that represents the value of the receiver.
Optimized to process only the significant digits of a LargeInteger.
SqR: 11/30/1998 21:11


 

asFraction

Answer a Fraction that represents value of the the receiver.


 

asHexDigit


 

asInteger

Answer with the receiver itself.


  enumerating top  
 

timesRepeat:

Evaluate the argument, aBlock, the number of times represented by the
receiver.


  mathematical functions top  
 

degreeCos

Answer the cosine of the receiver taken as an angle in degrees.


 

degreeSin

Answer the sine of the receiver taken as an angle in degrees.


 

factorial

Answer the factorial of the receiver.


 

gcd:

See Knuth, Vol 2, 4.5.2, Algorithm L


 

lcm:

Answer the least common multiple of the receiver and n.


 

raisedTo:modulo:

Answer the modular exponential. Code by Jesse Welton.


 

take:

Return the number of combinations of (self) elements taken kk at a time. For 6 take 3, this is 6*5*4 / (1*2*3). Zero outside of Pascal's triangle. Use a trick to go faster.


  printing top  
 

asStringWithCommas

123456789 asStringWithCommas


 

asStringWithCommasSigned

123456789 asStringWithCommasSigned


 

asTwoCharacterString

Answer a two-character string representing the receiver, with leading zero if required. Intended for use with integers in the range 0 to 99, but plausible replies given for other values too


 

asWords

SmallInteger maxVal asWords


 

byteEncode:base:

Print a representation of the receiver on the stream, aStream, in base, b,
where 2<=b<=16.


 

destinationBuffer:


 

digitBuffer:


 

hex


 

hex8

16r3333 hex8


 

isLiteral

Answer whether the receiver has a literal text form recognized by the
compiler.


 

printOn:base:

Print a representation of the receiver on the stream, aStream, in base, b,
where 2<=b<=16.


 

radix:

Answer a String representing the receiver as a base radix integer.


 

romanString

1999 romanString


  private top  
 

copyto:


 

digitAdd:


 

digitCompare:

Compare the magnitude of self with that of arg.
Return a code of 1, 0, -1 for self >, = , < arg


 

digitDiv:neg:

Answer with an array of (quotient, remainder).


 

digitLogic:op:length:


 

digitLshift:


 

digitMultiply:neg:


 

digitRshift:bytes:lookfirst:

Shift right 8*b+anInteger bits, 0<=n<8.
Discard all digits beyond a, and all zeroes at or below a.


 

digitSubtract:


 

growby:


 

growto:


 

romanDigits:for:on:


  system primitives top  
 

lastDigit

Answer the last digit of the integer base 256. LargePositiveInteger uses bytes of base two number, and each is a 'digit'.


 

replaceFrom:to:with:startingAt:

Catches failure if LgInt replace primitive fails


  testing top  
 

even

Refer to the comment in Number|even.


 

isInteger

True for all subclasses of Integer.


 

isPowerOfTwo

Return true if the receiver is an integral power of two.


  tiles top  
 

asPrecedenceName


  truncation and round off top  
 

asLargerPowerOfTwo

Convert the receiver into a power of two which is not less than the receiver


 

asPowerOfTwo

Convert the receiver into a power of two


 

asSmallerPowerOfTwo

Convert the receiver into a power of two which is not larger than the receiver


 

atRandom

Answer a random integer from 1 to self. This implementation uses a
shared generator. Heavy users should their own implementation or use
Interval>atRandom: directly.


 

atRandom:

Answer a random integer from 1 to self picked from aGenerator.


 

ceiling

Refer to the comment in Number|ceiling.


 

floor

Refer to the comment in Number|floor.


 

normalize

SmallInts OK; LgInts override


 

rounded

Refer to the comment in Number|rounded.


 

truncated

Refer to the comment in Number|truncated.


class methods
  examples top  
 

exampleFor:

Provide a MethodCall with a valid example of aSelector, ready to run. If 'all', provide all tests we know about for SUnit tests. When 'all', each will be a Verifier, with an expected answer. Returns a list of MethodCalls, Verifiers, or VerifierOfPropertys.


  instance creation top  
 

basicNew

Primitive. Answer an instance of the receiver (which is a class) with no
indexable variables. Fail if the class is indexable. Essential. See Object
documentation whatIsAPrimitive.


 

byte1:byte2:byte3:byte4:

Depending on high-order byte copy directly into a LargeInteger,
or build up a SmallInteger by shifting


 

initializedInstance

Answer an instance of the receiver which in some sense is initialized. In the case of Morphs, this will yield an instance that can be attached to the Hand after having received the same kind of basic initialization that would be obtained from an instance chosen from the 'new morph' menu. Return nil if the receiver is reluctant for some reason to return such a thing


 

new

Answer a new instance of the receiver (which is a class) with no indexable variables. Fail if the class is indexable.


 

new:neg:

Answer an instance of a large integer whose size is length. neg is a flag
determining whether the integer is negative or not.


 

readFrom:

Answer a new Integer as described on the stream, aStream.
Embedded radix specifiers not allowed - use Number readFrom: for that.


 

readFrom:base:

Answer an instance of one of my concrete subclasses. Initial minus sign
accepted, and bases > 10 use letters A-Z. Embedded radix specifiers not
allowed--use Number readFrom: for that. Answer zero (not an error) if
there are no digits.


 

testBitLogic

Integer testBitLogic


  prime numbers top  
 

largePrimesUpTo:

Compute and return all the prime numbers up to maxValue


 

largePrimesUpTo:do:

Evaluate aBlock with all primes up to maxValue.
The Algorithm is adapted from http://www.rsok.com/~jrm/printprimes.html
It encodes prime numbers much more compactly than #primesUpTo:
38.5 integer per byte (2310 numbers per 60 byte) allow for some fun large primes.
(all primes up to SmallInteger maxVal can be computed within ~27MB of memory;
the regular #primesUpTo: would require 4 *GIGA*bytes).
Note: The algorithm could be re-written to produce the first primes (which require
the longest time to sieve) faster but only at the cost of clarity.


 

primesUpTo:

Return a list of prime integers up to the given integer.


 

primesUpTo:do:

Compute aBlock with all prime integers up to the given integer.


 

verbosePrimesUpTo:

Integer verbosePrimesUpTo: SmallInteger maxVal


 

verbosePrimesUpTo:do:

Integer verbosePrimesUpTo: SmallInteger maxVal


  testing top  
 

testTwoComplementRightShift

self testTwoComplementRightShift