The bitstring module provides four classes,
Bits is the simplest, and represents an immutable sequence of bits, while
BitArray adds various methods that modify the contents (these classes are intended to loosely mirror
bytearray). The ‘Stream’ classes have additional methods to treat the bits as a file or stream.
If you need to change the contents of a bitstring after creation then you must use either the
BitStream classes. If you need to use bitstrings as keys in a dictionary or members of a set then you must use either a
Bits or a
ConstBitStream. In this section the generic term ‘bitstring’ is used to refer to an object of any of these classes.
Note that for the bitstream classes the bit position within the bitstream (the position from which reads occur) can change without affecting the equality operation. This means that the
bytepos properties can change even for a
The public methods, special methods and properties of both classes are detailed in this section.
The auto initialiser¶
Note that in places where a bitstring can be used as a parameter, any other valid input to the
auto initialiser can also be used. This means that the parameter can also be a format string which consists of tokens:
hex=, or simply starting with
0ximplies hexadecimal. e.g.
oct=, or simply starting with
0oimplies octal. e.g.
bin=, or simply starting with
0bimplies binary. e.g.
uintfollowed by a length in bits and
=gives base-2 integers. e.g.
To get big, little and native-endian whole-byte integers append
nerespectively to the
For floating point numbers use
float:followed by the length in bits and
=and the number. The default is big-endian, but you can also append
neas with integers. e.g.
sie=implies an exponential-Golomb coded integer. e.g.
Multiples tokens can be joined by separating them with commas, so for example
se=4, 0b1, se=-1 represents the concatenation of three elements.
Parentheses and multiplicative factors can also be used, for example
2*(0b10, 0xf) is equivalent to
0b10, 0xf, 0b10, 0xf. The multiplying factor must come before the thing it is being used to repeat.
auto parameter also accepts other types:
A list or tuple, whose elements will be evaluated as booleans (imagine calling
bool()on each item) and the bits set to
A positive integer, used to create a bitstring of that many zero bits.
A file object, presumably opened in read-binary mode, from which the bitstring will be formed.
arrayobject. This is used after being converted to it’s constituent byte data via its
Compact format strings¶
peek methods and
pack function you can use compact format strings similar to those used in the
array modules. These start with an endian identifier:
> for big-endian,
< for little-endian or
@ for native-endian. This must be followed by at least one of these codes:
8 bit signed integer
8 bit unsigned integer
16 bit signed integer
16 bit unsigned integer
32 bit signed integer
32 bit unsigned integer
64 bit signed integer
64 bit unsigned integer
16 bit floating point number
32 bit floating point number
64 bit floating point number
For more detail see Compact format strings.
Bitstrings use a wide range of properties for getting and setting different interpretations on the binary data, as well as accessing bit lengths and positions. For the mutable
BitArray objects the properties are all read and write (with the exception of the
len), whereas for immutable objects the only write enabled properties are for the position in the bitstream (