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

ash


The 'ash' functio performs an 'arithmetic shift' operation:

(ash integer count)
integer - an integer expression
count - an integer expression
returns - the number shifted by count

(defun ash (integer count)
  (or (integerp integer) (error "not an integer" integer))
  (or (integerp count)   (error "not an integer" count))
  (let* ((shift (* integer (expt 2.0 count)))
         (trunc (truncate shift)))
    ;; XLISP implementation of (FLOOR SHIFT)
    (if (or (plusp shift) (= shift trunc))
        trunc
        (1- trunc))))

The 'ash' functio performs an arithmetic shift operation on the binary representation of the 'integer' argument, which is treated as if it were binary. The 'integer' argument is shifted arithmetically to the left by 'count' bit positions if 'count' is positive, or to the right by 'count' bit positions if 'count' is negative. The shifted value of the same sign as 'integer' is returned.

The 'ash' function performs the computation:

floor (integer * 2^count)

Logically, the 'ash' function moves all of the bits in integer to the left, adding zero-bits at the right, or moves them to the right, discarding bits.

The 'ash' function is defined to behave as if integer were represented in two's complement form, regardless of how integers are represented internally.

Examples:

(ash 16  1)  => 32
(ash 16  0)  => 16
(ash 16 -1)  => 8
(defun debug:ash (integer count)
  (let ((shifted (ash integer count)))
    (format t "integer: ~a~%" (bin-string integer :all))
    (format t "shifted: ~a~%" (bin-string shifted :all))
    shifted))

See Binary Integer Numbers for the 'bin-string' function.

  Back to top


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