Skip to main content
Engineering LibreTexts

14.7: SmallIntegers in Pharo

  • Page ID
    46307
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Pharo small integers use a 2's complement arithmetic on 31 bits. A N-bit two's-complement numeral system can represent every integer in the range \( -1 * 2^{N-1} \) to \( 2^{N-1} - 1 \). So for 31 bits Pharo systems small integer values are the range \( -1 073 741 824 \) to \( 1 073 741 823 \). Remember that in Pharo integers are special objects and this marking requires one bit, therefore on 32 bits we have 31 bits for small signed integers. Of course since we also have automatic coercion this is not really a concern for the end programmer. Here we take a language implementation perspective. Let's check this encoding a bit (this is the occasion to say it).

    SmallInteger Maximum value encoding. Pharo’s small integers are encoded on 31 bits (because their internal representation requires one bit) and the smallest (small integer) negative integer is SmallInteger maxVal negated - 1. Here we see the exception of the most negative integer.

    "we negate the maximum number encoded on a small integer"
    SmallInteger maxVal negated
        → -1073741823
    "we still obtain a small integer"
    SmallInteger maxVal negated class
        → SmallInteger
    "adding one to the maximum number encoded on a small integer gets a large positive
        integer" 
    (SmallInteger maxVal + 1) class
        → LargePositiveInteger
    "But the smallest negative is one less than the negated largest positive small integer"
    (SmallInteger maxVal negated - 1)
        → -1073741824
    (SmallInteger maxVal negated - 1) class
        → SmallInteger
    

    Understanding some methods. If you want to know the number of bits used to represent a SmallInteger, just evaluate:

    SmallInteger maxVal highBit + 1
        returns 31
    

    SmallInteger maxVal highBit tells the highest bit which can be used to represent a positive SmallInteger, and + 1 accounts for the sign bit of the SmallInteger (0 for positive, 1 for negative).

    Let us explore a bit.

    2 raisedTo: 29
        → 536870912
    
    536870912 class
        → SmallInteger
    
    2 raisedTo: 30
        → 1073741824
    
    1073741824 class
        → LargePositiveInteger
    
    (1073741824 - 1) class
        → SmallInteger
    
    -1073741824 class
        → SmallInteger
    
    2 class maxVal
        returns 1073741823
    
    -1 * (2 raisedTo: (31-1))
        → -1073741824
    
    (2 raisedTo: 30) - 1
        → 1073741823
    
    (2 raisedTo: 30) - 1 = SmallInteger maxVal
        → true
    

    This page titled 14.7: SmallIntegers in Pharo is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.