From: rif
Subject: bit arrays vs. logical operations?
Date: 
Message-ID: <wj0br9aarlb.fsf@five-percent-nation.mit.edu>
What are the relative merits of using bit arrays or logical
operations?  As far as I can tell, the advantage of bit arrays is that
they print in a way that lets you easily tell what's set, and you can
get individual bits out without keeping a bunch of mask objects
around?  On the other hand, it seems that testing whether two bit
arrays are bitwise equivalent or turning the contents of a bitwise
array into an index (into a standard array, for example) require an
explicit accumulating loop?  Is this right?

rif

From: Wade Humeniuk
Subject: Re: bit arrays vs. logical operations?
Date: 
Message-ID: <3Nm0e.103622$fc4.9133@edtnps89>
rif wrote:
> What are the relative merits of using bit arrays or logical
> operations?  As far as I can tell, the advantage of bit arrays is that
> they print in a way that lets you easily tell what's set, and you can
> get individual bits out without keeping a bunch of mask objects
> around?  On the other hand, it seems that testing whether two bit
> arrays are bitwise equivalent or turning the contents of a bitwise
> array into an index (into a standard array, for example) require an
> explicit accumulating loop?  Is this right?
>

Nope,

CL-USER 5 > (type-of (map 'vector #'identity #*10110101))
SIMPLE-VECTOR

CL-USER 6 > (equal #*1011011 #*1100111)
NIL

CL-USER 7 > (equal #*1011011 #*1011011)
T


Wade
From: rif
Subject: Re: bit arrays vs. logical operations?
Date: 
Message-ID: <wj0fyym54a5.fsf@five-percent-nation.mit.edu>
Sorry, my question was insufficiently precise.  Let me clarify

1.  If I want to turn a bit-vector into an index into an array, do I
    need an explict accumulating loop?

2.  If I want to compare two small (less than a machine word size)
    bit-vectors for equality, is it clear that when I use equal, I'm
    going to get performance that efficiently uses the underlying
    hardware, or am I going to get the equivalent of a loop?  The
    hyperspec states that when two bit-vectors are compared using
    equal, they're compared element-by-element with eql.  This is the
    right semantics, but one also wants to know that this is as fast
    as checking that two numbers are eql.  I suspect the answer to
    this is implementation dependent.

Cheers,

rif
From: Bulent Murtezaoglu
Subject: Re: bit arrays vs. logical operations?
Date: 
Message-ID: <87mzstlehw.fsf@p4.internal>
>>>>> "rif" == rif  <ยทยทยท@mit.edu> writes:
[...]
    rif> 1.  If I want to turn a bit-vector into an index into an
    rif> array, do I need an explict accumulating loop? [...]

Let me try to understand this.  You want the bit pattern to be converted to 
a number, right?  EG: #*1001 turns into 9 (decimal)?  If so, then I don't 
believe we have a built-in function for doing this.  You might want to use
fixnums or smaller integers directly, and ldb/dpb etc. via an interface 
that is convenient for you for example

(defun get-bit (bits index)
  (ldb (byte 1 index) bits))

(defun set-bit (bits index value)
  (dpb value (byte 1 index) bits))

(defsetf get-bit set-bit)


CL-USER> #b1001
9
CL-USER> (get-bit 9 1) 
0
CL-USER> (set-bit 8 0 1)
9
CL-USER> (setf (get-bit 9 1) 1)
11
CL-USER> ; etc. 

If you want efficiency you'll need declarations and such of course.

cheers,

BM