bitstreams

Scott David Daniels Scott.Daniels at Acm.Org
Fri Jul 2 17:28:24 CEST 2004


Andreas Lobinger wrote:

> Aloha,
> can anyone here recommend a implementation for bitstreams?
> 
> For a project i need to extract bitfields from a file containing
> one large bitstream (lenght >> 1000bit). The bitfields (1bit-24bit)
> are not aligned to char boundaries (8bit).
> 
> Something like
> b1 = bitstream.fromfile('m1.bin')
> v1 = b1.getbits(startpos=3,length=10) # v ist a reg. int
> v2 = b1.getnext(8) # bits 0-7
> v3 = b1.getnext(10) # bits 8-17
> 
> Hoping for an answer and wishing a happy day
>         LOBI

You might start with something like the following as a primitive,
tweak it to deal correctly with your byte sex (big-endian vs. little 
endian), bit numbering, and such.  Then when that works, add a
current position and auto-advance stuff.  Then you can decide if you
want to go in bigger "bytes" by something like 'L' instead of 'B'
below.  Note that 1000 bits is not so very big that reading the file
into memory is a bad idea. In fact, I'd only think about that above
about a million bits.

     import array

     class Bitvector(object):
         def __init__(self, filename):
             datafile = file(filename, 'rb')
             self.data = array.array('B', datafile.read())
             datafile.close()
             self.elbits = self.data.itemsize * 8 # 8 bits/byte
         def grab(self, start, length):
             word, part = divmod(start, self.elbits)
             sofar = self.elbits - part
             result = self.data[word] & ((1L << sofar) - 1)
             # 1L above so no sign bit hassles below
             while sofar < length:
                 word += 1
                 result = (result << self.elbits) | self.data[word]
                 sofar += self.elbits
             return result >> sofar - length

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list