[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Q: Number2Bit-Vector Conversion ?



> does someone know, if there exists a function in clisp to
> convert a number into a bit-vector ?

There is no such function built-in, but you can write a portable one.

(defun to-bitvector (x &optional (bits 0 bitsp))
  (assert (typep x '(integer 0 *)))
  (if bitsp
    (assert (<= (integer-length x) bits))
    (setq bits (integer-length x))
  )
  (let ((v (make-array bits :element-type 'bit)))
    (dotimes (i bits)
      (when (logbitp i x) (setf (aref v i) 1))
    )
    v
) )

Bruno