eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'SmallInteger':

Home

everywhere
www.exept.de
for:
[back]

Class: SmallInteger


Inheritance:

   Object
   |
   +--Magnitude
      |
      +--ArithmeticValue
         |
         +--Number
            |
            +--Integer
               |
               +--SmallInteger

Package:
stx:libbasic
Category:
Magnitude-Numbers
Version:
rev: 1.185 date: 2010/04/13 18:58:23
user: cg
file: SmallInteger.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Claus Gittinger

Description:


SmallIntegers are Integers in the range of at least +/- 2^30
(i.e. 31 bits, which is not a guaranteed: on an alpha, 63 bits are used,
 if the system was configured for 64bit mode).

These are no real objects - they have no instances (not even storage !)
and cannot be subclassed.
The reason is to save both storage and runtime by not collecting
SmallIntegers in the system. SmallInts are marked by having the TAG_INT
bit set, in contrast to all other objects which do not.
Since this knowledge is hardwired into the system (and there is no
class-field stored with SmallIntegers) there can be no subclass of
SmallInteger (sorry).

If you really need this kind of thing, create a subclass of Integer,
with an instance variable holding the value.


Related information:

    Number
    Float
    Fraction
    FixedPoint
    LargeInteger

Class protocol:

bit mask constants
o  bitMaskFor: index
return a bitmask for the index's bit (index starts at 1)

constants
o  maxBits
return the number of bits in instances of me.
For very special uses only - not constant across implementations

o  maxBytes
return the number of bytes in instances of me.
For very special uses only - not constant across implementations

o  maxVal
return the largest Integer representable as SmallInteger.
For very special uses only - not constant across implementations

o  minVal
return the smallest Integer representable as SmallInteger.
For very special uses only - not constant across implementations

instance creation
o  basicNew
catch instance creation
- SmallIntegers cannot be created with new

o  basicNew: size
catch instance creation
- SmallIntegers cannot be created with new

queries
o  canBeSubclassed
return true, if its allowed to create subclasses of the receiver.
Return false here - since it is NOT possible for SmallInteger
(due to the tagged representation of SmallIntegers)

o  hasImmediateInstances
return true if this class has immediate instances
i.e. if the instances are represented in the pointer itself and
no real object header/storage is used for the object.
Redefined from Behavior

o  isBuiltInClass
return true if this class is known by the run-time-system.
Here, true is returned.


Instance protocol:

arithmetic
o  * aNumber
return the product of the receiver and the argument

o  + aNumber
return the sum of the receivers value and the arguments value

o  - aNumber
return the difference of the receivers value and the arguments value

o  / aNumber
return the quotient of the receivers value and the arguments value

o  // aNumber
return the integer part of the quotient of the receivers value
and the arguments value. The result is truncated toward negative infinity
and negative, if the operands signs differ.
Be careful with negative results: 9 // 4 = 2,
while -9 // 4 = -3.
The following is always true:
(receiver // aNumber) * aNumber + (receiver \\ aNUmber) = receiver
See #quo: which returns -2 in the latter.

o  \\ aNumber
Answer the integer remainder m defined by division with truncation toward
negative infinity. The remainder has the same sign as aNumber.
m < |aNumber| AND there is an integer k with (k * aNumber + m) = self
The following is always true:
(receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
Compare with #rem:

Redefined for speed.

o  abs
return the absolute value of the receiver
reimplemented here for speed

o  negated
return the negative value of the receiver
reimplemented here for speed

o  quo: aNumber
return the integer part of the quotient of the receivers value
and the arguments value. The result is truncated towards zero
and negative, if the operands signs differ..
The following is always true:
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
For positive results, this is the same as #//,
for negative results, the remainder is ignored.
I.e.: '9 // 4 = 2' and '-9 // 4 = -3'
in contrast: '9 quo: 4 = 2' and '-9 quo: 4 = -2'

binary storage
o  identityHashForBinaryStore

bit operators
o  bitAnd: anInteger
return the bitwise-and of the receiver and the argument, anInteger

o  bitAt: anIntegerIndex
return the value of the index's bit (index starts at 1) as 0 or 1.
Notice: the result of bitAt: on negative receivers is not
defined in the language standard (since the implementation
is free to choose any internal representation for integers)

o  bitClear: anInteger
return the bitwise-and of the receiver and the complement of the argument, anInteger,
returning the receiver with bits of the argument cleared.

o  bitCount
return the number of 1-bits in the receiver

o  bitInvert
return the value of the receiver with all bits inverted

o  bitOr: anInteger
return the bitwise-or of the receiver and the argument, anInteger

o  bitShift: shiftCount
return the value of the receiver shifted by shiftCount bits;
leftShift if shiftCount > 0; rightShift otherwise.
Notice: the result of bitShift: on negative receivers is not
defined in the language standard (since the implementation
is free to choose any internal representation for integers).
However, ST/X preserves the sign.

o  bitTest: aMask
return true, if any bit from aMask is set in the receiver.
I.e. true, if the bitwise-AND of the receiver and the argument, anInteger
is non-0, false otherwise.

o  bitXor: anInteger
return the bitwise-exclusive-or of the receiver and the argument, anInteger

o  clearBit: anInteger
return a new integer where the specified bit is off.
Bits are counted from 1 starting with the least significant.
The methods name may be missleading: the receiver is not changed,
but a new number is returned. Should be named #withBitCleared:

o  highBit
return the bitIndex of the highest bit set. The returned bitIndex
starts at 1 for the least significant bit.
Returns 0 if no bit is set.

o  invertBit: anInteger
return a new number where the specified bit is inverted.
Bits are counted from 1 starting with the least significant.
The methods name may be missleading: the receiver is not changed,
but a new number is returned. Should be named #withBitInverted:

o  lowBit
return the bitIndex of the lowest bit set. The returned bitIndex
starts at 1 for the least significant bit.
Returns 0 if no bit is set.

o  rightShift: shiftCount
return the value of the receiver shifted by shiftCount bits;
right shift if shiftCount > 0; left shift otherwise.
Notice: the result of bitShift: on negative receivers is not
defined in the language standard (since the implementation
is free to choose any internal representation for integers).
However, ST/X preserves the sign.

o  setBit: anInteger
return a new integer where the specified bit is on.
Bits are counted from 1 starting with the least significant.
The methods name may be missleading: the receiver is not changed,
but a new number is returned. Should be named #withBitSet:

bit operators-32bit
o  asSigned32
return a 32-bit integer with my bit-pattern. For protocol completeness.

o  asUnsigned32
return a 32-bit integer with my bit-pattern, but positive.
May be required for bit operations on the sign-bit and/or to
convert C/Java numbers.

o  bitInvert32
return the value of the receiver with all bits inverted in 32bit signed int space
(changes the sign)

o  bitRotate32: shiftCount
return the value of the receiver rotated by shiftCount bits,
but only within 32 bits, rotating left for positive, right for negative counts.
Rotates through the sign bit.
Useful for crypt algorithms, or to emulate C/Java semantics.

o  bitShift32: shiftCount
return the value of the receiver shifted by shiftCount bits,
but only within 32 bits, shifting into/out-of the sign bit.
May be useful for communication interfaces, to create ST-numbers
from a signed 32bit int value given as individual bytes,
or to emulate C/Java semantics.

o  bitXor32: aNumber
return the xor of the receiver and the argument.
The argument must be another SmallInteger or a 4-byte LargeInteger.
If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned.
This is of course not always correct, but allows for C/Java behavior to be emulated.

o  unsignedBitShift32: shiftCount
return the value of the receiver shifted by shiftCount bits,
but only within 32 unsigned bits.
May be useful for communication interfaces, to create ST-numbers
from an unsigned 32bit int value given as individual byte,
or to emulate C/Java semantics.

byte access
o  digitAt: index
return 8 bits of value, starting at byte index

o  digitByteAt: index
return 8 bits of my signed value, starting at byte index.
For positive receivers, this is the same as #digitAt:;
for negative ones, the actual bit representation is returned.

o  digitBytes
return a byteArray filled with the receivers bits
(8 bits of the absolute value per element),
least significant byte is first

o  digitBytesMSB: msbFlag
return a byteArray filled with the receivers bits
(8 bits of the absolute value per element),
if msbflag = true, most significant byte is first,
otherwise least significant byte is first

o  digitLength
return the number bytes required to represent this Integer.
For negative receivers, the digitLength of its absolute value
is returned.

catching messages
o  basicAt: index
catch indexed access - report an error
defined here since basicAt: in Object ommits the SmallInteger check.

o  basicAt: index put: anObject
catch indexed access - report an error
defined here since basicAt:put: in Object ommits the SmallInteger check.

o  basicSize
return the number of indexed instvars - SmallIntegers have none.
Defined here since basicSize in Object ommits the SmallInteger check.

o  size
return the number of indexed instvars - SmallIntegers have none.

coercing & converting
o  asCharacter
Return a character with the receiver as ascii value

o  asFloat
return a Float with same value as receiver.
Redefined for performance (machine can do it faster)

o  asLargeInteger
return a LargeInteger with same value as receiver

o  asShortFloat
return a ShortFloat with same value as receiver.
Redefined for performance (machine can do it faster)

o  codePoint
for compatibility with Characters.
(Allows for integers to be stored into U16/U32 strings)

o  coerce: aNumber
convert the argument aNumber into an instance of the receivers class and return it.

o  generality
return the generality value - see ArithmeticValue>>retry:coercing:

o  signExtended24BitValue
return a smallInteger from sign-extending the 24'th bit.
May be useful for communication interfaces

o  signExtendedByteValue
return a smallInteger from sign-extending the 8'th bit.
May be useful for communication interfaces

o  signExtendedShortValue
return a smallInteger from sign-extending the 16'th bit.
May be useful for communication interfaces

comparing
o  < aNumber
return true, if the argument is greater than the receiver

o  <= aNumber
return true, if the argument is greater or equal

o  = aNumber
return true, if the argument represents the same numeric value
as the receiver, false otherwise

o  > aNumber
return true, if the argument is less than the receiver

o  >= aNumber
return true, if the argument is less or equal

o  hash
return an integer useful for hashing on value

o  identityHash
return an integer useful for hashing on identity

o  max: aNumber
return the receiver or the argument, whichever is greater

o  min: aNumber
return the receiver or the argument, whichever is smaller

o  ~= aNumber
return true, if the arguments value is not equal to mine

copying
o  deepCopy
return a deep copy of myself
- reimplemented here since smallintegers are unique

o  deepCopyUsing: aDictionary postCopySelector: postCopySelector
return a deep copy of myself
- reimplemented here since smallintegers are unique

o  shallowCopy
return a shallow copy of myself
- reimplemented here since smallintegers are unique

o  simpleDeepCopy
return a deep copy of myself
- reimplemented here since smallintegers are unique

iteration
o  timesRepeat: aBlock
evaluate the argument, aBlock self times.
Reimplemented as primitive for speed

o  to: stop by: incr do: aBlock
reimplemented as primitive for speed

o  to: stop do: aBlock
evaluate aBlock for every integer between (and including) the receiver
and the argument, stop.
Reimplemented as primitive for speed

misc math
o  bernoulli
returns the nth Bernoulli number.
The series runs this:
1/6, 1/30, 1/42, 1/30, 5/66, 691/2730, etc
Uses a table of the first 20 bernoulli numbers.
So bernoulli(42) will fail for now.
Used with taylor series for tan

o  divMod: aNumber
return an array filled with self // aNumber and
self \\ aNumber.
The result is only defined for positive receiver and
argument.

o  gcd: anInteger
return the greatest common divisor (Euclid's algorithm).
This has been redefined here for more speed since due to the
use of gcd in Fraction code, it has become time-critical for
some code. (thanx to MessageTally)

o  gcd_helper: anInteger
same as gcd - see knuth & Integer>>gcd:

o  intlog10
return the truncation of log10 of the receiver -
stupid implementation; used to find out the number of digits needed
to print a number/and for conversion to a LargeInteger.
Implemented that way, to allow for tiny systems (PDAs) without a Float class
(i.e. without log).

modulo arithmetic
o  plus32: aNumber
return the sum of the receiver and the argument, as SmallInteger.
The argument must be another SmallInteger.
If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned.
This is of course not always correct, but allows for C/Java behavior to be emulated.

o  plus: aNumber
return the sum of the receiver and the argument, as SmallInteger.
The argument must be another SmallInteger.
If the result overflows the smallInteger range, the value modulo the
smallInteger range is returned (i.e. the low bits of the sum).
This is of course not always correct, but some code does a modulo anyway
and can therefore speed things up by not going through LargeIntegers.

o  subtract: aNumber
return the difference of the receiver and the argument, as SmallInteger.
The argument must be another SmallInteger.
If the result overflows the smallInteger range, the value modulo the
smallInteger range is returned (i.e. the low bits of the sum).
This is of course not always correct, but some code does a modulo anyway
and can therefore speed things up by not going through LargeIntegers.

o  times: aNumber
return the product of the receiver and the argument, as SmallInteger.
The argument must be another SmallInteger.
If the result overflows the smallInteger range, the value modulo the
smallInteger range is returned (i.e. the low bits of the product).
This is of course not always correct, but some code does a modulo anyway
and can therefore speed things up by not going through LargeIntegers.

printing & storing
o  printOn: aStream
append my printstring (base 10) to aStream.

o  printOn: aStream base: base showRadix: showRadix
append a string representation of the receiver in the specified numberBase to aStream
(if showRadix is true, with initial XXr)
The base argument should be between 2 and 36.

o  printString
return my printstring (base 10)

o  printStringRadix: base
return my printstring (optimized for bases 16, 10 and 8)

o  printfPrintString: formatString
non-standard, but sometimes useful.
return a printed representation of the receiver
as specified by formatString, which is defined by the C-
function 'printf'.
No checking for string overrun - the resulting string
must be shorter than 256 chars or else ...
This method is NONSTANDARD and may be removed without notice;
it is provided to allow special conversions in very special
situaltions.
Notice that a conversion may not be portable; for example,
to correctly convert an int on a 64-bit alpha, a %ld is required,
while other systems may be happy with a %d ...
Use at your own risk (if at all).
WARNNG: this goes directly to the C-printf function and may therefore me inherently unsafe.
Please use the printf: method, which is safe as it is completely implemented in Smalltalk.

private
o  sign: aNumber
private: for protocol completeness with LargeIntegers

testing
o  between: min and: max
return true if the receiver is less than or equal to the argument max
and greater than or equal to the argument min.
- reimplemented here for speed

o  even
return true, if the receiver is even

o  negative
return true, if the receiver is less than zero
reimplemented here for speed

o  odd
return true, if the receiver is odd

o  positive
return true, if the receiver is not negative
reimplemented here for speed

o  sign
return the sign of the receiver (-1, 0 or 1).
reimplemented here for speed

o  strictlyPositive
return true, if the receiver is greater than zero
reimplemented here for speed



ST/X 6.1.1; WebServer 1.620 at exept:8081; Wed, 23 May 2012 21:06:55 GMT