QtBase  v6.3.1
Public Types | Public Member Functions | Static Public Member Functions | Friends | Related Functions | List of all members
QBitArray Class Reference

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

#include <qbitarray.h>

Public Types

typedef QByteArray::DataPointer DataPtr
 

Public Member Functions

 QBitArray () noexcept
 
 QBitArray (qsizetype size, bool val=false)
 
 QBitArray (const QBitArray &other)
 
QBitArrayoperator= (const QBitArray &other)
 
 QBitArray (QBitArray &&other) noexcept
 
void swap (QBitArray &other) noexcept
 
qsizetype size () const
 
qsizetype count () const
 
qsizetype count (bool on) const
 
bool isEmpty () const
 
bool isNull () const
 
void resize (qsizetype size)
 
void detach ()
 
bool isDetached () const
 
void clear ()
 
bool testBit (qsizetype i) const
 
void setBit (qsizetype i)
 
void setBit (qsizetype i, bool val)
 
void clearBit (qsizetype i)
 
bool toggleBit (qsizetype i)
 
bool at (qsizetype i) const
 
QBitRef operator[] (qsizetype i)
 
bool operator[] (qsizetype i) const
 
QBitArrayoperator&= (const QBitArray &)
 
QBitArrayoperator|= (const QBitArray &)
 
QBitArrayoperator^= (const QBitArray &)
 
QBitArray operator~ () const
 
bool operator== (const QBitArray &other) const
 
bool operator!= (const QBitArray &other) const
 
bool fill (bool val, qsizetype size=-1)
 
void fill (bool val, qsizetype first, qsizetype last)
 
void truncate (qsizetype pos)
 
const char * bits () const
 
quint32 toUInt32 (QSysInfo::Endian endianness, bool *ok=nullptr) const noexcept
 
DataPtrdata_ptr ()
 

Static Public Member Functions

static QBitArray fromBits (const char *data, qsizetype len)
 

Friends

Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &, const QBitArray &)
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QBitArray &)
 
Q_CORE_EXPORT size_t qHash (const QBitArray &key, size_t seed) noexcept
 

Related Functions

(Note that these are not member functions.)

QBitArray operator& (const QBitArray &a1, const QBitArray &a2)
 
QBitArray operator| (const QBitArray &a1, const QBitArray &a2)
 
QBitArray operator^ (const QBitArray &a1, const QBitArray &a2)
 
QDataStreamoperator<< (QDataStream &out, const QBitArray &ba)
 
QDataStreamoperator>> (QDataStream &in, QBitArray &ba)
 

Detailed Description

The QBitArray class provides an array of bits.

\inmodule QtCore

\reentrant

A QBitArray is an array that gives access to individual bits and provides operators (\l{operator&()}{AND}, \l{operator|()}{OR}, \l{operator^()}{XOR}, and \l{operator~()}{NOT}) that work on entire arrays of bits. It uses \l{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):

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:

For technical reasons, it is more efficient to use testBit() and setBit() to access bits in the array than operator[](). For example:

QBitArray supports {&} (\l{operator&()}{AND}), {|} (\l{operator|()}{OR}), {^} (\l{operator^()}{XOR}), {~} (\l{operator~()}{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:

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:

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

Definition at line 48 of file qbitarray.h.

Member Typedef Documentation

◆ DataPtr

Definition at line 109 of file qbitarray.h.

Constructor & Destructor Documentation

◆ QBitArray() [1/4]

QBitArray::QBitArray ( )
inlinenoexcept

Constructs an empty bit array.

See also
isEmpty()

Definition at line 58 of file qbitarray.h.

Here is the caller graph for this function:

◆ QBitArray() [2/4]

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

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

Definition at line 147 of file qbitarray.cpp.

Here is the call graph for this function:

◆ QBitArray() [3/4]

QBitArray::QBitArray ( const QBitArray other)
inline

Constructs a copy of other.

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

See also
operator=()

Definition at line 60 of file qbitarray.h.

◆ QBitArray() [4/4]

QBitArray::QBitArray ( QBitArray &&  other)
inlinenoexcept

Move-constructs a QBitArray instance, making it point at the same object that other was pointing to.

Since
5.2

Definition at line 62 of file qbitarray.h.

Member Function Documentation

◆ at()

bool QBitArray::at ( qsizetype  i) const
inline

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 < size()).

See also
operator[]()

Definition at line 141 of file qbitarray.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bits()

const char * QBitArray::bits ( ) const
inline
Since
5.11

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
fromBits(), size()

Definition at line 103 of file qbitarray.h.

◆ clear()

void QBitArray::clear ( )
inline

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

See also
resize(), isEmpty()

Definition at line 78 of file qbitarray.h.

Here is the caller graph for this function:

◆ clearBit()

void QBitArray::clearBit ( qsizetype  i)
inline

Sets the bit at index position i to 0.

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

See also
setBit(), toggleBit()

Definition at line 128 of file qbitarray.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ count() [1/2]

qsizetype QBitArray::count ( ) const
inline

Same as size().

Definition at line 68 of file qbitarray.h.

Here is the caller graph for this function:

◆ count() [2/2]

qsizetype 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.

Definition at line 178 of file qbitarray.cpp.

Here is the call graph for this function:

◆ data_ptr()

DataPtr & QBitArray::data_ptr ( )
inline

Definition at line 110 of file qbitarray.h.

◆ detach()

void QBitArray::detach ( )
inline

Definition at line 76 of file qbitarray.h.

◆ fill() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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:

Definition at line 288 of file qbitarray.cpp.

Here is the call graph for this function:

◆ fill() [2/2]

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

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:

See also
resize()

Definition at line 113 of file qbitarray.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromBits()

QBitArray QBitArray::fromBits ( const char *  data,
qsizetype  size 
)
static
Since
5.11

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.

See also
bits()

Definition at line 326 of file qbitarray.cpp.

Here is the call graph for this function:

◆ isDetached()

bool QBitArray::isDetached ( ) const
inline

Definition at line 77 of file qbitarray.h.

◆ isEmpty()

bool QBitArray::isEmpty ( ) const
inline

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

See also
size()

Definition at line 71 of file qbitarray.h.

◆ isNull()

bool QBitArray::isNull ( ) const
inline

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

Example:

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
isEmpty()

Definition at line 72 of file qbitarray.h.

◆ operator!=()

bool QBitArray::operator!= ( const QBitArray other) const
inline

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

See also
operator==()

Definition at line 96 of file qbitarray.h.

Here is the call graph for this function:

◆ operator&=()

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:

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

Definition at line 555 of file qbitarray.cpp.

Here is the call graph for this function:

◆ operator=()

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

Assigns other to this bit array and returns a reference to this bit array.

Since
5.2

Moves other to this bit array and returns a reference to this bit array.

Definition at line 61 of file qbitarray.h.

Here is the call graph for this function:

◆ operator==()

bool QBitArray::operator== ( const QBitArray other) const
inline

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

See also
operator!=()

Definition at line 95 of file qbitarray.h.

Here is the call graph for this function:

◆ operator[]() [1/2]

QBitRef QBitArray::operator[] ( qsizetype  i)
inline

Returns the bit at index position i as a modifiable reference.

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

Example:

The return value is of type QBitRef, a helper class for QBitArray. When you get an object of type QBitRef, you can assign to it, and the assignment will apply to the bit in the QBitArray from which you got the reference.

The functions testBit(), setBit(), and clearBit() are slightly faster.

See also
at(), testBit(), setBit(), clearBit()

Definition at line 158 of file qbitarray.h.

◆ operator[]() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 140 of file qbitarray.h.

Here is the call graph for this function:

◆ 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:

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

Definition at line 610 of file qbitarray.cpp.

Here is the call graph for this function:

◆ 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:

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

Definition at line 584 of file qbitarray.cpp.

Here is the call graph for this function:

◆ operator~()

QBitArray QBitArray::operator~ ( ) const

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

Example:

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

Definition at line 631 of file qbitarray.cpp.

Here is the call graph for this function:

◆ resize()

void QBitArray::resize ( qsizetype  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()

Definition at line 220 of file qbitarray.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setBit() [1/2]

void QBitArray::setBit ( qsizetype  i)
inline

Sets the bit at index position i to 1.

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

See also
clearBit(), toggleBit()

Definition at line 124 of file qbitarray.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setBit() [2/2]

void QBitArray::setBit ( qsizetype  i,
bool  value 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Sets the bit at index position i to value.

Definition at line 132 of file qbitarray.h.

Here is the call graph for this function:

◆ size()

qsizetype QBitArray::size ( ) const
inline

Returns the number of bits stored in the bit array.

See also
resize()

Definition at line 67 of file qbitarray.h.

Here is the caller graph for this function:

◆ swap()

void QBitArray::swap ( QBitArray other)
inlinenoexcept
Since
4.8

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

Definition at line 65 of file qbitarray.h.

Here is the call graph for this function:

◆ testBit()

bool QBitArray::testBit ( qsizetype  i) const
inline

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 < size()).

See also
setBit(), clearBit()

Definition at line 120 of file qbitarray.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toggleBit()

bool QBitArray::toggleBit ( qsizetype  i)
inline

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 < size()).

See also
setBit(), clearBit()

Definition at line 135 of file qbitarray.h.

Here is the call graph for this function:

◆ toUInt32()

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

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 quint32 and returns it, obeying endianness. If ok is not a null pointer, and the array has more than 32 bits, ok is set to false and this function returns zero; otherwise, it's set to true.

Definition at line 354 of file qbitarray.cpp.

◆ truncate()

void QBitArray::truncate ( qsizetype  pos)
inline

Truncates the bit array at index position pos.

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

See also
resize()

Definition at line 101 of file qbitarray.h.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator&()

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

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:

See also
{QBitArray::}{operator&=()}, {QBitArray::}{operator|()}, {QBitArray::}{operator^()}

Definition at line 663 of file qbitarray.cpp.

◆ operator<< [1/2]

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

Writes bit array ba to stream out.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 770 of file qbitarray.cpp.

◆ operator<<() [2/2]

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

Writes bit array ba to stream out.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 770 of file qbitarray.cpp.

◆ operator>> [1/2]

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

Reads a bit array into ba from stream in.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 795 of file qbitarray.cpp.

◆ operator>>() [2/2]

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

Reads a bit array into ba from stream in.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 795 of file qbitarray.cpp.

◆ operator^()

QBitArray operator^ ( const QBitArray a1,
const QBitArray a2 
)
related

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:

See also
{QBitArray}{operator^=()}, {QBitArray}{operator&()}, {QBitArray}{operator|()}

Definition at line 709 of file qbitarray.cpp.

◆ operator|()

QBitArray operator| ( const QBitArray a1,
const QBitArray a2 
)
related

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:

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

Definition at line 686 of file qbitarray.cpp.

◆ qHash

Q_CORE_EXPORT size_t qHash ( const QBitArray key,
size_t  seed 
)
friend

Definition at line 817 of file qhash.cpp.


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