Write bits in file

Andrew Lee fiacre.patrick at gmail.com
Fri May 23 03:00:55 EDT 2008


Tim Roberts wrote:
> Monica Leko <monica.leko at gmail.com> wrote:
>> I have a specific format and I need binary representation.  Does
>> Python have some built-in function which will, for instance, represent
>> number 15 in exactly 10 bits?
> 
> For the record, I'd like to point out that even C cannot do this.  You need
> to use shifting and masking to produce a stream of 8-bit bytes, or to
> extract your values from a stream of 8-bit bytes.


Hmmmm, bitfields are exactly non-aligned bits in a structure and are 
part of ANSI C.  Although C gives no guarantee of the ordering of fields 
within machine words ... so bitfieds are of limited use in portable 
programs unless they are 0-initialized.

IIRC, Huffman code uses arbitrary length bit strings and is the basis of 
many compression algorithms.







More information about the Python-list mailing list