Qt Toolkit - QBitArray Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QBitArray Class Reference

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

`#include <qbitarray.h>`

Inherits QByteArray.

List of all member functions.

## Public Members

- QBitArray ()
- QBitArray ( uint size )
- QBitArray ( const QBitArray & a )
- QBitArray& operator= ( const QBitArray & )
- uint size () const
- bool resize ( uint size )
- bool fill ( bool v, int size = -1 )
- virtual void detach ()
- QBitArray copy () const
- bool testBit ( uint index ) const
- void setBit ( uint index )
- void setBit ( uint index, bool value )
- void clearBit ( uint index )
- bool toggleBit ( uint index )
- bool at ( uint index ) const
- QBitVal operator[] ( int index )
- bool operator[] ( int index ) const
- QBitArray& operator&= ( const QBitArray & )
- QBitArray& operator|= ( const QBitArray & )
- QBitArray& operator^= ( const QBitArray & )
- QBitArray operator~ () const

## Protected Members

- array_data* newData ()
- virtual void deleteData ( array_data * d )

## Related Functions

(Note that these are not member functions.)

- QDataStream & operator>> (QDataStream & s, QBitArray & a)
- QBitArray operator| (const QBitArray & a1, const QBitArray & a2)
- QBitArray operator^ (const QBitArray & a1, const QBitArray & a2)
- QDataStream & operator<< (QDataStream & s, const QBitArray & a)
- QBitArray operator& (const QBitArray & a1, const QBitArray & a2)

## Detailed Description

The QBitArray class provides an array of bits.

QString inherits QByteArray, which is defined as QArray<char>.

Since QBitArray is a QArray, it uses explicit sharing with a reference count.

A QBitArray is a special byte array that can access individual bits and perform bit-operations (AND, OR, XOR and NOT) on entire arrays or bits.

Bits can be manipulated by the setBit() and clearBit() functions, but it is also possible to use the indexing [] operator to test and set individual bits. The [] operator is a little slower than the others, because some tricks are required to implement single-bit assignments.

Example:

        QBitArray a(3);
        a.setBit( 0 );
        a.clearBit( 1 );
        a.setBit( 2 );                      // a = [1 0 1]

        QBitArray b(3);
        b[0] = 1;
        b[1] = 1;
        b[2] = 0;                           // b = [1 1 0]

        QBitArray c;
        c = ~a & b;                         // c = [0 1 0]

## Member Function Documentation

### QBitArray::QBitArray ()

Constructs an empty bit array.

### QBitArray::QBitArray ( const QBitArray & a )

Constructs a shallow copy of a.

### QBitArray::QBitArray ( uint size )

Constructs a bit array of size bits. The bits are uninitialized.

### bool QBitArray::at ( uint index ) const

Returns the value (0 or 1) of the bit at position index.

See also operator[]().

### void QBitArray::clearBit ( uint index )

Clears the bit at position index (sets it to 0).

See also setBit() and toggleBit().

### QBitArray QBitArray::copy () const

Returns a deep copy of the bit array.

See also detach().

### void QBitArray::deleteData ( array_data * d ) `[virtual protected]`

Deletes data specific to QBitArray that extended what QGArray provided.

Reimplemented from QGArray.

### void QBitArray::detach () `[virtual]`

Detaches from shared bit array data and makes sure that this bit array is the only one referring the data.

If multiple bit arrays share common data, this bit array dereferences the data and gets a copy of the data. Nothing will be done if there is just a single reference.

See also copy().

Reimplemented from QGArray.

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

Fills the bit array with v (1's if v is TRUE, or 0's if v is FALSE).

Will resize the bit array to size bits if size is nonnegative.

Returns FALSE if a nonnegative size was specified and if the bit array could not be resized, otherwise returns TRUE.

See also resize().

### QBitArray & QBitArray::operator&= ( const QBitArray & a )

Performs the AND operation between all bits in this bit array and a. Returns a reference to this bit array.

The result has the length of the longest bit array of the two, with the bits missing from the shortest array taken as 0.

Example:

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

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

### QBitArray & QBitArray::operator= ( const QBitArray & a )

Assigns a shallow copy of a to this bit array and returns a reference to this array.

### QBitVal QBitArray::operator[] ( int index )

Implements the [] operator for bit arrays.

The returned QBitVal is a context object. It makes it possible to get and set a single bit value.

Example:

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

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

See also at().

### bool QBitArray::operator[] ( int index ) const

Implements the [] operator for constant bit arrays.

### QBitArray & QBitArray::operator^= ( const QBitArray & a )

Performs the XOR operation between all bits in this bit array and a. Returns a reference to this bit array.

The result has the length of the longest bit array of the two, with the bits missing from the shortest array taken as 0.

Example:

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

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

### QBitArray & QBitArray::operator|= ( const QBitArray & a )

Performs the OR operation between all bits in this bit array and a. Returns a reference to this bit array.

The result has the length of the longest bit array of the two, with the bits missing from the shortest array taken as 0.

Example:

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

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

### QBitArray QBitArray::operator~ () const

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

Example:

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

### bool QBitArray::resize ( uint size )

Resizes the bit array to size bits. Returns TRUE if the bit array could be resized.

When expanding the bit array, the new bits will be uninitialized.

See also size().

### void QBitArray::setBit ( uint index )

Sets the bit at position index (sets it to 1).

See also clearBit() and toggleBit().

### void QBitArray::setBit ( uint index, bool value )

Sets the bit at position index to value.

Equivalent to:

        if ( value )
            setBit( index );
        else
            clearBit( index );

See also clearBit() and toggleBit().

### uint QBitArray::size () const

Returns the size (number of bits) of the bit array.

See also resize().

### bool QBitArray::testBit ( uint index ) const

Returns TRUE if the bit at position index is set.

See also setBit() and clearBit().

### bool QBitArray::toggleBit ( uint index )

Toggles the bit at position index.

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

See also setBit() and clearBit().

## Related Functions

### QDataStream & operator>> (QDataStream & s, QBitArray & a)

Reads a bit array from a stream.

See also Format of the QDataStream operators

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

Returns the OR result between the bit arrays a1 and a2.

See also QBitArray::operator|=().

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

Returns the XOR result between the bit arrays a1 and a2.

See also QBitArray::operator^().

### QDataStream & operator<< (QDataStream & s, const QBitArray & a)

Writes a bit array to a stream.

See also Format of the QDataStream operators

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

Returns the AND result between the bit arrays a1 and a2.

See also QBitArray::operator&=().

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.