Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# Octal Integer Numbers

XLISP provides the #o read-macro for octal numbers:

    #o0  => 0        #o10  => 8         #o20  => 16
    #o1  => 1        #o11  => 9         #o21  => 17
    #o2  => 2        #o12  => 10        #o22  => 18
    #o3  => 3        #o13  => 11        #o23  => 19
    #o4  => 4        #o14  => 12        #o24  => 20
    #o5  => 5        #o15  => 13        #o25  => 21
    #o6  => 6        #o16  => 14        #o26  => 22
    #o7  => 7        #o17  => 15        #o27  => 23

(oct-string integer [all])
  integer - an integer expression
  all - a boolean expression
  returns - the integer in octal form as string

    (defun oct-string (integer &optional all)
      (if (integerp integer)
          (let ((fmt (if all
                         (or (dolist (bits '(16 32 64 128) nil)
                               (let ((fixnum (round (expt 2.0 (1- bits)))))
                                 (and (plusp (1- fixnum))
                                      (minusp fixnum)
                                      (return (format nil "%.~ao"
                                                          (1+ (/ bits 3)))))))
                             (error "integer limit not found"))
                         "%o")))
            (progv '(*integer-format*) (list fmt)
              (format nil "~a" integer)))
          (error "not an integer" integer)))

The 'oct-string' function converts the 'integer' argument into octal form and returns is as a string. If the optional 'all' argument is not given or NIL, leading zeros are not included in the string. If the optional 'all' argument is non-NIL, all digits of the internal representation of the 'integer' argument, including leading zeros, are contained in the string. This is useful for debugging integer overflow and bit-wise functions.

(oct integer [all])
  integer - an integer expression
  all - a boolean expression
  prints - the integer in octal form
  returns - the integer argument

    (defun oct (integer &optional all)
      (if (integerp integer)
          (format t "#o~a~%" (oct-string integer all))
          (format t ";; not an integer~%"))
      integer)

The 'oct' function prints the 'integer' argument in octal form on the screen. Together with the #o read-macro this can be used for interactive octal computations.

    > (oct 12345678)
    #o57060516
    12345678

Back to top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference