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

Discussion of Common Lisp
Post Reply
dented42
Posts: 3
Joined: Tue Apr 06, 2010 9:37 pm

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

Post by dented42 » Tue Apr 06, 2010 9:52 pm

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?

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

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

Post by nuntius » Tue Apr 06, 2010 10:10 pm

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.

dented42
Posts: 3
Joined: Tue Apr 06, 2010 9:37 pm

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

Post by dented42 » Tue Apr 06, 2010 10:20 pm

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?

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

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

Post by gugamilare » Wed Apr 07, 2010 5:23 am

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).

dented42
Posts: 3
Joined: Tue Apr 06, 2010 9:37 pm

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

Post by dented42 » Wed Apr 07, 2010 9:00 am

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:

Post Reply