Page 1 of 1

How to read in portions of a file as bit-vectors

Posted: Tue Apr 06, 2010 9:52 pm
by dented42
Hi,
I've been redoing all of my introductory CS assignments in LISP, and have arrived at Huffman compression. It seems to me that the most obvious way to deal with sequences of bits is to put them in a bit vector. This works nicely until I want to read or write to files. I have not been able to figure out how to read or write data from an file a bit vector. Ideally I would like to read the entire file into one large bit vector (or write a large bit vector into a file), but I realize that this is obviously unrealistic. So what I am trying to to is find a way to read small portions of the file at a time, something akin to the following:

Code: Select all

(defparameter *file* (open "foo"))
(read-bits *file* 6) ; read 6 bits
(read-bits *file* 3) ; read 3 bits
From what I have been able to discover, the only way to do this is to perform bit twiddling on integers using #'LDB and #'DPB, however that seems cumbersome.
Is what I want possible?

Re: How to read in portions of a file as bit-vectors

Posted: Tue Apr 06, 2010 10:10 pm
by nuntius
Here's a snippet from cl-jpeg.

Code: Select all

(with-open-file (in filename :direction :input :element-type 'unsigned-byte)
  (decode-stream in))
The code then uses read-byte to get data. Many other libraries use the same approach to binary data.

Re: How to read in portions of a file as bit-vectors

Posted: Tue Apr 06, 2010 10:20 pm
by dented42
Ok, but READ-BYTE returns an integer, is there a way to read int a bit vector of a specified length? or barring that, is there a way to convert from an integer (byte) to a bit vector?

Re: How to read in portions of a file as bit-vectors

Posted: Wed Apr 07, 2010 5:23 am
by gugamilare
dented42 wrote:Ok, but READ-BYTE returns an integer, is there a way to read int a bit vector of a specified length? or barring that, is there a way to convert from an integer (byte) to a bit vector?
You have to do it by hand, unless there is a library to do it for you. Odd-streams tries to do explicitly what you are trying to, you might try it.

This section of ANSI CL specification contains a bunch of functions that operate on integers as bits and bytes. For simplicity you may leave behind the operations starting with boole (they do the same as the ones starting with log, but with a strange interface).

Re: How to read in portions of a file as bit-vectors

Posted: Wed Apr 07, 2010 9:00 am
by dented42
Ok, thank you. I was hoping to be able to read and write bit-vectors because they are printed and read in a compact form, but that is of course just convenience.
In any case I think its time to dive back into the hyperspec! :geek: