QBitArray Class

The QBitArray class provides an array of bits. More...

Header: #include <QBitArray>
qmake: QT += core

Note: All functions in this class are reentrant.

Public Functions

QBitArray(int size, bool value = false)
int count(bool on) const
void fill(bool value, int begin, int end)
void resize(int size)
QBitArray &operator&=(const QBitArray &other)
QBitArray &operator^=(const QBitArray &other)
QBitArray &operator|=(const QBitArray &other)
QBitArray operator~() const

Static Public Members

QBitArray fromBits(const char *data, qsizetype size)
QBitArray operator&(const QBitArray &a1, const QBitArray &a2)
QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
QDataStream &operator>>(QDataStream &in, QBitArray &ba)
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
QBitArray operator|(const QBitArray &a1, const QBitArray &a2)

Detailed Description

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


  QBitArray ba(200);

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

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:


  QBitArray ba;
  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:


  QBitArray ba(3);
  ba.setBit(0, true);
  ba.setBit(1, false);
  ba.setBit(2, true);

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:


  QBitArray x(5);
  x.setBit(3, true);
  // x: [ 0, 0, 0, 1, 0 ]

  QBitArray y(5);
  y.setBit(4, true);
  // y: [ 0, 0, 0, 0, 1 ]

  x |= y;
  // x: [ 0, 0, 0, 1, 1 ]

For historical reasons, QBitArray distinguishes 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 and QVector.

Member Function Documentation

QBitArray::QBitArray(int size, bool value = false)

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

int QBitArray::count(bool on) const

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.

void QBitArray::fill(bool value, int begin, int end)

This is an overloaded function.

Sets bits at index positions begin up to (but not including) end to value.

begin must be a valid index position in the bit array (0 <= begin < size()).

end must 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:


  QBitArray ba(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 ]

[static] QBitArray QBitArray::fromBits(const char *data, qsizetype size)

Creates a QBitArray with the dense bit array located at data, with size bits. The byte array at data must be at least size / 8 (rounded up) bytes long.

If size is not a multiple of 8, this function will include the lowest size % 8 bits from the last byte in data.

This function was introduced in Qt 5.11.

See also bits().

void QBitArray::resize(int size)

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.

See also size().

QBitArray &QBitArray::operator&=(const QBitArray &other)

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:


  QBitArray a(3);
  QBitArray b(2);
  a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
  b[0] = 1; b[1] = 1;             // b: [ 1, 1 ]
  a &= b;                         // a: [ 1, 0, 0 ]

See also operator&(), operator|=(), operator^=(), and operator~().

QBitArray &QBitArray::operator^=(const QBitArray &other)

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:


  QBitArray a(3);
  QBitArray b(2);
  a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
  b[0] = 1; b[1] = 1;             // b: [ 1, 1 ]
  a ^= b;                         // a: [ 0, 1, 1 ]

See also operator^(), operator&=(), operator|=(), and operator~().

QBitArray &QBitArray::operator|=(const QBitArray &other)

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:


  QBitArray a(3);
  QBitArray b(2);
  a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
  b[0] = 1; b[1] = 1;             // b: [ 1, 1 ]
  a |= b;                         // a: [ 1, 1, 1 ]

See also operator|(), operator&=(), operator^=(), and operator~().

QBitArray QBitArray::operator~() const

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

Example:


  QBitArray a(3);
  QBitArray b;
  a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
  b = ~a;                         // b: [ 0, 1, 0 ]

See also operator&(), operator|(), and operator^().

Related Non-Members

QBitArray QBitArray::operator&(const QBitArray &a1, const QBitArray &a2)

Returns a bit array that is the AND of the bit arrays a1 and a2.

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:


  QBitArray a(3);
  QBitArray b(2);
  QBitArray c;
  a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
  b[0] = 1; b[1] = 1;             // b: [ 1, 1 ]
  c = a & b;                      // c: [ 1, 0, 0 ]

See also operator&=(), operator|(), and operator^().

QDataStream &QBitArray::operator<<(QDataStream &out, const QBitArray &ba)

Writes bit array ba to stream out.

See also Format of the QDataStream operators.

QDataStream &QBitArray::operator>>(QDataStream &in, QBitArray &ba)

Reads a bit array into ba from stream in.

See also Format of the QDataStream operators.

QBitArray QBitArray::operator^(const QBitArray &a1, const QBitArray &a2)

Returns a bit array that is the XOR of the bit arrays a1 and a2.

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:


  QBitArray a(3);
  QBitArray b(2);
  QBitArray c;
  a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
  b[0] = 1; b[1] = 1;             // b: [ 1, 1 ]
  c = a ^ b;                      // c: [ 0, 1, 1 ]

See also operator^=(), operator&(), and operator|().

QBitArray QBitArray::operator|(const QBitArray &a1, const QBitArray &a2)

Returns a bit array that is the OR of the bit arrays a1 and a2.

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:


  QBitArray a(3);
  QBitArray b(2);
  QBitArray c;
  a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
  b[0] = 1; b[1] = 1;             // b: [ 1, 1 ]
  c = a | b;                      // c: [ 1, 1, 1 ]

See also QBitArray::operator|=(), operator&(), and operator^().