Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

TQBitArray Class Reference

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

All the functions in this class are reentrant when TQt is built with thread support.

#include <tqbitarray.h>

Inherits TQByteArray.

List of all member functions.

Public Members

Related Functions


Detailed Description

The TQBitArray class provides an array of bits.

Because TQBitArray is a TQMemArray, it uses explicit sharing with a reference count.

A TQBitArray 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 setBit() and clearBit() because some tricks are required to implement single-bit assignments.

Example:

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

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

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

When a TQBitArray is constructed the bits are uninitialized. Use fill() to set all the bits to 0 or 1. The array can be resized with resize() and copied with copy(). Bits can be set with setBit() and cleared with clearBit(). Bits can be toggled with toggleBit(). A bit's value can be obtained with testBit() and with at().

TQBitArray supports the & (AND), | (OR), ^ (XOR) and ~ (NOT) operators.

See also Collection Classes, Implicitly and Explicitly Shared Classes, and Non-GUI Classes.


Member Function Documentation

TQBitArray::TQBitArray ()

Constructs an empty bit array.

TQBitArray::TQBitArray ( uint size )

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

See also fill().

TQBitArray::TQBitArray ( const TQBitArray & a )

Constructs a shallow copy of a.

bool TQBitArray::at ( uint index ) const

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

See also operator[]().

void TQBitArray::clearBit ( uint index )

Clears the bit at position index, i.e. sets it to 0.

See also setBit() and toggleBit().

TQBitArray TQBitArray::copy () const

Returns a deep copy of the bit array.

See also detach().

void TQBitArray::detach () [virtual]

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

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

See also copy().

Reimplemented from TQMemArray.

bool TQBitArray::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).

fill() resizes the bit array to size bits if size is nonnegative.

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

See also resize().

TQBitArray & TQBitArray::operator&= ( const TQBitArray & 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 of the two bit arrays, with any missing bits (i.e. if one array is shorter than the other), taken to be 0.

    TQBitArray 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~().

TQBitArray & TQBitArray::operator= ( const TQBitArray & a )

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

TQBitVal TQBitArray::operator[] ( int index )

Implements the [] operator for bit arrays.

The returned TQBitVal is a context object. It makes it possible to get and set a single bit value by its index position.

Example:

    TQBitArray 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 TQBitArray::operator[] ( int index ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Implements the [] operator for constant bit arrays.

TQBitArray & TQBitArray::operator^= ( const TQBitArray & 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 of the two bit arrays, with any missing bits (i.e. if one array is shorter than the other), taken to be 0.

    TQBitArray 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~().

TQBitArray & TQBitArray::operator|= ( const TQBitArray & 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 of the two bit arrays, with any missing bits (i.e. if one array is shorter than the other), taken to be 0.

    TQBitArray 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~().

TQBitArray TQBitArray::operator~ () const

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

Example:

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

bool TQBitArray::resize ( uint size )

Resizes the bit array to size bits and returns TRUE if the bit array could be resized; otherwise returns FALSE. The array becomes a null array if size == 0.

If the array is expanded, the new bits are set to 0.

See also size().

void TQBitArray::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().

void TQBitArray::setBit ( uint index )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the bit at position index to 1.

See also clearBit() and toggleBit().

uint TQBitArray::size () const

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

See also resize().

bool TQBitArray::testBit ( uint index ) const

Returns TRUE if the bit at position index is set, i.e. is 1; otherwise returns FALSE.

See also setBit() and clearBit().

bool TQBitArray::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

TQBitArray operator& ( const TQBitArray & a1, const TQBitArray & a2 )

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

The result has the length of the longest of the two bit arrays, with any missing bits (i.e. if one array is shorter than the other), taken to be 0.

See also TQBitArray::operator&=().

TQDataStream & operator<< ( TQDataStream & s, const TQBitArray & a )

Writes bit array a to stream s.

See also Format of the TQDataStream operators.

TQDataStream & operator>> ( TQDataStream & s, TQBitArray & a )

Reads a bit array into a from stream s.

See also Format of the TQDataStream operators.

TQBitArray operator^ ( const TQBitArray & a1, const TQBitArray & a2 )

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

The result has the length of the longest of the two bit arrays, with any missing bits (i.e. if one array is shorter than the other), taken to be 0.

See also TQBitArray::operator^().

TQBitArray operator| ( const TQBitArray & a1, const TQBitArray & a2 )

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

The result has the length of the longest of the two bit arrays, with any missing bits (i.e. if one array is shorter than the other), taken to be 0.

See also TQBitArray::operator|=().


This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8