6.0.0-rc4Source

QBitArray Class

The QBitArray class provides an array of bits. More…

Header#include <QBitArray>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QBitArray()
QBitArray(qsizetype size, bool value = false)
qsizetypesize() const
qsizetypecount() const
qsizetypecount(bool on) const
boolisEmpty() const
boolisNull() const
voidresize(qsizetype size)
voidtruncate(qsizetype pos)
voidclear()
voidswap(QBitArray &o)
booltestBit(qsizetype i) const
boolat(qsizetype i) const
voidsetBit(qsizetype i)
voidsetBit(qsizetype i, bool v)
voidclearBit(qsizetype i)
booltoggleBit(qsizetype i)
boolfill(bool v, qsizetype size = -1)
voidfill(bool v, qsizetype first, qsizetype last)
booloperator[](qsizetype i) const
QBitRefoperator[](qsizetype i)
QBitArray &operator&=(const QBitArray &o)
QBitArray &operator|=(const QBitArray &o)
QBitArray &operator^=(const QBitArray &o)
QBitArrayoperator~() const
const char *bits() const
quint32toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const

Static Public Members

QBitArrayfromBits(const char *data, qsizetype size)

Detailed Description

A QBitArray stores a sequence of bits and gives access to each of them by index. Bits can be tested, set, cleared and toggled individually, and arrays can be combined with the bitwise operators &, |, ^ and ~.

QBitArray bits(5);
bits.setBit(1);
bits.setBit(3);
QBitArray inverted = ~bits;

Like QByteArray, a QBitArray distinguishes between a null array and an empty one; see isNull().

See also QByteArray.

Differences from Qt

Bits are stored in a std::vector<bool>, so bits() packs them into a separate buffer each time it is called. Indexes are not range-checked. The bit count is a qsizetype.

Member Function Documentation

QBitArray::QBitArray()

Constructs an empty, null bit array.

See also isNull().

[explicit] QBitArray::QBitArray(qsizetype size, bool value = false)

Constructs a bit array containing size bits, each initialized to value. The array is not null.

bool QBitArray::at(qsizetype i) const

Returns the value of the bit at index position i.

See also testBit().

const char * QBitArray::bits() const

Returns the bits packed into bytes: bit i is bit i % 8 of byte i / 8, counting from the least significant bit. Unused bits of the last byte are 0. The pointer stays valid until the array is changed or bits() is called again.

This function was introduced in 5.11.

See also fromBits() and size().

void QBitArray::clear()

Clears the contents of the array and makes it null.

See also isNull() and resize().

void QBitArray::clearBit(qsizetype i)

Sets the bit at index position i to false.

See also setBit() and toggleBit().

qsizetype QBitArray::count() const

Same as size().

qsizetype QBitArray::count(bool on) const

This is an overloaded function.

Returns the number of bits in the array whose value is on.

bool QBitArray::fill(bool v, qsizetype size = -1)

Sets every bit in the array to v and returns true. If size is not negative, the array is first resized to size bits.

See also resize().

void QBitArray::fill(bool v, qsizetype first, qsizetype last)

This is an overloaded function.

Sets the bits from index first up to, but not including, index last to v. Both indexes must be valid, with first not greater than last.

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

Returns a bit array of size bits read from data, in the layout that bits() produces. data must hold at least (size + 7) / 8 bytes.

This function was introduced in 5.11.

See also bits().

bool QBitArray::isEmpty() const

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

bool QBitArray::isNull() const

Returns true if this array is null; otherwise returns false.

A default-constructed array, or one that has been cleared, is null. An array becomes non-null when it is constructed with a size or resized.

See also isEmpty() and clear().

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

Performs a bitwise AND of this array with o and stores the result in this array. If o is longer, this array is enlarged to its size; the shorter operand is treated as padded with zero bits. Returns a reference to this array.

bool QBitArray::operator[](qsizetype i) const

Returns the value of the bit at index position i.

QBitRef QBitArray::operator[](qsizetype i)

This is an overloaded function.

Returns a QBitRef that refers to the bit at index position i and can be assigned to.

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

Performs a bitwise XOR of this array with o and stores the result in this array. If o is longer, this array is enlarged to its size; the shorter operand is treated as padded with zero bits. Returns a reference to this array.

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

Performs a bitwise OR of this array with o and stores the result in this array. If o is longer, this array is enlarged to its size; the shorter operand is treated as padded with zero bits. Returns a reference to this array.

QBitArray QBitArray::operator~() const

Returns a copy of this array with every bit inverted.

void QBitArray::resize(qsizetype size)

Resizes the array to size bits. New bits are set to false.

See also size() and truncate().

void QBitArray::setBit(qsizetype i)

Sets the bit at index position i to true.

See also clearBit() and toggleBit().

void QBitArray::setBit(qsizetype i, bool v)

This is an overloaded function.

Sets the bit at index position i to v.

qsizetype QBitArray::size() const

Returns the number of bits in the array.

void QBitArray::swap(QBitArray &o)

Swaps this bit array with o. This operation is very fast and never fails.

bool QBitArray::testBit(qsizetype i) const

Returns true if the bit at index position i is set; otherwise returns false. i must be a valid index.

See also setBit() and clearBit().

bool QBitArray::toggleBit(qsizetype i)

Inverts the bit at index position i and returns its previous value.

See also setBit() and clearBit().

quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const

Returns the bits as a 32-bit number. With QSysInfo::LittleEndian endianness, bit i of the array is bit i of the number; with QSysInfo::BigEndian, the first bit of the array is the most significant of the size() bits. If the array has more than 32 bits, returns 0 and sets *ok to false; otherwise sets *ok to true, if ok is not nullptr.

This function was introduced in 6.0.

void QBitArray::truncate(qsizetype pos)

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

See also resize().