Belle II Software  release-05-02-19
Bits Class Reference
Inheritance diagram for Bits:
Collaboration diagram for Bits:

Public Member Functions

def __init__ (self, auto=None, length=None, offset=None, **kwargs)
 
def __new__ (cls, auto=None, length=None, offset=None, _cache={}, **kwargs)
 
def __copy__ (self)
 
def __lt__ (self, other)
 
def __gt__ (self, other)
 
def __le__ (self, other)
 
def __ge__ (self, other)
 
def __add__ (self, bs)
 
def __radd__ (self, bs)
 
def __getitem__ (self, key)
 
def __len__ (self)
 
def __str__ (self)
 
def __repr__ (self)
 
def __eq__ (self, bs)
 
def __ne__ (self, bs)
 
def __invert__ (self)
 
def __lshift__ (self, n)
 
def __rshift__ (self, n)
 
def __mul__ (self, n)
 
def __rmul__ (self, n)
 
def __and__ (self, bs)
 
def __rand__ (self, bs)
 
def __or__ (self, bs)
 
def __ror__ (self, bs)
 
def __xor__ (self, bs)
 
def __rxor__ (self, bs)
 
def __contains__ (self, bs)
 
def __hash__ (self)
 
def __nonzero__ (self)
 
def unpack (self, fmt, **kwargs)
 
def find (self, bs, start=None, end=None, bytealigned=None)
 
def findall (self, bs, start=None, end=None, count=None, bytealigned=None)
 
def rfind (self, bs, start=None, end=None, bytealigned=None)
 
def cut (self, bits, start=None, end=None, count=None)
 
def split (self, delimiter, start=None, end=None, count=None, bytealigned=None)
 
def join (self, sequence)
 
def tobytes (self)
 
def tofile (self, f)
 
def startswith (self, prefix, start=None, end=None)
 
def endswith (self, suffix, start=None, end=None)
 
def all (self, value, pos=None)
 
def any (self, value, pos=None)
 
def count (self, value)
 

Properties

 _offset = property(_getoffset)
 
 len
 
 length
 
 bool
 
 hex
 
 bin
 
 oct
 
 bytes
 
 int
 
 uint
 
 float
 
 intbe
 
 uintbe
 
 floatbe
 
 intle
 
 uintle
 
 floatle
 
 intne
 
 uintne
 
 floatne
 
 ue
 
 se
 
 uie
 
 sie
 

Private Member Functions

def _initialise (self, auto, length, offset, **kwargs)
 
def _initialise_from_auto (self, auto, length, offset)
 
def _assertsanity (self)
 
def _init_with_token (cls, name, token_length, value)
 
def _clear (self)
 
def _setauto (self, s, length, offset)
 
def _setfile (self, filename, length, offset)
 
def _setbytes_safe (self, data, length=None, offset=0)
 
def _setbytes_unsafe (self, data, length, offset)
 
def _readbytes (self, length, start)
 
def _getbytes (self)
 
def _setuint (self, uint, length=None)
 
def _readuint (self, length, start)
 
def _getuint (self)
 
def _setint (self, int_, length=None)
 
def _readint (self, length, start)
 
def _getint (self)
 
def _setuintbe (self, uintbe, length=None)
 
def _readuintbe (self, length, start)
 
def _getuintbe (self)
 
def _setintbe (self, intbe, length=None)
 
def _readintbe (self, length, start)
 
def _getintbe (self)
 
def _setuintle (self, uintle, length=None)
 
def _readuintle (self, length, start)
 
def _getuintle (self)
 
def _setintle (self, intle, length=None)
 
def _readintle (self, length, start)
 
def _getintle (self)
 
def _setfloat (self, f, length=None)
 
def _readfloat (self, length, start)
 
def _getfloat (self)
 
def _setfloatle (self, f, length=None)
 
def _readfloatle (self, length, start)
 
def _getfloatle (self)
 
def _setue (self, i)
 
def _readue (self, pos)
 
def _getue (self)
 
def _setse (self, i)
 
def _getse (self)
 
def _readse (self, pos)
 
def _setuie (self, i)
 
def _readuie (self, pos)
 
def _getuie (self)
 
def _setsie (self, i)
 
def _getsie (self)
 
def _readsie (self, pos)
 
def _setbool (self, value)
 
def _getbool (self)
 
def _readbool (self, pos)
 
def _setbin_safe (self, binstring)
 
def _setbin_unsafe (self, binstring)
 
def _readbin (self, length, start)
 
def _getbin (self)
 
def _setoct (self, octstring)
 
def _readoct (self, length, start)
 
def _getoct (self)
 
def _sethex (self, hexstring)
 
def _readhex (self, length, start)
 
def _gethex (self)
 
def _getoffset (self)
 
def _getlength (self)
 
def _ensureinmemory (self)
 
def _converttobitstring (cls, bs, offset=0, cache={})
 
def _copy (self)
 
def _slice (self, start, end)
 
def _readtoken (self, name, pos, length)
 
def _append (self, bs)
 
def _prepend (self, bs)
 
def _reverse (self)
 
def _truncatestart (self, bits)
 
def _truncateend (self, bits)
 
def _insert (self, bs, pos)
 
def _overwrite (self, bs, pos)
 
def _delete (self, bits, pos)
 
def _reversebytes (self, start, end)
 
def _set (self, pos)
 
def _unset (self, pos)
 
def _invert (self, pos)
 
def _invert_all (self)
 
def _ilshift (self, n)
 
def _irshift (self, n)
 
def _imul (self, n)
 
def _inplace_logical_helper (self, bs, f)
 
def _ior (self, bs)
 
def _iand (self, bs)
 
def _ixor (self, bs)
 
def _readbits (self, length, start)
 
def _validate_slice (self, start, end)
 
def _readlist (self, fmt, pos, **kwargs)
 
def _findbytes (self, bytes_, start, end, bytealigned)
 
def _findregex (self, reg_ex, start, end, bytealigned)
 

Private Attributes

 _pos
 
 _datastore
 

Static Private Attributes

tuple __slots__ = ('_datastore')
 
def __bool__ = __nonzero__
 
def _setfloatne = _setfloatle
 
def _readfloatne = _readfloatle
 
def _getfloatne = _getfloatle
 
def _setuintne = _setuintle
 
def _readuintne = _readuintle
 
def _getuintne = _getuintle
 
def _setintne = _setintle
 
def _readintne = _readintle
 
def _getintne = _getintle
 

Detailed Description

A container holding an immutable sequence of bits.

For a mutable container use the BitArray class instead.

Methods:

all() -- Check if all specified bits are set to 1 or 0.
any() -- Check if any of specified bits are set to 1 or 0.
count() -- Count the number of bits set to 1 or 0.
cut() -- Create generator of constant sized chunks.
endswith() -- Return whether the bitstring ends with a sub-string.
find() -- Find a sub-bitstring in the current bitstring.
findall() -- Find all occurrences of a sub-bitstring in the current bitstring.
join() -- Join bitstrings together using current bitstring.
rfind() -- Seek backwards to find a sub-bitstring.
split() -- Create generator of chunks split by a delimiter.
startswith() -- Return whether the bitstring starts with a sub-bitstring.
tobytes() -- Return bitstring as bytes, padding if needed.
tofile() -- Write bitstring to file, padding if needed.
unpack() -- Interpret bits using format string.

Special methods:

Also available are the operators [], ==, !=, +, *, ~, <<, >>, &, |, ^.

Properties:

bin -- The bitstring as a binary string.
bool -- For single bit bitstrings, interpret as True or False.
bytes -- The bitstring as a bytes object.
float -- Interpret as a floating point number.
floatbe -- Interpret as a big-endian floating point number.
floatle -- Interpret as a little-endian floating point number.
floatne -- Interpret as a native-endian floating point number.
hex -- The bitstring as a hexadecimal string.
int -- Interpret as a two's complement signed integer.
intbe -- Interpret as a big-endian signed integer.
intle -- Interpret as a little-endian signed integer.
intne -- Interpret as a native-endian signed integer.
len -- Length of the bitstring in bits.
oct -- The bitstring as an octal string.
se -- Interpret as a signed exponential-Golomb code.
ue -- Interpret as an unsigned exponential-Golomb code.
sie -- Interpret as a signed interleaved exponential-Golomb code.
uie -- Interpret as an unsigned interleaved exponential-Golomb code.
uint -- Interpret as a two's complement unsigned integer.
uintbe -- Interpret as a big-endian unsigned integer.
uintle -- Interpret as a little-endian unsigned integer.
uintne -- Interpret as a native-endian unsigned integer.

Definition at line 684 of file bitstring.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  auto = None,
  length = None,
  offset = None,
**  kwargs 
)
Either specify an 'auto' initialiser:
auto -- a string of comma separated tokens, an integer, a file object,
a bytearray, a boolean iterable, an array or another bitstring.

Or initialise via **kwargs with one (and only one) of:
bytes -- raw data as a string, for example read from a binary file.
bin -- binary string representation, e.g. '0b001010'.
hex -- hexadecimal string representation, e.g. '0x2ef'
oct -- octal string representation, e.g. '0o777'.
uint -- an unsigned integer.
int -- a signed integer.
float -- a floating point number.
uintbe -- an unsigned big-endian whole byte integer.
intbe -- a signed big-endian whole byte integer.
floatbe - a big-endian floating point number.
uintle -- an unsigned little-endian whole byte integer.
intle -- a signed little-endian whole byte integer.
floatle -- a little-endian floating point number.
uintne -- an unsigned native-endian whole byte integer.
intne -- a signed native-endian whole byte integer.
floatne -- a native-endian floating point number.
se -- a signed exponential-Golomb code.
ue -- an unsigned exponential-Golomb code.
sie -- a signed interleaved exponential-Golomb code.
uie -- an unsigned interleaved exponential-Golomb code.
bool -- a boolean (True or False).
filename -- a file which will be opened in binary read-only mode.

Other keyword arguments:
length -- length of the bitstring in bits, if needed and appropriate.
  It must be supplied for all integer and float initialisers.
offset -- bit offset to the data. These offset bits are
  ignored and this is mainly intended for use when
  initialising using 'bytes' or 'filename'.

Reimplemented in BitStream, ConstBitStream, and BitArray.

Definition at line 739 of file bitstring.py.

Member Function Documentation

◆ __add__()

def __add__ (   self,
  bs 
)
Concatenate bitstrings and return new bitstring.

bs -- the bitstring to append.

Reimplemented in ConstBitStream.

Definition at line 864 of file bitstring.py.

◆ __and__()

def __and__ (   self,
  bs 
)
Bit-wise 'and' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '&' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1075 of file bitstring.py.

◆ __contains__()

def __contains__ (   self,
  bs 
)
Return whether bs is contained in the current bitstring.

bs -- The bitstring to search for.

Definition at line 1153 of file bitstring.py.

◆ __copy__()

def __copy__ (   self)
Return a new copy of the Bits for the copy module.

Reimplemented in BitStream, ConstBitStream, and BitArray.

Definition at line 846 of file bitstring.py.

◆ __eq__()

def __eq__ (   self,
  bs 
)
Return True if two bitstrings have the same binary representation.

>>> BitArray('0b1110') == '0xe'
True

Definition at line 986 of file bitstring.py.

◆ __getitem__()

def __getitem__ (   self,
  key 
)
Return a new bitstring representing a slice of the current bitstring.

Indices are in units of the step parameter (default 1 bit).
Stepping is used to specify the number of bits in each item.

>>> print BitArray('0b00110')[1:4]
'0b011'
>>> print BitArray('0x00112233')[1:3:8]
'0x1122'

Definition at line 889 of file bitstring.py.

◆ __hash__()

def __hash__ (   self)
Return an integer hash of the object.

Definition at line 1171 of file bitstring.py.

◆ __invert__()

def __invert__ (   self)
Return bitstring with every bit inverted.

Raises Error if the bitstring is empty.

Definition at line 1008 of file bitstring.py.

◆ __len__()

def __len__ (   self)
Return the length of the bitstring in bits.

Definition at line 934 of file bitstring.py.

◆ __lshift__()

def __lshift__ (   self,
  n 
)
Return bitstring with bits shifted by n to the left.

n -- the number of bits to shift. Must be >= 0.

Definition at line 1020 of file bitstring.py.

◆ __mul__()

def __mul__ (   self,
  n 
)
Return bitstring consisting of n concatenations of self.

Called for expression of the form 'a = b*3'.
n -- The number of concatenations. Must be >= 0.

Definition at line 1051 of file bitstring.py.

◆ __ne__()

def __ne__ (   self,
  bs 
)
Return False if two bitstrings have the same binary representation.

>>> BitArray('0b111') == '0x7'
False

Definition at line 999 of file bitstring.py.

◆ __nonzero__()

def __nonzero__ (   self)
Return True if any bits are set to 1, otherwise return False.

Definition at line 1195 of file bitstring.py.

◆ __or__()

def __or__ (   self,
  bs 
)
Bit-wise 'or' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '|' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1101 of file bitstring.py.

◆ __radd__()

def __radd__ (   self,
  bs 
)
Append current bitstring to bs and return new bitstring.

bs -- the string for the 'auto' initialiser that will be appended to.

Definition at line 880 of file bitstring.py.

◆ __rand__()

def __rand__ (   self,
  bs 
)
Bit-wise 'and' between two bitstrings. Returns new bitstring.

bs -- the bitstring to '&' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1091 of file bitstring.py.

◆ __repr__()

def __repr__ (   self)
Return representation that could be used to recreate the bitstring.

If the returned string is too long it will be truncated. See __str__().

Definition at line 965 of file bitstring.py.

◆ __rmul__()

def __rmul__ (   self,
  n 
)
Return bitstring consisting of n concatenations of self.

Called for expressions of the form 'a = 3*b'.
n -- The number of concatenations. Must be >= 0.

Definition at line 1066 of file bitstring.py.

◆ __ror__()

def __ror__ (   self,
  bs 
)
Bit-wise 'or' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '|' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1117 of file bitstring.py.

◆ __rshift__()

def __rshift__ (   self,
  n 
)
Return bitstring with bits shifted by n to the right.

n -- the number of bits to shift. Must be >= 0.

Definition at line 1035 of file bitstring.py.

◆ __rxor__()

def __rxor__ (   self,
  bs 
)
Bit-wise 'xor' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '^' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1143 of file bitstring.py.

◆ __str__()

def __str__ (   self)
Return approximate string representation of bitstring for printing.

Short strings will be given wholly in hexadecimal or binary. Longer
strings may be part hexadecimal and part binary. Very long strings will
be truncated with '...'.

Definition at line 938 of file bitstring.py.

◆ __xor__()

def __xor__ (   self,
  bs 
)
Bit-wise 'xor' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '^' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1127 of file bitstring.py.

◆ _append()

def _append (   self,
  bs 
)
private
Append a bitstring to the current bitstring.

Definition at line 2029 of file bitstring.py.

◆ _assertsanity()

def _assertsanity (   self)
private
Check internal self consistency as a debugging aid.

Definition at line 1202 of file bitstring.py.

◆ _clear()

def _clear (   self)
private
Reset the bitstring to an empty state.

Reimplemented in ConstBitStream.

Definition at line 1248 of file bitstring.py.

◆ _converttobitstring()

def _converttobitstring (   cls,
  bs,
  offset = 0,
  cache = {} 
)
private
Convert bs to a bitstring and return it.

offset gives the suggested bit offset of first significant
bit, to optimise append etc.

Definition at line 1962 of file bitstring.py.

◆ _copy()

def _copy (   self)
private
Create and return a new copy of the Bits (always in memory).

Definition at line 1995 of file bitstring.py.

◆ _delete()

def _delete (   self,
  bits,
  pos 
)
private
Delete bits at pos.

Definition at line 2129 of file bitstring.py.

◆ _ensureinmemory()

def _ensureinmemory (   self)
private
Ensure the data is held in memory, not in a file.

Definition at line 1956 of file bitstring.py.

◆ _findbytes()

def _findbytes (   self,
  bytes_,
  start,
  end,
  bytealigned 
)
private
Quicker version of find when everything's whole byte
and byte aligned.

Definition at line 2359 of file bitstring.py.

◆ _findregex()

def _findregex (   self,
  reg_ex,
  start,
  end,
  bytealigned 
)
private
Find first occurrence of a compiled regular expression.

Note that this doesn't support arbitrary regexes, in particular they
must match a known length.

Definition at line 2386 of file bitstring.py.

◆ _getbin()

def _getbin (   self)
private
Return interpretation as a binary string.

Definition at line 1868 of file bitstring.py.

◆ _getbytes()

def _getbytes (   self)
private
Return the data as an ordinary string.

Definition at line 1351 of file bitstring.py.

◆ _getfloat()

def _getfloat (   self)
private
Interpret the whole bitstring as a float.

Definition at line 1582 of file bitstring.py.

◆ _getfloatle()

def _getfloatle (   self)
private
Interpret the whole bitstring as a little-endian float.

Definition at line 1621 of file bitstring.py.

◆ _gethex()

def _gethex (   self)
private
Return the hexadecimal representation as a string prefixed with '0x'.

Raises an InterpretError if the bitstring's length is not a multiple of 4.

Definition at line 1941 of file bitstring.py.

◆ _getint()

def _getint (   self)
private
Return data as a two's complement signed int.

Definition at line 1450 of file bitstring.py.

◆ _getintbe()

def _getintbe (   self)
private
Return data as a big-endian two's complement signed int.

Definition at line 1486 of file bitstring.py.

◆ _getlength()

def _getlength (   self)
private
Return the length of the bitstring in bits.

Definition at line 1952 of file bitstring.py.

◆ _getoct()

def _getoct (   self)
private
Return interpretation as an octal string.

Definition at line 1902 of file bitstring.py.

◆ _getse()

def _getse (   self)
private
Return data as signed exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1694 of file bitstring.py.

◆ _getsie()

def _getsie (   self)
private
Return data as signed interleaved exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1777 of file bitstring.py.

◆ _getue()

def _getue (   self)
private
Return data as unsigned exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1672 of file bitstring.py.

◆ _getuie()

def _getuie (   self)
private
Return data as unsigned interleaved exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1755 of file bitstring.py.

◆ _getuint()

def _getuint (   self)
private
Return data as an unsigned int.

Definition at line 1413 of file bitstring.py.

◆ _getuintbe()

def _getuintbe (   self)
private
Return data as a big-endian two's complement unsigned int.

Definition at line 1468 of file bitstring.py.

◆ _ilshift()

def _ilshift (   self,
  n 
)
private
Shift bits by n to the left in place. Return self.

Definition at line 2190 of file bitstring.py.

◆ _imul()

def _imul (   self,
  n 
)
private
Concatenate n copies of self in place. Return self.

Definition at line 2204 of file bitstring.py.

◆ _inplace_logical_helper()

def _inplace_logical_helper (   self,
  bs,
  f 
)
private
Helper function containing most of the __ior__, __iand__, __ixor__ code.

Definition at line 2218 of file bitstring.py.

◆ _insert()

def _insert (   self,
  bs,
  pos 
)
private
Insert bs at pos.

Definition at line 2075 of file bitstring.py.

◆ _invert()

def _invert (   self,
  pos 
)
private
Flip bit at pos 1<->0.

Definition at line 2178 of file bitstring.py.

◆ _invert_all()

def _invert_all (   self)
private
Invert every bit.

Definition at line 2183 of file bitstring.py.

◆ _irshift()

def _irshift (   self,
  n 
)
private
Shift bits by n to the right in place. Return self.

Definition at line 2197 of file bitstring.py.

◆ _overwrite()

def _overwrite (   self,
  bs,
  pos 
)
private
Overwrite with bs at pos.

Definition at line 2096 of file bitstring.py.

◆ _prepend()

def _prepend (   self,
  bs 
)
private
Prepend a bitstring to the current bitstring.

Definition at line 2033 of file bitstring.py.

◆ _readbin()

def _readbin (   self,
  length,
  start 
)
private
Read bits and interpret as a binary string.

Definition at line 1851 of file bitstring.py.

◆ _readbits()

def _readbits (   self,
  length,
  start 
)
private
Read some bits from the bitstring and return newly constructed bitstring.

Definition at line 2243 of file bitstring.py.

◆ _readbytes()

def _readbytes (   self,
  length,
  start 
)
private
Read bytes and return them. Note that length is in bits.

Definition at line 1342 of file bitstring.py.

◆ _readfloat()

def _readfloat (   self,
  length,
  start 
)
private
Read bits and interpret as a float.

Definition at line 1564 of file bitstring.py.

◆ _readfloatle()

def _readfloatle (   self,
  length,
  start 
)
private
Read bits and interpret as a little-endian float.

Definition at line 1602 of file bitstring.py.

◆ _readhex()

def _readhex (   self,
  length,
  start 
)
private
Read bits and interpret as a hex string.

Definition at line 1924 of file bitstring.py.

◆ _readint()

def _readint (   self,
  length,
  start 
)
private
Read bits and interpret as a signed int

Definition at line 1440 of file bitstring.py.

◆ _readintbe()

def _readintbe (   self,
  length,
  start 
)
private
Read bits and interpret as a big-endian signed int.

Definition at line 1479 of file bitstring.py.

◆ _readintle()

def _readintle (   self,
  length,
  start 
)
private
Read bits and interpret as a little-endian signed int.

Definition at line 1535 of file bitstring.py.

◆ _readoct()

def _readoct (   self,
  length,
  start 
)
private
Read bits and interpret as an octal string.

Definition at line 1887 of file bitstring.py.

◆ _readse()

def _readse (   self,
  pos 
)
private
Return interpretation of next bits as a signed exponential-Golomb code.

Advances position to after the read code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1708 of file bitstring.py.

◆ _readsie()

def _readsie (   self,
  pos 
)
private
Return interpretation of next bits as a signed interleaved exponential-Golomb code.

Advances position to after the read code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1791 of file bitstring.py.

◆ _readtoken()

def _readtoken (   self,
  name,
  pos,
  length 
)
private
Reads a token from the bitstring and returns the result.

Definition at line 2013 of file bitstring.py.

◆ _readue()

def _readue (   self,
  pos 
)
private
Return interpretation of next bits as unsigned exponential-Golomb code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1647 of file bitstring.py.

◆ _readuie()

def _readuie (   self,
  pos 
)
private
Return interpretation of next bits as unsigned interleaved exponential-Golomb code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1735 of file bitstring.py.

◆ _readuint()

def _readuint (   self,
  length,
  start 
)
private
Read bits and interpret as an unsigned int.

Definition at line 1395 of file bitstring.py.

◆ _readuintbe()

def _readuintbe (   self,
  length,
  start 
)
private
Read bits and interpret as a big-endian unsigned int.

Definition at line 1461 of file bitstring.py.

◆ _readuintle()

def _readuintle (   self,
  length,
  start 
)
private
Read bits and interpret as a little-endian unsigned int.

Definition at line 1497 of file bitstring.py.

◆ _reverse()

def _reverse (   self)
private
Reverse all bits in-place.

Definition at line 2037 of file bitstring.py.

◆ _reversebytes()

def _reversebytes (   self,
  start,
  end 
)
private
Reverse bytes in-place.

Definition at line 2155 of file bitstring.py.

◆ _set()

def _set (   self,
  pos 
)
private
Set bit at pos to 1.

Definition at line 2168 of file bitstring.py.

◆ _setauto()

def _setauto (   self,
  s,
  length,
  offset 
)
private
Set bitstring from a bitstring, file, bool, integer, array, iterable or string.

Definition at line 1252 of file bitstring.py.

◆ _setbin_safe()

def _setbin_safe (   self,
  binstring 
)
private
Reset the bitstring to the value given in binstring.

Definition at line 1830 of file bitstring.py.

◆ _setbin_unsafe()

def _setbin_unsafe (   self,
  binstring 
)
private
Same as _setbin_safe, but input isn't sanity checked. binstring mustn't start with '0b'.

Definition at line 1837 of file bitstring.py.

◆ _setbytes_safe()

def _setbytes_safe (   self,
  data,
  length = None,
  offset = 0 
)
private
Set the data from a string.

Definition at line 1321 of file bitstring.py.

◆ _setbytes_unsafe()

def _setbytes_unsafe (   self,
  data,
  length,
  offset 
)
private
Unchecked version of _setbytes_safe.

Definition at line 1337 of file bitstring.py.

◆ _setfile()

def _setfile (   self,
  filename,
  length,
  offset 
)
private
Use file as source of bits.

Definition at line 1306 of file bitstring.py.

◆ _sethex()

def _sethex (   self,
  hexstring 
)
private
Reset the bitstring to have the value given in hexstring.

Definition at line 1906 of file bitstring.py.

◆ _setint()

def _setint (   self,
  int_,
  length = None 
)
private
Reset the bitstring to have given signed int interpretation.

Definition at line 1417 of file bitstring.py.

◆ _setintbe()

def _setintbe (   self,
  intbe,
  length = None 
)
private
Set bitstring to a big-endian signed int interpretation.

Definition at line 1472 of file bitstring.py.

◆ _setoct()

def _setoct (   self,
  octstring 
)
private
Reset the bitstring to have the value given in octstring.

Definition at line 1872 of file bitstring.py.

◆ _setse()

def _setse (   self,
  i 
)
private
Initialise bitstring with signed exponential-Golomb code for integer i.

Definition at line 1686 of file bitstring.py.

◆ _setsie()

def _setsie (   self,
  i 
)
private
Initialise bitstring with signed interleaved exponential-Golomb code for integer i.

Definition at line 1769 of file bitstring.py.

◆ _setue()

def _setue (   self,
  i 
)
private
Initialise bitstring with unsigned exponential-Golomb code for integer i.

Raises CreationError if i < 0.

Definition at line 1625 of file bitstring.py.

◆ _setuie()

def _setuie (   self,
  i 
)
private
Initialise bitstring with unsigned interleaved exponential-Golomb code for integer i.

Raises CreationError if i < 0.

Definition at line 1724 of file bitstring.py.

◆ _setuint()

def _setuint (   self,
  uint,
  length = None 
)
private
Reset the bitstring to have given unsigned int interpretation.

Definition at line 1358 of file bitstring.py.

◆ _setuintbe()

def _setuintbe (   self,
  uintbe,
  length = None 
)
private
Set the bitstring to a big-endian unsigned int interpretation.

Definition at line 1454 of file bitstring.py.

◆ _slice()

def _slice (   self,
  start,
  end 
)
private
Used internally to get a slice, without error checking.

Definition at line 2002 of file bitstring.py.

◆ _truncateend()

def _truncateend (   self,
  bits 
)
private
Truncate bits from the end of the bitstring.

Definition at line 2062 of file bitstring.py.

◆ _truncatestart()

def _truncatestart (   self,
  bits 
)
private
Truncate bits from the start of the bitstring.

Definition at line 2049 of file bitstring.py.

◆ _unset()

def _unset (   self,
  pos 
)
private
Set bit at pos to 0.

Definition at line 2173 of file bitstring.py.

◆ _validate_slice()

def _validate_slice (   self,
  start,
  end 
)
private
Validate start and end and return them as positive bit positions.

Definition at line 2247 of file bitstring.py.

◆ all()

def all (   self,
  value,
  pos = None 
)
Return True if one or many bits are all set to value.

value -- If value is True then checks for bits set to 1, otherwise
 checks for bits set to 0.
pos -- An iterable of bit positions. Negative numbers are treated in
       the same way as slice indices. Defaults to the whole bitstring.

Definition at line 2726 of file bitstring.py.

◆ any()

def any (   self,
  value,
  pos = None 
)
Return True if any of one or many bits are set to value.

value -- If value is True then checks for bits set to 1, otherwise
 checks for bits set to 0.
pos -- An iterable of bit positions. Negative numbers are treated in
       the same way as slice indices. Defaults to the whole bitstring.

Definition at line 2748 of file bitstring.py.

◆ count()

def count (   self,
  value 
)
Return count of total number of either zero or one bits.

value -- If True then bits set to 1 are counted, otherwise bits set
 to 0 are counted.

>>> Bits('0xef').count(1)
7

Definition at line 2770 of file bitstring.py.

◆ cut()

def cut (   self,
  bits,
  start = None,
  end = None,
  count = None 
)
Return bitstring generator by cutting into bits sized chunks.

bits -- The size in bits of the bitstring chunks to generate.
start -- The bit position to start the first cut. Defaults to 0.
end -- The bit position one past the last bit to use in the cut.
       Defaults to self.len.
count -- If specified then at most count items are generated.
 Default is to cut as many times as possible.

Definition at line 2547 of file bitstring.py.

◆ endswith()

def endswith (   self,
  suffix,
  start = None,
  end = None 
)
Return whether the current bitstring ends with suffix.

suffix -- The bitstring to search for.
start -- The bit position to start from. Defaults to 0.
end -- The bit position to end at. Defaults to self.len.

Definition at line 2711 of file bitstring.py.

◆ find()

def find (   self,
  bs,
  start = None,
  end = None,
  bytealigned = None 
)
Find first occurrence of substring bs.

Returns a single item tuple with the bit position if found, or an
empty tuple if not found. The bit position (pos property) will
also be set to the start of the substring if it is found.

bs -- The bitstring to find.
start -- The bit position to start the search. Defaults to 0.
end -- The bit position one past the last bit to search.
       Defaults to self.len.
bytealigned -- If True the bitstring will only be
       found on byte boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or
if end < start.

>>> BitArray('0xc3e').find('0b1111')
(6,)

Definition at line 2418 of file bitstring.py.

◆ findall()

def findall (   self,
  bs,
  start = None,
  end = None,
  count = None,
  bytealigned = None 
)
Find all occurrences of bs. Return generator of bit positions.

bs -- The bitstring to find.
start -- The bit position to start the search. Defaults to 0.
end -- The bit position one past the last bit to search.
       Defaults to self.len.
count -- The maximum number of occurrences to find.
bytealigned -- If True the bitstring will only be found on
       byte boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or
if end < start.

Note that all occurrences of bs are found, even if they overlap.

Definition at line 2456 of file bitstring.py.

◆ join()

def join (   self,
  sequence 
)
Return concatenation of bitstrings joined by self.

sequence -- A sequence of bitstrings.

Definition at line 2631 of file bitstring.py.

◆ rfind()

def rfind (   self,
  bs,
  start = None,
  end = None,
  bytealigned = None 
)
Find final occurrence of substring bs.

Returns a single item tuple with the bit position if found, or an
empty tuple if not found. The bit position (pos property) will
also be set to the start of the substring if it is found.

bs -- The bitstring to find.
start -- The bit position to end the reverse search. Defaults to 0.
end -- The bit position one past the first bit to reverse search.
       Defaults to self.len.
bytealigned -- If True the bitstring will only be found on byte
       boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or
if end < start.

Definition at line 2508 of file bitstring.py.

◆ split()

def split (   self,
  delimiter,
  start = None,
  end = None,
  count = None,
  bytealigned = None 
)
Return bitstring generator by splittling using a delimiter.

The first item returned is the initial bitstring before the delimiter,
which may be an empty bitstring.

delimiter -- The bitstring used as the divider.
start -- The bit position to start the split. Defaults to 0.
end -- The bit position one past the last bit to use in the split.
       Defaults to self.len.
count -- If specified then at most count items are generated.
 Default is to split as many times as possible.
bytealigned -- If True splits will only occur on byte boundaries.

Raises ValueError if the delimiter is empty.

Definition at line 2574 of file bitstring.py.

◆ startswith()

def startswith (   self,
  prefix,
  start = None,
  end = None 
)
Return whether the current bitstring starts with prefix.

prefix -- The bitstring to search for.
start -- The bit position to start from. Defaults to 0.
end -- The bit position to end at. Defaults to self.len.

Definition at line 2696 of file bitstring.py.

◆ tobytes()

def tobytes (   self)
Return the bitstring as bytes, padding with zero bits if needed.

Up to seven zero bits will be added at the end to byte align.

Definition at line 2649 of file bitstring.py.

◆ tofile()

def tofile (   self,
  f 
)
Write the bitstring to a file object, padding with zero bits if needed.

Up to seven zero bits will be added at the end to byte align.

Definition at line 2662 of file bitstring.py.

◆ unpack()

def unpack (   self,
  fmt,
**  kwargs 
)
Interpret the whole bitstring using fmt and return list.

fmt -- A single string or a list of strings with comma separated tokens
       describing how to interpret the bits in the bitstring. Items
       can also be integers, for reading new bitstring of the given length.
kwargs -- A dictionary or keyword-value pairs - the keywords used in the
  format string will be replaced with their given value.

Raises ValueError if the format is not understood. If not enough bits
are available then all bits to the end of the bitstring will be used.

See the docstring for 'read' for token examples.

Definition at line 2265 of file bitstring.py.

Property Documentation

◆ bin

bin
static
Initial value:
= property(_getbin,
doc=)

Definition at line 2829 of file bitstring.py.

◆ bool

bool
static
Initial value:
= property(_getbool,
doc=)

Definition at line 2823 of file bitstring.py.

◆ bytes

bytes
static
Initial value:
= property(_getbytes,
doc=)

Definition at line 2835 of file bitstring.py.

◆ float

float
static
Initial value:
= property(_getfloat,
doc=)

Definition at line 2844 of file bitstring.py.

◆ floatbe

floatbe
static
Initial value:
= property(_getfloat,
doc=)

Definition at line 2853 of file bitstring.py.

◆ floatle

floatle
static
Initial value:
= property(_getfloatle,
doc=)

Definition at line 2862 of file bitstring.py.

◆ floatne

floatne
static
Initial value:
= property(_getfloatne,
doc=)

Definition at line 2871 of file bitstring.py.

◆ hex

hex
static
Initial value:
= property(_gethex,
doc=)

Definition at line 2826 of file bitstring.py.

◆ int

int
static
Initial value:
= property(_getint,
doc=)

Definition at line 2838 of file bitstring.py.

◆ intbe

intbe
static
Initial value:
= property(_getintbe,
doc=)

Definition at line 2847 of file bitstring.py.

◆ intle

intle
static
Initial value:
= property(_getintle,
doc=)

Definition at line 2856 of file bitstring.py.

◆ intne

intne
static
Initial value:
= property(_getintne,
doc=)

Definition at line 2865 of file bitstring.py.

◆ len

len
static
Initial value:
= property(_getlength,
doc=)

Definition at line 2817 of file bitstring.py.

◆ length

length
static
Initial value:
= property(_getlength,
doc=)

Definition at line 2820 of file bitstring.py.

◆ oct

oct
static
Initial value:
= property(_getoct,
doc=)

Definition at line 2832 of file bitstring.py.

◆ se

se
static
Initial value:
= property(_getse,
doc=)

Definition at line 2877 of file bitstring.py.

◆ sie

sie
static
Initial value:
= property(_getsie,
doc=)

Definition at line 2883 of file bitstring.py.

◆ ue

ue
static
Initial value:
= property(_getue,
doc=)

Definition at line 2874 of file bitstring.py.

◆ uie

uie
static
Initial value:
= property(_getuie,
doc=)

Definition at line 2880 of file bitstring.py.

◆ uint

uint
static
Initial value:
= property(_getuint,
doc=)

Definition at line 2841 of file bitstring.py.

◆ uintbe

uintbe
static
Initial value:
= property(_getuintbe,
doc=)

Definition at line 2850 of file bitstring.py.

◆ uintle

uintle
static
Initial value:
= property(_getuintle,
doc=)

Definition at line 2859 of file bitstring.py.

◆ uintne

uintne
static
Initial value:
= property(_getuintne,
doc=)

Definition at line 2868 of file bitstring.py.


The documentation for this class was generated from the following file: