Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bit arrays vs. logical operations?

5 views
Skip to first unread message

rif

unread,
Mar 23, 2005, 6:08:00 PM3/23/05
to

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

Wade Humeniuk

unread,
Mar 23, 2005, 6:16:15 PM3/23/05
to

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

rif

unread,
Mar 23, 2005, 6:30:26 PM3/23/05
to

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

Bulent Murtezaoglu

unread,
Mar 24, 2005, 1:55:23 AM3/24/05
to
>>>>> "rif" == rif <r...@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

0 new messages