Qx v0.5.7
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::BitArray Class Reference

The BitArray class is a more robust variant of QBitArray, which provides an array of bits. More...

#include <qx/core/qx-bitarray.h>

Collaboration diagram for Qx::BitArray:
[legend]

Public Member Functions

 BitArray ()
 
 BitArray (int size, bool value=false)
 
void append (bool bit=false)
 
BitArray operator+ (BitArray rhs)
 
void operator+= (const BitArray &rhs)
 
BitArray operator<< (int n)
 
void operator<<= (int n)
 
BitArray operator>> (int n)
 
void operator>>= (int n)
 
void replace (const BitArray &bits, int start=0, int length=-1)
 
template<typename T >
requires std::integral<T>
void replace (T integer, int start=0, int length=-1)
 
BitArray subArray (int start, int length=-1)
 
BitArray takeFromEnd (int length=-1)
 
BitArray takeFromStart (int length=-1)
 
QByteArray toByteArray (QSysInfo::Endian endianness=QSysInfo::BigEndian)
 
template<typename T >
requires std::integral<T>
toInteger ()
 
- Public Member Functions inherited from QBitArray
 QBitArray (const QBitArray &other)
 
 QBitArray (QBitArray &&other)
 
 QBitArray (qsizetype size, bool value)
 
bool at (qsizetype i) const const
 
const char * bits () const const
 
void clear ()
 
void clearBit (qsizetype i)
 
qsizetype count () const const
 
qsizetype count (bool on) const const
 
void fill (bool value, qsizetype begin, qsizetype end)
 
bool fill (bool value, qsizetype size)
 
bool isEmpty () const const
 
bool isNull () const const
 
bool operator!= (const QBitArray &other) const const
 
QBitArray operator& (const QBitArray &a1, const QBitArray &a2)
 
QBitArrayoperator&= (const QBitArray &other)
 
QDataStreamoperator<< (QDataStream &out, const QBitArray &ba)
 
QBitArrayoperator= (const QBitArray &other)
 
QBitArrayoperator= (QBitArray &&other)
 
bool operator== (const QBitArray &other) const const
 
QDataStreamoperator>> (QDataStream &in, QBitArray &ba)
 
QBitRef operator[] (qsizetype i)
 
bool operator[] (qsizetype i) const const
 
QBitArray operator^ (const QBitArray &a1, const QBitArray &a2)
 
QBitArrayoperator^= (const QBitArray &other)
 
QBitArray operator| (const QBitArray &a1, const QBitArray &a2)
 
QBitArrayoperator|= (const QBitArray &other)
 
QBitArray operator~ () const const
 
void resize (qsizetype size)
 
void setBit (qsizetype i)
 
void setBit (qsizetype i, bool value)
 
qsizetype size () const const
 
void swap (QBitArray &other)
 
bool testBit (qsizetype i) const const
 
bool toggleBit (qsizetype i)
 
quint32 toUInt32 (QSysInfo::Endian endianness, bool *ok) const const
 
void truncate (qsizetype pos)
 

Static Public Member Functions

template<typename T >
requires std::integral<T>
static BitArray fromInteger (const T &integer)
 
- Static Public Member Functions inherited from QBitArray
QBitArray fromBits (const char *data, qsizetype size)
 

Detailed Description

BitArray derives from QBitArray and therefore shares all of its functionality, but this Qx variant provides additional data manipulation and conversion methods that are missing from its base class.

The implementation of this class assumes that it contents will be stored and manipulated in big-endian order when working with instances that may represent sets of bytes.

Constructor & Destructor Documentation

◆ BitArray() [1/2]

Qx::BitArray::BitArray ( )

Constructs an empty bit array.

◆ BitArray() [2/2]

Qx::BitArray::BitArray ( int size,
bool value = false )

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

Member Function Documentation

◆ append()

void Qx::BitArray::append ( bool bit = false)

Appends a 0 or 1 onto the end of this bit array depending on the value of bit.

◆ fromInteger()

template<typename T >
requires std::integral<T>
BitArray Qx::BitArray::fromInteger ( const T & integer)
inlinestatic

Converts the primitive integer to a bit array, with the resultant contents encoded in big-endian format if a multibyte integer type is provided.

ba.at(0); // 0
ba.at(1); // 0
ba.at(2); // 1
ba.at(3); // 0
ba.at(4); // 1
ba.at(5); // 0
ba.at(6); // 0
ba.at(7); // 0
ba.at(8); // 0
ba.at(9); // 0
ba.at(10); // 0
ba.at(11); // 0
ba.at(12); // 0
ba.at(13); // 0
ba.at(14); // 0
ba.at(15); // 0

◆ operator+()

BitArray Qx::BitArray::operator+ ( BitArray rhs)

Returns a bit array which is the result of concatenating this bit array and rhs.

◆ operator+=()

void Qx::BitArray::operator+= ( const BitArray & rhs)

Appends rhs onto the end of the bit array.

◆ operator<<()

BitArray Qx::BitArray::operator<< ( int n)

Returns a new bit array with the contents of the original shifted left n times.

◆ operator<<=()

void Qx::BitArray::operator<<= ( int n)

Left shifts the contents of the bit array n times.

◆ operator>>()

BitArray Qx::BitArray::operator>> ( int n)

Returns a new bit array with the contents of the original shifted right n times.

◆ operator>>=()

void Qx::BitArray::operator>>= ( int n)

Right shifts the contents of the bit array n times.

◆ replace() [1/2]

void Qx::BitArray::replace ( const BitArray & bits,
int start = 0,
int length = -1 )

Replace at most length bits beginning at index start with bits.

A value of -1 for length will result in the replacement only being limited by the size of the bit array.

◆ replace() [2/2]

template<typename T >
requires std::integral<T>
void Qx::BitArray::replace ( T integer,
int start = 0,
int length = -1 )
inline

Replace at most length bits at index start with the bits that make up integer.

A value of -1 for length will result in the replacement only being limited by the size of the bit array.

See also
fromInteger().

◆ subArray()

BitArray Qx::BitArray::subArray ( int start,
int length = -1 )

Returns a new bit array that contains length bits from the original, beggining at start.

A value of -1 for length will result all bits from start to the end of the array being included.

◆ takeFromEnd()

BitArray Qx::BitArray::takeFromEnd ( int length = -1)

Removes length bits from the end of the array and returns them

A value of -1 for length will result in all bits being included.

◆ takeFromStart()

BitArray Qx::BitArray::takeFromStart ( int length = -1)

Removes length bits from the start of the array and returns them

A value of -1 for length will result in all bits being included.

◆ toByteArray()

QByteArray Qx::BitArray::toByteArray ( QSysInfo::Endian endianness = QSysInfo::BigEndian)

Constructs and returns a QByteArray using the contents of the bit array and endianness.

Each group of 8 bits within the bit array are converted to the equivalent byte in order starting from index position 0 and inserted into the returned byte array in the same order if endianness equals QSysInfo::BigEndian, or the reverse order if endianness equals QSysInfo::LittleEndian.

If the bit array's length is not evenly divisible by 8, the conversion is performed as if the end of the array was padded with zeroes until it would be.

Warning
The accuracy of the provided endianness relies on the contents of the bit array being in big-endian order, since using QSysInfo::LittleEndian simply reverses the resultant byte array's byte order.

◆ toInteger()

template<typename T >
requires std::integral<T>
T Qx::BitArray::toInteger ( )
inline

Converts the contents of the bit array to an integer, with the contents interpreted as being in big-endian format if a multibyte integer type is specified.

If the specified integer type is not large enough to store the result, as many bits as possible will be used in the conversion instead.

If the bit array's length is not evenly divisible by 8, the conversion is performed as if the end of the array was padded with zeroes until it would be.


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