Re: Bits from a stream



Zach Beane <xach@xxxxxxxx> writes:

"Alan Manuel K. Gloria" <almkglor@xxxxxxxxx> writes:

I'm currently trying to figure out how bits work in CL.

You can access the bits of an integer with functions like LDB and
LOGAND. You can produce integers with modified bits with functions
like DPB and LOGIOR. You can perform bitwise shifts with ASH. You can
test individual bits with LOGBITP or groups of bits with LOGTEST.

There are many more functions for accessing and operating on the bits
of an integer. See the numbers dictionary for a list:

http://www.lispworks.com/documentation/HyperSpec/Body/c_number.htm

Basically I need to do some compression on some data I'm storing - the
way I keep my data is rather amenable to compression, but compressing
it requires access to individual bits. I would like to store and
retrieve data from a file, so I will need access to bits in a stream.

I have written a few bitstream functions for a Flash file
reader/writer. They are available here:

http://www.xach.com/lisp/cl-flash/bitio.lisp

It will allow you to both read and write variable-length contiguous
bits across octet boundaries in an (UNSIGNED-BYTE 8) stream. There
isn't much to it.

You might also want to check out Chapter 24, "Parsing Binary Files" of
my book wherein I discuss how to build a library for parsing various
kinds of binary files.

<http://www.gigamonkeys.com/book/practical-parsing-binary-files.html>

-Peter

--
Peter Seibel * peter@xxxxxxxxxxxxxxx
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp * http://www.gigamonkeys.com/book/
.



Relevant Pages

  • Re: Getting literal newline in output after porting code
    ... and unrelated to any index items for Stream. ... binary files. ... I think that sort of thing would be allowed by the normative text in the Fortran standard as well / ftell() correspondence exists in whatever the Fortran equivalents are); the quoted paragraph is just an exemplary note. ...
    (comp.lang.fortran)
  • Re: fgets() and embedded null characters
    ... > pipe without throwing a premature EOF. ... Sure, because stdin is a text stream, not a binary stream. ... distinction between text and binary files. ...
    (comp.lang.c)
  • Re: Write position
    ... on write to a text stream. ... libraries fail to write a last, unterminated, line in a text file; ... that is NOT a breach of standard, but would be for binary files. ... This all applies to Fortran, too, but there is a lot more experience ...
    (comp.lang.fortran)
  • Re: Detecting binary verses text file streams
    ... > the underlying character code. ... For binary files you should use readand ... depending on whether the file was meant to be written in binary format ... I was hoping the mode of the stream would help me make this ...
    (comp.lang.cpp)
  • Re: How to determine how much TAPE-GB is required for DISK-GB
    ... >>Binary files are not highly compressible, and if you have ZIP files on ... you can actually see negative compression. ... >>the size might be if they are too small for quality compression... ... It is my understanding that single-pass compression used in tape drives ...
    (comp.os.vms)