This module provides an object type which efficiently represents an array of booleans. Bitarrays are sequence types and behave very much like usual lists. Eight bits are represented by one byte in a contiguous block of memory. The user can select between two representations; little-endian and big-endian. Most of the functionality is implemented in C. Methods for accessing the machine representation are provided. This can be useful when bit level access to binary files is required, such as portable bitmap image files (.pbm). Also, when dealing with compressed data which uses variable bit length encoding, you may find this module useful.
- On 32bit systems, a bitarray object can contain up to 2^34 elements, that is 16 Gbits (on 64bit machines up to 2^63 elements in theory -- on Python 2.4 only 2^31 elements, see PEP 353 (added in Python 2.5)).
- All crucial functionality implemented in C.
- Bitarray objects behave very much like a list object, in particular slicing (including slice assignment and deletion) is supported.
- The bit endianness can be specified for each bitarray object, see below.
- Packing and unpacking to other binary data formats, e.g. numpy.ndarray, is possible.
- Fast methods for encoding and decoding variable bit length prefix codes
- Sequential search (as list or iterator)
- Bitwise operations: &, |, ^, &=, |=, ^=, ~
- Pickling and unpickling of bitarray objects possible.
- Bitarray objects support the buffer protocol (Python 2.7 only)
bitarray can be installed from source:
$ tar xzf bitarray-0.8.0.tar.gz $ cd bitarray-0.8.0 $ python setup.py install
On Unix systems, the latter command may have to be executed with root privileges. If you have distribute installed, you can easy_install bitarray. Once you have installed the package, you may want to test it:
$ python -c 'import bitarray; bitarray.test()' bitarray is installed in: /usr/local/lib/python2.7/site-packages/bitarray bitarray version: 0.8.0 2.7.2 (r271:86832, Nov 29 2010) [GCC 4.2.1 (SUSE Linux)] ......................................................................... ........................................... ---------------------------------------------------------------------- Ran 129 tests in 1.387s OK
You can always import the function test, and test().wasSuccessful() will return True when the test went well.
As mentioned above, bitarray objects behave very much like lists, so there is not too new to learn. The biggest difference to list objects is the ability to access the machine representation of the object. When doing so, the bit endianness is of importance, this issue is explained in detail in the section below. Here, we demonstrate the basic usage of bitarray objects:
>>> from bitarray import bitarray >>> a = bitarray() # create empty bitarray >>> a.append(True) >>> a.extend([False, True, True]) >>> a bitarray('1011')
Bitarray objects can be instantiated in different ways:
>>> a = bitarray(2**20) # bitarray of length 1048576 (uninitialized) >>> bitarray('1001011') # from a string bitarray('1001011') >>> lst = [True, False, False, True, False, True, True] >>> bitarray(lst) # from list, tuple, iterable bitarray('1001011')
Bits can be assigned from any Python object, if the value can be interpreted as a truth value. You can think of this as Python's built-in function bool() being applied, whenever casting an object:
>>> a = bitarray([42, '', True, {}, 'foo', None]) >>> a bitarray('101010') >>> a.append(a) # note that bool(a) is True >>> a.count(42) # counts occurrences of True (not 42) 4L >>> a.remove('') # removes first occurrence of False >>> a bitarray('110101')
Like lists, bitarray objects support slice assignment and deletion:
>>> a = bitarray(50) >>> a.setall(False) >>> a[11:37:3] = 9 * bitarray([True]) >>> a bitarray('00000000000100100100100100100100100100000000000000') >>> del a[12::3] >>> a bitarray('0000000000010101010101010101000000000') >>> a[-6:] = bitarray('10011') >>> a bitarray('000000000001010101010101010100010011') >>> a += bitarray('000111') >>> a[9:] bitarray('001010101010101010100010011000111')
In addition, slices can be assigned to booleans, which is easier (and faster) than assigning to a bitarray in which all values are the same:
>>> a = 20 * bitarray('0') >>> a[1:15:3] = True >>> a bitarray('01001001001001000000')
This is easier and faster than:
>>> a = 20 * bitarray('0') >>> a[1:15:3] = 5 * bitarray('1') >>> a bitarray('01001001001001000000')
Note that in the latter we have to create a temporary bitarray whose length must be known or calculated.
Since a bitarray allows addressing of individual bits, where the machine represents 8 bits in one byte, there two obvious choices for this mapping; little- and big-endian. When creating a new bitarray object, the endianness can always be specified explicitly:
>>> a = bitarray(endian='little') >>> a.frombytes(b'A') >>> a bitarray('10000010') >>> b = bitarray('11000010', endian='little') >>> b.tobytes() 'C'
Here, the low-bit comes first because little-endian means that increasing numeric significance corresponds to an increasing address (index). So a[0] is the lowest and least significant bit, and a[7] is the highest and most significant bit.
>>> a = bitarray(endian='big') >>> a.frombytes(b'A') >>> a bitarray('01000001') >>> a[6] = 1 >>> a.tobytes() 'C'
Here, the high-bit comes first because big-endian means "most-significant first". So a[0] is now the lowest and most significant bit, and a[7] is the highest and least significant bit.
The bit endianness is a property attached to each bitarray object. When comparing bitarray objects, the endianness (and hence the machine representation) is irrelevant; what matters is the mapping from indices to bits:
>>> bitarray('11001', endian='big') == bitarray('11001', endian='little') True
Bitwise operations (&, |, ^, &=, |=, ^=, ~) are implemented efficiently using the corresponding byte operations in C, i.e. the operators act on the machine representation of the bitarray objects. Therefore, one has to be cautious when applying the operation to bitarrays with different endianness.
When converting to and from machine representation, using the tobytes, frombytes, tofile and fromfile methods, the endianness matters:
>>> a = bitarray(endian='little') >>> a.frombytes(b'\x01') >>> a bitarray('10000000') >>> b = bitarray(endian='big') >>> b.frombytes(b'\x80') >>> b bitarray('10000000') >>> a == b True >>> a.tobytes() == b.tobytes() False
The endianness can not be changed once an object is created. However, since creating a bitarray from another bitarray just copies the memory representing the data, you can create a new bitarray with different endianness:
>>> a = bitarray('11100000', endian='little') >>> a bitarray('11100000') >>> b = bitarray(a, endian='big') >>> b bitarray('00000111') >>> a == b False >>> a.tobytes() == b.tobytes() True
The default bit endianness is currently big-endian, however this may change in the future, and when dealing with the machine representation of bitarray objects, it is recommended to always explicitly specify the endianness.
Unless, explicitly converting to machine representation, using the tobytes, frombytes, tofile and fromfile methods, the bit endianness will have no effect on any computation, and one can safely ignore setting the endianness, and other details of this section.
Python 2.7 provides memoryview objects, which allow Python code to access the internal data of an object that supports the buffer protocol without copying. Bitarray objects support this protocol, with the memory being interpreted as simple bytes.
>>> a = bitarray('01000001' '01000010' '01000011', endian='big') >>> v = memoryview(a) >>> len(v) 3 >>> v[-1] 'C' >>> v[:2].tobytes() 'AB' >>> v.readonly # changing a bitarray's memory is also possible False >>> v[1] = 'o' >>> a bitarray('010000010110111101000011')
The method encode takes a dictionary mapping symbols to bitarrays and an iterable, and extends the bitarray object with the encoded symbols found while iterating. For example:
>>> d = {'H':bitarray('111'), 'e':bitarray('0'), ... 'l':bitarray('110'), 'o':bitarray('10')} ... >>> a = bitarray() >>> a.encode(d, 'Hello') >>> a bitarray('111011011010')
Note that the string 'Hello' is an iterable, but the symbols are not limited to characters, any hashable Python object can be a symbol. Taking the same dictionary, we can apply the decode method which will return a list of the symbols:
>>> a.decode(d) ['H', 'e', 'l', 'l', 'o'] >>> ''.join(a.decode(d)) 'Hello'
Since symbols are not limited to being characters, it is necessary to return them as elements of a list, rather than simply returning the joined string.
The bitarray class:
Return a new bitarray object whose items are bits initialized from the optional initial, and endianness. If no object is provided, the bitarray is initialized to have length zero. The initial object may be of the following types:
The optional keyword arguments 'endian' specifies the bit endianness of the created bitarray object. Allowed values are 'big' and 'little' (default is 'big').
Note that setting the bit endianness only has an effect when accessing the machine representation of the bitarray, i.e. when using the methods: tofile, fromfile, tobytes, frombytes.
A bitarray object supports the following methods:
Functions defined in the module:
0.8.0 (2012-04-04):
- add Python 2.4 support
- add (module level) function bitdiff for calculating the difference between two bitarrays
0.7.0 (2012-02-15):
- add iterdecode method (C level), which returns an iterator but is otherwise like the decode method
- improve memory efficiency and speed of pickling large bitarray objects
0.6.0 (2012-02-06):
- add buffer protocol to bitarray objects (Python 2.7 only)
- allow slice assignment to 0 or 1, e.g. a[::3] = 0 (in addition to booleans)
- moved implementation of itersearch method to C level (Lluis Pamies)
- search, itersearch now only except bitarray objects, whereas __contains__ excepts either booleans or bitarrays
- use a priority queue for Huffman tree example (thanks to Ushma Bhatt)
- improve documentation
Please find the complete change log here.