From: David Bakhash
Subject: bit-vectors...
Date: 
Message-ID: <cxjr9zltvuu.fsf@engc.bu.edu>
hey,

I'm trying to write some simulation software in CL so I can do some
algorithm verification in CL instead of Verilog.  But I don't feel at
ease with the bit-array stuff.  Here are some of my questions:

1) If I specifiy something like this:

(setq b #*00111111)

then what's the LSB?  Is it a 0 or a 1?  It seems that it's a 0 in
this case, and that the LSB is on the left.  I don't like that, but I
guess it's the way Lisp is, right?  i.e. if I do:

(sbit b 0)

==> 0

suggesting that #*01 is a decimal 2 and NOT a decimal 1.

2) There seems to be a distinction between bit-arrays and
   simple-bit-arrays.  What is it?  Can someone make me a bit-array
   that's not simple?  does it have to do with multiple dimensions?

3) If I wanted to convert a decimal number to binary (i.e. into a
   bit-vector), do I have to write my own routine, or can Lisp do this 
   type of stuff for me?

thanks,
dave

From: Tim Bradshaw
Subject: Re: bit-vectors...
Date: 
Message-ID: <ey3k95ciw7e.fsf@todday.aiai.ed.ac.uk>
* David Bakhash wrote:
> 1) If I specifiy something like this:

> (setq b #*00111111)

> then what's the LSB?  Is it a 0 or a 1?  It seems that it's a 0 in
> this case, and that the LSB is on the left.  I don't like that, but I
> guess it's the way Lisp is, right?  i.e. if I do:

I don't think this question makes sense.  A bit vector is a
one-dimensional array of bits, not a number.  The 0th element of it is
indeed printed at the left, in the same way as the 0th element of 
#(1 2 3) is 1.


> 2) There seems to be a distinction between bit-arrays and
>    simple-bit-arrays.  What is it?  Can someone make me a bit-array
>    that's not simple?  does it have to do with multiple dimensions?

Any array is simple if you didn't ask for it to be adjustable (I think
that's right -- you need to allow for implementations which make all
arrays adjustable).  You can make a non-simple bit array by:

	(make-array 10 :element-type 'bit :adjustable t)

> 3) If I wanted to convert a decimal number to binary (i.e. into a
>    bit-vector), do I have to write my own routine, or can Lisp do this 
>    type of stuff for me?

I think you have to write your own. Note that if you want short bit
vectors, especially ones less than ~30 bits, using integers can be
quite a good trick in terms of efficiency, although possibly not in
terms of clarity.

--tim
From: Raymond Toy
Subject: Re: bit-vectors...
Date: 
Message-ID: <4nd8b4d85y.fsf@rtp.ericsson.se>
>>>>> "David" == David Bakhash <·····@bu.edu> writes:

    David> 3) If I wanted to convert a decimal number to binary (i.e. into a
    David>    bit-vector), do I have to write my own routine, or can Lisp do this 
    David>    type of stuff for me?

Here's what I used (from memory).  It's easy, but not very efficient.
:-)

(defun int->bitvec (n)
   (read-from-string (format nil "#*~b" n)))

So 123 -> #*1111011.  Note that the bits might be in the wrong order 
for what you want.  If so, just do a reverse on the result.

Ray
From: Tim Bradshaw
Subject: Re: bit-vectors...
Date: 
Message-ID: <ey3vhow4dc1.fsf@todday.aiai.ed.ac.uk>
* Raymond Toy wrote:

> (defun int->bitvec (n)
>    (read-from-string (format nil "#*~b" n)))

As part of my secret campaign to followup all posts to c.l.l using
READ-FROM-STRING with better solutions (:):

    (defun integer->bitvector (i)
      ;; get a corresponding bitvector from an integer
      (declare (type integer i))
      (loop with len of-type integer = (integer-length i)
	    with v of-type bit-vector = (make-array len :element-type 'bit)
	    for b of-type integer from 0 below len
	    do (setf (bit v b) (if (logbitp b i) 1 0))
	    finally (return v)))

    (defun bitvector->integer (v)
      ;; integerify a bitvector
      (declare (type (array bit *) v))
      (loop for i of-type fixnum from 0 below (length v)
	    sum (if (not (zerop (bit v i)))
		    (ash 1 i)
		    0)))

There are obvious neat recursive ones too, though I think these are
pretty efficient.

--tim
From: Barry Margolin
Subject: Re: bit-vectors...
Date: 
Message-ID: <Z3xr1.30$P34.466377@cam-news-reader1.bbnplanet.com>
In article <···············@engc.bu.edu>, David Bakhash  <·····@bu.edu> wrote:
>1) If I specifiy something like this:
>
>(setq b #*00111111)
>
>then what's the LSB?  Is it a 0 or a 1?  It seems that it's a 0 in
>this case, and that the LSB is on the left.  I don't like that, but I
>guess it's the way Lisp is, right?  i.e. if I do:
>
>(sbit b 0)
>
>==> 0
>
>suggesting that #*01 is a decimal 2 and NOT a decimal 1.

If you care what the numeric value is, you probably shouldn't be using bit
vectors.  Bit vectors are intended to be used as vectors of boolean data.
Perhaps you really should be using the boole-XXX functions on integers.

>2) There seems to be a distinction between bit-arrays and
>   simple-bit-arrays.  What is it?  Can someone make me a bit-array
>   that's not simple?  does it have to do with multiple dimensions?

A simple-bit-array is a simple-array with :ELEMENT-TYPE BIT.  A
simple-array is an array that was created with :ADJUSTABLE, :FILL-POINTER,
and :DISPLACED-TO all being NIL (either explicitly or by default).

>3) If I wanted to convert a decimal number to binary (i.e. into a
>   bit-vector), do I have to write my own routine, or can Lisp do this 
>   type of stuff for me?

I don't think there's a built-in function to do it.  This seems like
another reason why you probably should use the logical operations on
integers.  See section 12.7 of CLtL2.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.