Hello, I'd like use Lisp to return the binary (base-2) representation of
a number as a list of integers, e.g. (binaryrep 9) => (1 0 0 1). I've
gotten as far as the "write-to-string" function,
> (write-to-string 9 :base 2)
=> "1001"
.. but now stuck on how to convert the "1001" string into a (1 0 0 1)
integer list. Any help appreciated --- thanks!
Drew Krause <········@mindspring.com> writes:
> Hello, I'd like use Lisp to return the binary (base-2) representation
> of a number as a list of integers, e.g. (binaryrep 9) => (1 0 0
> 1). I've gotten as far as the "write-to-string" function,
>
> > (write-to-string 9 :base 2)
> => "1001"
>
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!
You can actually treat an integer directly as a "string" of bits
fairly easily. Here's what I might use if I needed to get the bits of
an integer as a list:
(defun binary-digit-list (integer)
(let ((result '()))
(dotimes (i (integer-length integer) result)
(push (ldb (byte 1 i) integer) result))))
See also LOGBITP, LOGAND, LOGTEST, etc. Because of these (and related)
functions, I can't think of many situations where I'd use list of bits
in an integer instead of the integer itself.
Zach
Drew Krause wrote:
> Hello, I'd like use Lisp to return the binary (base-2) representation of
> a number as a list of integers, e.g. (binaryrep 9) => (1 0 0 1). I've
> gotten as far as the "write-to-string" function,
>
> > (write-to-string 9 :base 2)
> => "1001"
>
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!
Why use a string as intermediate value? With division and modulo
you can get the individual numbers and list them as you like.
--
No man is good enough to govern another man without that other's
consent. -- Abraham Lincoln
Drew Krause <········@mindspring.com> wrote:
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!
an iterative solution:
(loop for digit across (write-to-string 9 :base 2) collect
(if (char= digit #\1) 1 0))
or a more functional solution:
(map 'list
#'(lambda (x) (if (char= x #\1) 1 0))
(write-to-string 9 :base 2))
--
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Drew Krause <········@mindspring.com> writes:
> Hello, I'd like use Lisp to return the binary (base-2) representation
> of a number as a list of integers, e.g. (binaryrep 9) => (1 0 0
> 1). I've gotten as far as the "write-to-string" function,
>
> > (write-to-string 9 :base 2)
> => "1001"
>
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!
(coerce "1010" 'list)
;; gives a list of _characters_.
(map 'list (lambda (ch) (parse-integer (string ch))) "1010")
;; gives a list of integers.
(let ((string "1010"))
(loop for i from 0 below (length string)
collect (parse-integer string :start i :end (1+ i))))
;; too.
;; It might be better to stay with integers when converting bases:
(let ((n 12) (base 2))
(nreverse (loop for m = n then (truncate m base)
until (zerop m)
collect (mod m base))))
;; Usually, digits are kept in lists the less significant digit first,
;; (just remove the nreverse).
--
__Pascal Bourguignon__ http://www.informatimago.com/
Drew Krause wrote:
> Hello, I'd like use Lisp to return the binary (base-2) representation
> of a number as a list of integers, e.g. (binaryrep 9) => (1 0 0 1).
> I've gotten as far as the "write-to-string" function,
>
>> (write-to-string 9 :base 2)
> => "1001"
>
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!
Here's a way using a mapping function with <digit-char-p>.
CL-USER 9 >
(defun explode-integer-into-binary (in-integer)
(map 'list
#'digit-char-p
(write-to-string (the (integer 0 *) in-integer) :base 2)))
EXPLODE-INTEGER-INTO-BINARY
CL-USER 10 >
(explode-integer-into-binary 217)
(1 1 0 1 1 0 0 1)
Carl Taylor