From: Piet Wambacq
Subject: Re: integer --> bit-vector
Date: 
Message-ID: <CEJ7qo.KJo@gate.esat.kuleuven.ac.be>
In article <····················@godel.cs.orst.edu> ······@godel.cs.orst.edu
 (Peter Dudey, Order of the Golden Parentheses) writes 

Well, you can do some bit-twiddling on integers.  See p. 357 of
Steele's _Common Lisp_.

Unfortunately, no efficient conversion is provided. So the question remains :
given a bitvector, what is the corresponding integer ? I know that some solutions
have already been posted 
like
(defun bv2int(z)
    "convert a bitvector to a positive integer. e.g. #*101 -> 5"
    (reduce #'(lambda(x y) (+ (* 2 x) y)) z))

but they are much less efficient than a simple "cast" in C. Is there really no
more efficient solution ?
By the way, I use Lucid Common Lisp, perhaps there exists a Lucid extension to do
this ?

Piet Wambacq (············@esat.kuleuven.ac.be)

From: Kelly Murray
Subject: Re: integer --> bit-vector
Date: 
Message-ID: <291pv4INN427@no-names.nerdc.ufl.edu>
In article <··········@gate.esat.kuleuven.ac.be>, ········@cezanne.esat.kuleuven.ac.be (Piet Wambacq) writes:
|> In article <····················@godel.cs.orst.edu> ······@godel.cs.orst.edu
|>  (Peter Dudey, Order of the Golden Parentheses) writes 
|> 
|> Well, you can do some bit-twiddling on integers.  See p. 357 of
|> Steele's _Common Lisp_.
|> 
|> Unfortunately, no efficient conversion is provided. So the question remains :
|> given a bitvector, what is the corresponding integer ? I know that some solutions
|> have already been posted 
|> like
|> (defun bv2int(z)
|>     "convert a bitvector to a positive integer. e.g. #*101 -> 5"
|>     (reduce #'(lambda(x y) (+ (* 2 x) y)) z))
|> 
|> but they are much less efficient than a simple "cast" in C. Is there really no
|> more efficient solution ?
|> By the way, I use Lucid Common Lisp, perhaps there exists a Lucid extension to do
|> this ?
|> Piet Wambacq (············@esat.kuleuven.ac.be)

What do you consider efficient?  A bit-vector is a different object than an integer.
You can bit-twiddle integers themselves using LDB and friends (see CLtL).
This might be appropriate if the operation of converting from bit-vectors 
to integers is very common relative to manipulating them.
This may be faster than bitvectors if you know the integers will be fixnums,
which I'm guessing is what you are looking for: a C-like implementation.
But if the number of bits is >16 (or >30 in Lucid?), then the C-compatible view
won't work, and you'll need real bit-vectors (which become bignums when converted).

If these operations are of critical speed for your application, you might
check into writing Lucid-implementation-specific inline assembly language
to do the bit-twiddling.

-Kelly Murray
From: Christopher Hoover
Subject: Re: integer --> bit-vector
Date: 
Message-ID: <CELDHB.Ew0.2@cs.cmu.edu>
In article <··········@gate.esat.kuleuven.ac.be>,
Piet Wambacq <········@cezanne.esat.kuleuven.ac.be> wrote:
>In article <····················@godel.cs.orst.edu> ······@godel.cs.orst.edu
> (Peter Dudey, Order of the Golden Parentheses) writes 
>
>Well, you can do some bit-twiddling on integers.  See p. 357 of
>Steele's _Common Lisp_.
>
>Unfortunately, no efficient conversion is provided. So the question remains :
>given a bitvector, what is the corresponding integer ? I know that some solutions
>have already been posted 
>like
>(defun bv2int(z)
>    "convert a bitvector to a positive integer. e.g. #*101 -> 5"
>    (reduce #'(lambda(x y) (+ (* 2 x) y)) z))
>
>but they are much less efficient than a simple "cast" in C. Is there really no
>more efficient solution ?
>By the way, I use Lucid Common Lisp, perhaps there exists a Lucid extension to do
>this ?
>
>Piet Wambacq (············@esat.kuleuven.ac.be)

The type BIT-VECTOR is as unrelated to the type INTEGER as the type
ARRAY is unrelated to the type INTEGER.  To be precise BIT-VECTOR is
the same as (VECTOR BIT) aka (ARRAY BIT (*)).

There is no equivalent built-in C type akin to BIT-VECTOR.  The
closest thing to you will find are the bitmaps used by select(2) or
signal masks -- these (obviously) are not part of C proper but parts
of a library interface.  There is no automatic ``casting'' between
integers and these bitmaps -- you have to use explicit bit twiddling
which is fortunately typically hidden behind macros.

You can treat integers as vectors of bits with two's complement
encoding in Common Lisp just as you can in C.  The interface you
should use is LOG{IOR,XOR,AND,...} along with ASH, LDB, et al -- see
Section 12.7 ``Logical Operations on Numbers'' and 12.8 ``Byte
Manipulation Functions'' in CLtL2.  Note that all of this is
orthogonal to the BIT-VECTOR type and the functions that work on that
type.

Assuming you are dealing with fixnums and you can convince your
compiler of this (and your compiler is decent), you will likely get
the same machine code you would from your C compiler.  Otherwise
will likely wind up with slower, out-of-line operators.

-- Chris.
(··@lks.csi.com)