bitstr.icn: Procedures for bits in Icon strings

link bitstr
August 14, 1996; Robert J. Alexander
See also: bitstrm.icn
This file is in the public domain.

Procedures for working with strings made up of numeric values
represented by strings of an arbitrary number of bits, stored without
regard to character boundaries.

In conjunction with the "large integers" feature of Icon, this
facility can deal with bitstring segments of arbitrary size.  If
"large integers" are not supported, bitstring segments (i.e.  the
nbits parameter of BitStringGet and BitStringPut) wider that the
integer size of the platform are likely to produce incorrect results.
____________________________________________________________

Usage of BitStringPut, by example:

     record bit_value(value, nbits)
     ...
     bitString := BitString("")
     while value := get_new_value() do       # loop to append to string
             BitStringPut(bitString, value.nbits, value.value)
     resultString := BitStringPut(bitString) # output any buffered bits

Note the interesting effect that BitStringPut(bitString), as well as
producing the complete string, pads the buffered string to an even
character boundary.  This can be dune during construction of a bit
string if the effect is desired.

The "value" argument defaults to zero.
____________________________________________________________

Usage of BitStringGet, by example:

     record bit_value(value, nbits)
     ...
     bitString := BitString(string_of_bits)
     while value := BitStringGet(bitString, nbits) do
             # do something with value

BitStringGet fails when too few bits remain to satisfy a request.
However, if bits remain in the string, subsequent calls with fewer
bits requested may succeed.  A negative "nbits" value gets the value
of the entire remainder of the string, to the byte boundary at its
end.

Source code | Program Library Page | Icon Home Page