CopperSpice API  1.9.1
QBitArray Class Reference

Stores an array of bits. More...

Public Methods

 QBitArray ()
 
 QBitArray (const QBitArray &other)
 
 QBitArray (int size, bool value=false)
 
bool at (int i) const
 
void clear ()
 
void clearBit (int i)
 
int count () const
 
int count (bool on) const
 
void fill (bool value, int begin, int end)
 
bool fill (bool value, int size=-1)
 
bool isEmpty () const
 
bool isNull () const
 
bool operator!= (const QBitArray &other) const
 
QBitArray & operator&= (const QBitArray &other)
 
QBitArray & operator= (const QBitArray &other)
 
QBitArray & operator= (QBitArray &&other)
 
bool operator== (const QBitArray &other) const
 
QBitRef operator[] (int i)
 
bool operator[] (int i) const
 
QBitRef operator[] (uint i)
 
bool operator[] (uint i) const
 
QBitArray & operator^= (const QBitArray &other)
 
QBitArray & operator|= (const QBitArray &other)
 
QBitArray operator~ () const
 
void resize (int size)
 
void setBit (int i)
 
void setBit (int i, bool value)
 
int size () const
 
void swap (QBitArray &other)
 
bool testBit (int i) const
 
bool toggleBit (int i)
 
void truncate (int pos)
 

Friends

QDataStreamoperator<< (QDataStream &stream, const QBitArray &bitArray)
 
QDataStreamoperator>> (QDataStream &stream, QBitArray &bitArray)
 

Related Functions

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)
 

Detailed Description

The QBitArray class stores an array of bits which provides access to individual bits and 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. 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.

ba.resize(3);
ba[0] = true;
ba[1] = false;
ba[2] = true;

It is more efficient to use testBit() and setBit() to access bits in the array than operator[]().

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.

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 ]

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 however an empty bit array is not 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

The only method which distinguishes between null and empty is isNull(). For example, QBitArray() compares equal to QBitArray(0). You should normally use isEmpty() instead of isNull().

See also
QByteArray, QVector

Constructor & Destructor Documentation

QBitArray::QBitArray ( )
inline

Constructs an empty bit array.

See also
isEmpty()
QBitArray::QBitArray ( int  size,
bool  value = false 
)
explicit

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

QBitArray::QBitArray ( const QBitArray &  other)
inline

Copy constructs a new QBitArray from other.

This operation takes constant time, because QBitArray is 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 linear time.

See also
operator=()

Method Documentation

bool QBitArray::at ( int  i) const
inline

Returns the value of the bit at index position i. The value for i must be a valid index position in the bit array.

See also
operator[]()
void QBitArray::clear ( )
inline

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

See also
resize(), isEmpty()
void QBitArray::clearBit ( int  i)
inline

Sets the bit at index position i to 0. The value for i must be a valid index position in the bit array.

See also
setBit(), toggleBit()
int QBitArray::count ( ) const
inline

Equivalent to calling size().

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 
)

Sets bits at index positions begin up to and excluding end to value. The value for begin and end must be valid index positions in the bit array.

bool QBitArray::fill ( bool  value,
int  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 bit array is resized to the value of size beforehand.

QBitArray ba(8);
ba.fill(true); // ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
ba.fill(false, 2); // ba: [ 0, 0 ]
See also
resize()
bool QBitArray::isEmpty ( ) const
inline

Returns true if this bit array has a size of 0, otherwise returns false.

See also
size()
bool QBitArray::isNull ( ) const
inline

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

QBitArray().isNull(); // returns true
QBitArray(0).isNull(); // returns false
QBitArray(3).isNull(); // returns false

CopperSpice makes a distinction between null bit arrays and empty bit arrays. 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()
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==()
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.

QBitArray a(3);
QBitArray b(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
operator&(), operator|=(), operator^=(), operator~()
QBitArray & QBitArray::operator= ( const QBitArray &  other)
inline

Copy assigns from other and returns a reference to this object.

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

Move assigns from other and returns a reference to this object.

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

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

See also
operator!=()
QBitRef QBitArray::operator[] ( int  i)
inline

Returns a reference to the data at index position i. The value for i must be a valid index position in the bit array.

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.

QBitArray a(3);
a[0] = false;
a[1] = true;
a[2] = a[0] ^ a[1];

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

See also
at(), testBit(), setBit(), clearBit()
bool QBitArray::operator[] ( int  i) const
inline

Refer to operator[]().

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

Refer to operator[]().

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

Refer to 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.

QBitArray a(3);
QBitArray b(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
operator^(), operator&=(), operator|=(), 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.

QBitArray a(3);
QBitArray b(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
operator|(), operator&=(), operator^=(), operator~()
QBitArray QBitArray::operator~ ( ) const

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

QBitArray a(3);
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b = ~a; // b: [ 0, 1, 0 ]
See also
operator&(), operator|(), operator^()
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()
void QBitArray::setBit ( int  i)
inline

Sets the bit at index position i to 1. The value for i must be a valid index position in the bit array.

See also
clearBit(), toggleBit()
void QBitArray::setBit ( int  i,
bool  value 
)
inline

Sets the bit at index position i to value.

int QBitArray::size ( ) const
inline

Returns the number of bits stored in the bit array.

See also
resize()
void QBitArray::swap ( QBitArray &  other)
inline

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

bool QBitArray::testBit ( int  i) const
inline

Returns true if the bit at index position i is 1, otherwise returns false. The value for i must be a valid index position in the bit array.

See also
setBit(), clearBit()
bool QBitArray::toggleBit ( int  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). The value for i must be a valid index position in the bit array.

If the previous value was 0 then the new value will be 1. If the previous value was 1, the new value will be 0.

See also
setBit(), clearBit()
void QBitArray::truncate ( int  pos)
inline

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

See also
resize()

Friends And Related Function Documentation

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.

QBitArray a(3);
QBitArray b(2);
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
c = a & b; // c: [ 1, 0, 0 ]
See also
operator&(), operator|(), operator^()
QDataStream & operator<< ( QDataStream stream,
const QBitArray &  bitArray 
)
friend

Writes the given bitArray to the stream. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream stream,
QBitArray &  bitArray 
)
friend

Reads from the stream into the given bitArray. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

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.

QBitArray a(3);
QBitArray b(2);
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
c = a ^ b; // c: [ 0, 1, 1 ]
See also
QBitArray::operator^=(), operator&(), 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.

QBitArray a(3);
QBitArray b(2);
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
c = a | b; // c: [ 1, 1, 1 ]
See also
QBitArray::operator|=(), operator&(), operator^()