PySide6.QtCore.QBitArray¶
- class QBitArray¶
- The - QBitArrayclass provides an array of bits. More…- Synopsis¶- Methods¶- def - __init__()
- def - __getitem__()
- def - __len__()
- def - __setitem__()
- def - at()
- def - bits()
- def - clear()
- def - clearBit()
- def - count()
- def - fill()
- def - isEmpty()
- def - isNull()
- def - __ne__()
- def - __and__()
- def - __iand__()
- def - __eq__()
- def - operator[]()
- def - __xor__()
- def - __ixor__()
- def - __or__()
- def - __ior__()
- def - __invert__()
- def - resize()
- def - setBit()
- def - size()
- def - swap()
- def - testBit()
- def - toUInt32()
- def - toggleBit()
- def - truncate()
 - Static functions¶- def - fromBits()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - A - QBitArrayis an array that gives access to individual bits and provides operators (- AND,- OR,- XOR, and- NOT) that work on entire arrays of bits. It uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data.- The following code constructs a - QBitArraycontaining 200 bits initialized to false (0):- ba = QBitArray(200) - To initialize the bits to true, either pass - trueas second argument to the constructor, or call- fill()later on.- QBitArrayuses 0-based indexes, just like C++ arrays. To access the bit at a particular index position, you can use operator[](). On non-const bit arrays, operator[]() returns a reference to a bit that can be used on the left side of an assignment. For example:- ba = QBitArray() ba.resize(3) ba[0] = True ba[1] = False ba[2] = True - For technical reasons, it is more efficient to use - testBit()and- setBit()to access bits in the array than operator[](). For example:- ba = QBitArray(3) ba.setBit(0, True) ba.setBit(1, False) ba.setBit(2, True) - QBitArraysupports- &(- AND),- |(- OR),- ^(- XOR),- ~(- NOT), as well as- &=,- |=, and- ^=. These operators work in the same way as the built-in C++ bitwise operators of the same name. For example:- x = QBitArray(5) x.setBit(3, True) # x: [ 0, 0, 0, 1, 0 ] y = QBitArray(5) y.setBit(4, True) # y: [ 0, 0, 0, 0, 1 ] x |= y # x: [ 0, 0, 0, 1, 1 ] - For historical reasons, - QBitArraydistinguishes between a null bit array and an empty bit array. A null bit array is a bit array that is initialized using- QBitArray‘s default constructor. An empty bit array is any bit array with size 0. A null bit array is always empty, but an empty bit array isn’t necessarily null:- QBitArray().isNull() # returns true QBitArray().isEmpty() # returns true QBitArray(0).isNull() # returns false QBitArray(0).isEmpty() # returns true QBitArray(3).isNull() # returns false QBitArray(3).isEmpty() # returns false - All functions except - isNull()treat null bit arrays the same as empty bit arrays; for example,- QBitArray()compares equal to- QBitArray(0). We recommend that you always use- isEmpty()and avoid- isNull().- See also - QByteArray- QList- __init__()¶
 - Constructs an empty bit array. - See also - __init__(other)
- Parameters:
- other – - QBitArray
 
 - Constructs a copy of - other.- This operation takes constant time , because - QBitArrayis implicitly shared . This makes returning a- QBitArrayfrom a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time .- See also - operator=()- __init__(size[, val=false])
- Parameters:
- size – int 
- val – bool 
 
 
 - Constructs a bit array containing - sizebits. The bits are initialized with- value, which defaults to false (0).- __getitem__()¶
 - __len__()¶
- Return type:
- int 
 
 - __setitem__()¶
 - at(i)¶
- Parameters:
- i – int 
- Return type:
- bool 
 
 - Returns the value of the bit at index position - i.- imust be a valid index position in the bit array (i.e., 0 <=- i<- size()).- See also - operator[]()- bits()¶
- Return type:
- str 
 
 - Returns a pointer to a dense bit array for this - QBitArray. Bits are counted upwards from the least significant bit in each byte. The number of bits relevant in the last byte is given by- size() % 8.- See also - clear()¶
 - Clears the contents of the bit array and makes it empty. - clearBit(i)¶
- Parameters:
- i – int 
 
 - Sets the bit at index position - ito 0.- imust be a valid index position in the bit array (i.e., 0 <=- i<- size()).- See also - count()¶
- Return type:
- int 
 
 - Same as - size().- count(on)
- Parameters:
- on – bool 
- Return type:
- int 
 
 - If - onis true, this function returns the number of 1-bits stored in the bit array; otherwise the number of 0-bits is returned.- fill(aval[, asize=-1])¶
- Parameters:
- aval – bool 
- asize – int 
 
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets every bit in the bit array to - value, returning true if successful; otherwise returns- false. If- sizeis different from -1 (the default), the bit array is resized to- sizebeforehand.- Example: - ba = QBitArray(8) ba.fill(True) # ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ] ba.fill(False, 2) # ba: [ 0, 0 ] - See also - fill(val, first, last)
- Parameters:
- val – bool 
- first – int 
- last – int 
 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - This is an overloaded function. - Sets bits at index positions - beginup to (but not including)- endto- value.- beginmust be a valid index position in the bit array (0 <=- begin<- size()).- endmust be either a valid index position or equal to- size(), in which case the fill operation runs until the end of the array (0 <=- end<=- size()).- Example: - ba = QBitArray(4) ba.fill(True, 1, 2) # ba: [ 0, 1, 0, 0 ] ba.fill(True, 1, 3) # ba: [ 0, 1, 1, 0 ] ba.fill(True, 1, 4) # ba: [ 0, 1, 1, 1 ] - Creates a - QBitArraywith the dense bit array located at- data, with- sizebits. The byte array at- datamust be at least- size/ 8 (rounded up) bytes long.- If - sizeis not a multiple of 8, this function will include the lowest- size% 8 bits from the last byte in- data.- See also - isEmpty()¶
- Return type:
- bool 
 
 - Returns - trueif this bit array has size 0; otherwise returns false.- See also - isNull()¶
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns - trueif this bit array is null; otherwise returns- false.- Example: - QBitArray().isNull() # returns true QBitArray(0).isNull() # returns false QBitArray(3).isNull() # returns false - Qt makes a distinction between null bit arrays and empty bit arrays for historical reasons. For most applications, what matters is whether or not a bit array contains any data, and this can be determined using - isEmpty().- See also - Returns - trueif- lhsis not equal to- rhsbit array; otherwise returns- false.- See also - operator==()- Returns - trueif- lhsis equal to- rhsbit array; otherwise returns- false.- See also - operator!=()- operator(i)¶
- Parameters:
- i – int 
- Return type:
- bool 
 
 - This is an overloaded function. - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns a bit array that contains the inverted bits of the bit array - a.- Example: - a = QBitArray(3) b = QBitArray() a[0] = 1 a[1] = 0; a[2] = 1; # a: [ 1, 0, 1 ] b = ~a # b: [ 0, 1, 0 ] - See also - operator&()- operator|()- operator^()- resize(size)¶
- Parameters:
- size – int 
 
 - Resizes the bit array to - sizebits.- If - sizeis greater than the current size, the bit array is extended to make it- sizebits with the extra bits added to the end. The new bits are initialized to false (0).- If - sizeis less than the current size, bits are removed from the end.- See also - setBit(i)¶
- Parameters:
- i – int 
 
 - Sets the bit at index position - ito 1.- imust be a valid index position in the bit array (i.e., 0 <=- i<- size()).- See also - setBit(i, val)
- Parameters:
- i – int 
- val – bool 
 
 
 - This is an overloaded function. - Sets the bit at index position - ito- value.- size()¶
- Return type:
- int 
 
 - Returns the number of bits stored in the bit array. - See also - Swaps this bit array with - other. This operation is very fast and never fails.- testBit(i)¶
- Parameters:
- i – int 
- Return type:
- bool 
 
 - Returns - trueif the bit at index position- iis 1; otherwise returns- false.- imust be a valid index position in the bit array (i.e., 0 <=- i<- size()).- See also - Returns the array of bit converted to an int. The conversion is based on - endianness. Converts up to the first 32 bits of the array to- quint32and returns it, obeying- endianness. If- okis not a null pointer, and the array has more than 32 bits,- okis set to false and this function returns zero; otherwise, it’s set to true.- toggleBit(i)¶
- Parameters:
- i – int 
- Return type:
- bool 
 
 - Inverts the value of the bit at index position - i, returning the previous value of that bit as either true (if it was set) or false (if it was unset).- If the previous value was 0, the new value will be 1. If the previous value was 1, the new value will be 0. - imust be a valid index position in the bit array (i.e., 0 <=- i<- size()).- See also - truncate(pos)¶
- Parameters:
- pos – int 
 
 - Truncates the bit array at index position - pos.- If - posis beyond the end of the array, nothing happens.- See also