Next: Flonum Operations, Previous: Fixnum and Flonum Operations, Up: Fixnum and Flonum 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.
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.
These are the standard order and equality predicates on fixnums. When compiled, they do not check the types of their arguments.
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?
andfix:negative?
produce code identical to equivalent expressions usingfix:>
andfix:<
.
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.
This procedure is like
integer-divide
, except that its arguments and its results must be fixnums. It should be used in conjunction withinteger-divide-quotient
andinteger-divide-remainder
.
The following are bitwise-logical operations on fixnums.
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
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
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
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
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
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