Fixnum Operations - MIT/GNU Scheme 7.7.90+
Description: MIT/GNU Scheme 7.7.90+

Next: Flonum Operations, Previous: Fixnum and Flonum Operations, Up: Fixnum and Flonum Operations

#### 4.7.1 Fixnum Operations

A fixnum is an exact integer that is small enough to fit in a machine word. In MIT/GNU Scheme, fixnums are typically 24 or 26 bits, depending on the machine; it is reasonable to assume that fixnums are at least 24 bits. Fixnums are signed; they are encoded using 2's complement.

All exact integers that are small enough to be encoded as fixnums are always encoded as fixnums — in other words, any exact integer that is not a fixnum is too big to be encoded as such. For this reason, small constants such as `0` or `1` are guaranteed to be fixnums.

— procedure: fix:fixnum? object

> Returns `#t` if object is a fixnum; otherwise returns `#f`.

Here is an expression that determines the largest fixnum:

         (let loop ((n 1))
           (if (fix:fixnum? n)
               (loop (* n 2))
               (- n 1)))

A similar expression determines the smallest fixnum.

— procedure: fix:= fixnum fixnum
— procedure: fix:< fixnum fixnum
— procedure: fix:> fixnum fixnum
— procedure: fix:<= fixnum fixnum
— procedure: fix:>= fixnum fixnum

> These are the standard order and equality predicates on fixnums. When compiled, they do not check the types of their arguments.

— procedure: fix:zero? fixnum
— procedure: fix:positive? fixnum
— procedure: fix:negative? fixnum

> These procedures compare their argument to zero. When compiled, they do not check the type of their argument. The code produced by the following expressions is identical:

>               (fix:zero? fixnum)
>               (fix:= fixnum 0)

> Similarly, `fix:positive?` and `fix:negative?` produce code identical to equivalent expressions using `fix:>` and `fix:<`.

— procedure: fix:+ fixnum fixnum
— procedure: fix:- fixnum fixnum
— procedure: fix:* fixnum fixnum
— procedure: fix:quotient fixnum fixnum
— procedure: fix:remainder fixnum fixnum
— procedure: fix:gcd fixnum fixnum
— procedure: fix:1+ fixnum
— procedure: fix:-1+ fixnum

> These procedures are the standard arithmetic operations on fixnums. When compiled, they do not check the types of their arguments. Furthermore, they do not check to see if the result can be encoded as a fixnum. If the result is too large to be encoded as a fixnum, a malformed object is returned, with potentially disastrous effect on the garbage collector.

— procedure: fix:divide fixnum fixnum

> This procedure is like `integer-divide`, except that its arguments and its results must be fixnums. It should be used in conjunction with `integer-divide-quotient` and `integer-divide-remainder`.

The following are bitwise-logical operations on fixnums.

— procedure: fix:not fixnum

> This returns the bitwise-logical inverse of its argument. When compiled, it does not check the type of its argument.

>               (fix:not 0)                             ⇒  -1
>               (fix:not -1)                            ⇒  0
>               (fix:not 1)                             ⇒  -2
>               (fix:not -34)                           ⇒  33

— procedure: fix:and fixnum fixnum

> This returns the bitwise-logical “and” of its arguments. When compiled, it does not check the types of its arguments.

>               (fix:and #x43 #x0f)                     ⇒  3
>               (fix:and #x43 #xf0)                     ⇒  #x40

— procedure: fix:andc fixnum fixnum

> Returns the bitwise-logical “and” of the first argument with the bitwise-logical inverse of the second argument. When compiled, it does not check the types of its arguments.

>               (fix:andc #x43 #x0f)                    ⇒  #x40
>               (fix:andc #x43 #xf0)                    ⇒  3

— procedure: fix:or fixnum fixnum

> This returns the bitwise-logical “inclusive or” of its arguments. When compiled, it does not check the types of its arguments.

>               (fix:or #x40 3)                         ⇒ #x43
>               (fix:or #x41 3)                         ⇒ #x43

— procedure: fix:xor fixnum fixnum

> This returns the bitwise-logical “exclusive or” of its arguments. When compiled, it does not check the types of its arguments.

>               (fix:xor #x40 3)                        ⇒ #x43
>               (fix:xor #x41 3)                        ⇒ #x42

— procedure: fix:lsh fixnum1 fixnum2

> This procedure returns the result of logically shifting fixnum1 by fixnum2 bits. If fixnum2 is positive, fixnum1 is shifted left; if negative, it is shifted right. When compiled, it does not check the types of its arguments, nor the validity of its result.

>               (fix:lsh 1 10)                          ⇒  #x400
>               (fix:lsh #x432 -10)                     ⇒  1
>               (fix:lsh -1 3)                          ⇒  -8
>               (fix:lsh -128 -4)                       ⇒  #x3FFFF8