Table Of Contents

Previous topic

QByteArrayMatcher

Next topic

QSystemSemaphore

QBitArray

Inheritance diagram of QBitArray

Synopsis

Functions

Detailed Description

The PySide.QtCore.QBitArray class provides an array of bits.

A PySide.QtCore.QBitArray is 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 PySide.QtCore.QBitArray containing 200 bits initialized to false (0):

ba = QBitArray(200)

To initialize the bits to true, either pass true as second argument to the constructor, or call PySide.QtCore.QBitArray.fill() later on.

PySide.QtCore.QBitArray uses 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 PySide.QtCore.QBitArray.testBit() and PySide.QtCore.QBitArray.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)

PySide.QtCore.QBitArray supports & ( 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, PySide.QtCore.QBitArray distinguishes between a null bit array and an empty bit array. A null bit array is a bit array that is initialized using PySide.QtCore.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 PySide.QtCore.QBitArray.isNull() treat null bit arrays the same as empty bit arrays; for example, PySide.QtCore.QBitArray.QBitArray() compares equal to PySide.QtCore.QBitArray (0). We recommend that you always use PySide.QtCore.QBitArray.isEmpty() and avoid PySide.QtCore.QBitArray.isNull() .

See also

PySide.QtCore.QByteArray QVector

class PySide.QtCore.QBitArray
class PySide.QtCore.QBitArray(other)
class PySide.QtCore.QBitArray(size[, val=false])
Parameters:

Constructs an empty bit array.

Constructs a copy of other .

This operation takes constant time , because PySide.QtCore.QBitArray is implicitly shared . This makes returning a PySide.QtCore.QBitArray from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time .

See also

PySide.QtCore.QBitArray.operator=()

Constructs a bit array containing size bits. The bits are initialized with value , which defaults to false (0).

PySide.QtCore.QBitArray.__getitem__()
PySide.QtCore.QBitArray.__len__()
PySide.QtCore.QBitArray.__setitem__()
PySide.QtCore.QBitArray.at(i)
Parameters:iPySide.QtCore.int
Return type:PySide.QtCore.bool

Returns the value of the bit at index position i .

i must be a valid index position in the bit array (i.e., 0 <= i < PySide.QtCore.QBitArray.size() ).

See also

PySide.QtCore.QBitArray.operator[]()

PySide.QtCore.QBitArray.clear()

Clears the contents of the bit array and makes it empty.

PySide.QtCore.QBitArray.clearBit(i)
Parameters:iPySide.QtCore.int

Sets the bit at index position i to 0.

i must be a valid index position in the bit array (i.e., 0 <= i < PySide.QtCore.QBitArray.size() ).

PySide.QtCore.QBitArray.count()
Return type:PySide.QtCore.int

Same as PySide.QtCore.QBitArray.size() .

PySide.QtCore.QBitArray.count(on)
Parameters:onPySide.QtCore.bool
Return type:PySide.QtCore.int

If on is true, this function returns the number of 1-bits stored in the bit array; otherwise the number of 0-bits is returned.

PySide.QtCore.QBitArray.fill(val, first, last)
Parameters:
  • valPySide.QtCore.bool
  • firstPySide.QtCore.int
  • lastPySide.QtCore.int

This is an overloaded function.

Sets bits at index positions begin up to and excluding end to value .

begin and end must be a valid index position in the bit array (i.e., 0 <= begin <= PySide.QtCore.QBitArray.size() and 0 <= end <= PySide.QtCore.QBitArray.size() ).

PySide.QtCore.QBitArray.fill(val[, size=-1])
Parameters:
  • valPySide.QtCore.bool
  • sizePySide.QtCore.int
Return type:

PySide.QtCore.bool

Sets every bit in the bit array to value , returning true if successful; otherwise returns false. If size is different from -1 (the default), the bit array is resized to size beforehand.

Example:

ba = QBitArray(8)
ba.fill(True)
# ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ]

ba.fill(False, 2)
# ba: [ 0, 0 ]
PySide.QtCore.QBitArray.isEmpty()
Return type:PySide.QtCore.bool

Returns true if this bit array has size 0; otherwise returns false.

PySide.QtCore.QBitArray.isNull()
Return type:PySide.QtCore.bool

Returns true if 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 PySide.QtCore.QBitArray.isEmpty() .

PySide.QtCore.QBitArray.__ne__(a)
Parameters:aPySide.QtCore.QBitArray
Return type:PySide.QtCore.bool

Returns true if other is not equal to this bit array; otherwise returns false.

See also

PySide.QtCore.QBitArray.operator==()

PySide.QtCore.QBitArray.__and__(arg__2)
Parameters:arg__2PySide.QtCore.QBitArray
Return type:PySide.QtCore.QBitArray
PySide.QtCore.QBitArray.__iand__(arg__1)
Parameters:arg__1PySide.QtCore.QBitArray
Return type:PySide.QtCore.QBitArray

Performs the AND operation between all bits in this bit array and other . Assigns the result to this bit array, and returns a reference to it.

The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.

Example:

a = QBitArray(3)
b = QBitArray(2)
a[0] = 1
a[1] = 0
a[2] = 1
# a: [ 1, 0, 1 ]

b[0] = 1
b[1] = 0
# b: [ 1, 1 ]

a &= b
# a: [ 1, 0, 0 ]

See also

PySide.QtCore.QBitArray.operator&() PySide.QtCore.QBitArray.operator|=() PySide.QtCore.QBitArray.operator^=() PySide.QtCore.QBitArray.operator~()

PySide.QtCore.QBitArray.__eq__(a)
Parameters:aPySide.QtCore.QBitArray
Return type:PySide.QtCore.bool

Returns true if other is equal to this bit array; otherwise returns false.

See also

PySide.QtCore.QBitArray.operator!=()

PySide.QtCore.QBitArray.operator[](i)
Parameters:iPySide.QtCore.int
Return type:PySide.QtCore.bool

This is an overloaded function.

PySide.QtCore.QBitArray.operator[](i)
Parameters:iPySide.QtCore.uint
Return type:PySide.QtCore.bool

This is an overloaded function.

PySide.QtCore.QBitArray.__xor__(arg__2)
Parameters:arg__2PySide.QtCore.QBitArray
Return type:PySide.QtCore.QBitArray
PySide.QtCore.QBitArray.__ixor__(arg__1)
Parameters:arg__1PySide.QtCore.QBitArray
Return type:PySide.QtCore.QBitArray

Performs the XOR operation between all bits in this bit array and other . Assigns the result to this bit array, and returns a reference to it.

The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.

Example:

a = QBitArray(3)
b = QBitArray(2)
a[0] = 1
a[1] = 0
a[2] = 1
# a: [ 1, 0, 1 ]

b[0] = 1
b[1] = 0
# b: [ 1, 1 ]

a ^= b
# a: [ 0, 1, 1 ]

See also

PySide.QtCore.QBitArray.operator^() PySide.QtCore.QBitArray.operator&=() PySide.QtCore.QBitArray.operator|=() PySide.QtCore.QBitArray.operator~()

PySide.QtCore.QBitArray.__or__(arg__2)
Parameters:arg__2PySide.QtCore.QBitArray
Return type:PySide.QtCore.QBitArray
PySide.QtCore.QBitArray.__ior__(arg__1)
Parameters:arg__1PySide.QtCore.QBitArray
Return type:PySide.QtCore.QBitArray

Performs the OR operation between all bits in this bit array and other . Assigns the result to this bit array, and returns a reference to it.

The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.

Example:

a = QBitArray(3)
b = QBitArray(2)
a[0] = 1
a[1] = 0
a[2] = 1
# a: [ 1, 0, 1 ]

b[0] = 1
b[1] = 0
# b: [ 1, 1 ]

a |= b
# a: [ 1, 1, 1 ]

See also

PySide.QtCore.QBitArray.operator|() PySide.QtCore.QBitArray.operator&=() PySide.QtCore.QBitArray.operator^=() PySide.QtCore.QBitArray.operator~()

PySide.QtCore.QBitArray.operator~()
Return type:PySide.QtCore.QBitArray

Returns a bit array that contains the inverted bits of this bit array.

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

PySide.QtCore.QBitArray.operator&() PySide.QtCore.QBitArray.operator|() PySide.QtCore.QBitArray.operator^()

PySide.QtCore.QBitArray.resize(size)
Parameters:sizePySide.QtCore.int

Resizes the bit array to size bits.

If size is greater than the current size, the bit array is extended to make it size bits with the extra bits added to the end. The new bits are initialized to false (0).

If size is less than the current size, bits are removed from the end.

PySide.QtCore.QBitArray.setBit(i)
Parameters:iPySide.QtCore.int

Sets the bit at index position i to 1.

i must be a valid index position in the bit array (i.e., 0 <= i < PySide.QtCore.QBitArray.size() ).

PySide.QtCore.QBitArray.setBit(i, val)
Parameters:
  • iPySide.QtCore.int
  • valPySide.QtCore.bool

This is an overloaded function.

Sets the bit at index position i to value .

PySide.QtCore.QBitArray.size()
Return type:PySide.QtCore.int

Returns the number of bits stored in the bit array.

PySide.QtCore.QBitArray.testBit(i)
Parameters:iPySide.QtCore.int
Return type:PySide.QtCore.bool

Returns true if the bit at index position i is 1; otherwise returns false.

i must be a valid index position in the bit array (i.e., 0 <= i < PySide.QtCore.QBitArray.size() ).

PySide.QtCore.QBitArray.toggleBit(i)
Parameters:iPySide.QtCore.int
Return type:PySide.QtCore.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.

i must be a valid index position in the bit array (i.e., 0 <= i < PySide.QtCore.QBitArray.size() ).

PySide.QtCore.QBitArray.truncate(pos)
Parameters:posPySide.QtCore.int

Truncates the bit array at index position pos .

If pos is beyond the end of the array, nothing happens.