Qt 4.8
Public Types | Public Functions | Properties | 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::DataPtr DataPtr
 

Public Functions

bool at (int i) const
 Returns the value of the bit at index position i. More...
 
void clear ()
 Clears the contents of the bit array and makes it empty. More...
 
void clearBit (int i)
 Sets the bit at index position i to 0. More...
 
int count () const
 Same as size(). More...
 
int 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. More...
 
DataPtrdata_ptr ()
 
void detach ()
 
bool fill (bool val, int size=-1)
 Sets every bit in the bit array to value, returning true if successful; otherwise returns false. More...
 
void fill (bool val, int first, int last)
 Sets bits at index positions begin up to and excluding end to value. More...
 
bool isDetached () const
 
bool isEmpty () const
 Returns true if this bit array has size 0; otherwise returns false. More...
 
bool isNull () const
 Returns true if this bit array is null; otherwise returns false. More...
 
QBitArrayoperator &= (const QBitArray &)
 
bool operator!= (const QBitArray &a) const
 Returns true if other is not equal to this bit array; otherwise returns false. More...
 
QBitArrayoperator= (const QBitArray &other)
 Assigns other to this bit array and returns a reference to this bit array. More...
 
bool operator== (const QBitArray &a) const
 Returns true if other is equal to this bit array; otherwise returns false. More...
 
QBitRef operator[] (int i)
 Returns the bit at index position i as a modifiable reference. More...
 
bool operator[] (int i) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
QBitRef operator[] (uint i)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool operator[] (uint i) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
QBitArrayoperator^= (const QBitArray &)
 Performs the XOR operation between all bits in this bit array and other. More...
 
QBitArrayoperator|= (const QBitArray &)
 Performs the OR operation between all bits in this bit array and other. More...
 
QBitArray operator~ () const
 Returns a bit array that contains the inverted bits of this bit array. More...
 
 QBitArray ()
 Constructs an empty bit array. More...
 
 QBitArray (int size, bool val=false)
 Constructs a bit array containing size bits. More...
 
 QBitArray (const QBitArray &other)
 Constructs a copy of other. More...
 
void resize (int size)
 Resizes the bit array to size bits. More...
 
void setBit (int i)
 Sets the bit at index position i to 1. More...
 
void setBit (int i, bool val)
 Sets the bit at index position i to value. More...
 
int size () const
 Returns the number of bits stored in the bit array. More...
 
void swap (QBitArray &other)
 Swaps bit array other with this bit array. More...
 
bool testBit (int i) const
 Returns true if the bit at index position i is 1; otherwise returns false. More...
 
bool toggleBit (int i)
 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). More...
 
void truncate (int pos)
 Truncates the bit array at index position pos. More...
 

Properties

QByteArray d
 

Friends

Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &, const QBitArray &)
 Writes bit array ba to stream out. More...
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QBitArray &)
 Reads a bit array into ba from stream in. More...
 
Q_CORE_EXPORT uint qHash (const QBitArray &key)
 

Related Functions

(Note that these are not member functions.)

QBitArray operator& (const QBitArray &a1, const QBitArray &a2)
 Returns a bit array that is the AND of the bit arrays a1 and a2. More...
 
QBitArray operator^ (const QBitArray &a1, const QBitArray &a2)
 Returns a bit array that is the XOR of the bit arrays a1 and a2. More...
 
QBitArray operator| (const QBitArray &a1, const QBitArray &a2)
 Returns a bit array that is the OR of the bit arrays a1 and a2. More...
 

Detailed Description

The QBitArray class provides an array of bits.

Note
This class or function is reentrant.

A QBitArray is an array that gives access to individual bits and provides 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, 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:

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

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

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. For example:

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 ]

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:

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

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, QVector

Definition at line 54 of file qbitarray.h.

Typedefs

◆ DataPtr

Warning
This function is not part of the public interface.

Definition at line 113 of file qbitarray.h.

Constructors and Destructors

◆ QBitArray() [1/3]

QBitArray::QBitArray ( )
inline

Constructs an empty bit array.

See also
isEmpty()

Definition at line 62 of file qbitarray.h.

62 {}

◆ QBitArray() [2/3]

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

Definition at line 129 of file qbitarray.cpp.

130 {
131  if (!size) {
132  d.resize(0);
133  return;
134  }
135  d.resize(1 + (size+7)/8);
136  uchar* c = reinterpret_cast<uchar*>(d.data());
137  memset(c, value ? 0xff : 0, d.size());
138  *c = d.size()*8 - size;
139  if (value && size && size % 8)
140  *(c+1+size/8) &= (1 << (size%8)) - 1;
141 }
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ QBitArray() [3/3]

QBitArray::QBitArray ( const QBitArray other)
inline

Constructs a copy of 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=()

Definition at line 64 of file qbitarray.h.

64 : d(other.d) {}
QByteArray d
Definition: qbitarray.h:59

Functions

◆ at()

bool QBitArray::at ( int  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 146 of file qbitarray.h.

Referenced by QHeaderViewPrivate::isVisualIndexHidden().

146 { return testBit(i); }
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124

◆ clear()

void QBitArray::clear ( )
inline

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

See also
resize(), isEmpty()

Definition at line 85 of file qbitarray.h.

Referenced by QXIMInputContext::ICData::clear(), and QHeaderViewPrivate::prepareSectionSelected().

85 { d.clear(); }
QByteArray d
Definition: qbitarray.h:59
void clear()
Clears the contents of the byte array and makes it empty.

◆ clearBit()

void QBitArray::clearBit ( int  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 132 of file qbitarray.h.

Referenced by QLCDNumberPrivate::internalSetString(), and xic_draw_callback().

133 { Q_ASSERT(uint(i) < uint(size()));
134  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
unsigned int uint
Definition: qglobal.h:996
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ count() [1/2]

int QBitArray::count ( ) const
inline

Same as size().

Definition at line 74 of file qbitarray.h.

Referenced by QHeaderViewPrivate::_q_layoutChanged(), QHeaderViewPrivate::prepareSectionSelected(), and QHeaderView::sectionsInserted().

74 { return (d.size() << 3) - *d.constData(); }
QByteArray d
Definition: qbitarray.h:59
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402

◆ count() [2/2]

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.

Definition at line 166 of file qbitarray.cpp.

167 {
168  int numBits = 0;
169  int len = size();
170 #if 0
171  for (int i = 0; i < len; ++i)
172  numBits += testBit(i);
173 #else
174  // See http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
175  const quint8 *bits = reinterpret_cast<const quint8 *>(d.data()) + 1;
176  while (len >= 32) {
177  quint32 v = quint32(bits[0]) | (quint32(bits[1]) << 8) | (quint32(bits[2]) << 16) | (quint32(bits[3]) << 24);
178  quint32 c = ((v & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
179  c += (((v & 0xfff000) >> 12) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
180  c += ((v >> 24) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
181  len -= 32;
182  bits += 4;
183  numBits += int(c);
184  }
185  while (len >= 24) {
186  quint32 v = quint32(bits[0]) | (quint32(bits[1]) << 8) | (quint32(bits[2]) << 16);
187  quint32 c = ((v & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
188  c += (((v & 0xfff000) >> 12) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
189  len -= 24;
190  bits += 3;
191  numBits += int(c);
192  }
193  while (len >= 0) {
194  if (bits[len / 8] & (1 << ((len - 1) & 7)))
195  ++numBits;
196  --len;
197  }
198 #endif
199  return on ? numBits : size() - numBits;
200 }
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
unsigned char quint8
Definition: qglobal.h:934
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
QByteArray d
Definition: qbitarray.h:59
unsigned int quint32
Definition: qglobal.h:938
#define Q_UINT64_C(c)
Definition: qglobal.h:941
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ data_ptr()

DataPtr & QBitArray::data_ptr ( )
inline
Warning
This function is not part of the public interface.

Definition at line 114 of file qbitarray.h.

114 { return d.data_ptr(); }
QByteArray d
Definition: qbitarray.h:59
DataPtr & data_ptr()
Definition: qbytearray.h:397

◆ detach()

void QBitArray::detach ( )
inline
Warning
This function is not part of the public interface.

Definition at line 83 of file qbitarray.h.

83 { d.detach(); }
QByteArray d
Definition: qbitarray.h:59
void detach()
Definition: qbytearray.h:435

◆ fill() [1/2]

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 default), the bit array is resized to size beforehand.

Example:

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

Definition at line 117 of file qbitarray.h.

Referenced by QHeaderViewPrivate::prepareSectionSelected(), and QHeaderView::sectionsInserted().

118 { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
QBitArray()
Constructs an empty bit array.
Definition: qbitarray.h:62
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ fill() [2/2]

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

Sets bits at index positions begin up to and excluding end to value.

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

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

Definition at line 287 of file qbitarray.cpp.

288 {
289  while (begin < end && begin & 0x7)
290  setBit(begin++, value);
291  int len = end - begin;
292  if (len <= 0)
293  return;
294  int s = len & ~0x7;
295  uchar *c = reinterpret_cast<uchar*>(d.data());
296  memset(c + (begin >> 3) + 1, value ? 0xff : 0, s >> 3);
297  begin += s;
298  while (begin < end)
299  setBit(begin++, value);
300 }
unsigned char c[8]
Definition: qnumeric_p.h:62
void setBit(int i)
Sets the bit at index position i to 1.
Definition: qbitarray.h:128
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
static const KeyPair *const end

◆ isDetached()

bool QBitArray::isDetached ( ) const
inline
Warning
This function is not part of the public interface.

Definition at line 84 of file qbitarray.h.

84 { return d.isDetached(); }
QByteArray d
Definition: qbitarray.h:59
bool isDetached() const
Definition: qbytearray.h:437

◆ isEmpty()

bool QBitArray::isEmpty ( ) const
inline

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

See also
size()

Definition at line 78 of file qbitarray.h.

Referenced by QHeaderViewPrivate::isVisualIndexHidden(), QDeclarativeVMEMetaObject::metaCall(), QHeaderView::moveSection(), QDeclarativeVMEMetaObject::registerInterceptor(), and QHeaderViewPrivate::setVisualIndexHidden().

78 { return d.isEmpty(); }
QByteArray d
Definition: qbitarray.h:59
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ isNull()

bool QBitArray::isNull ( ) const
inline

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

Example:

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

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 79 of file qbitarray.h.

79 { return d.isNull(); }
QByteArray d
Definition: qbitarray.h:59
bool isNull() const
Returns true if this byte array is null; otherwise returns false.

◆ operator &=()

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

Referenced by fill().

◆ 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 105 of file qbitarray.h.

105 { return d != a.d; }
long ASN1_INTEGER_get ASN1_INTEGER * a
QByteArray d
Definition: qbitarray.h:59

◆ operator=()

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

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

Definition at line 65 of file qbitarray.h.

65 { d = other.d; return *this; }
QByteArray d
Definition: qbitarray.h:59

◆ 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 104 of file qbitarray.h.

104 { return d == a.d; }
long ASN1_INTEGER_get ASN1_INTEGER * a
QByteArray d
Definition: qbitarray.h:59

◆ operator[]() [1/4]

QBitRef QBitArray::operator[] ( int  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:

a[0] = false;
a[1] = true;
a[2] = a[0] ^ a[1];

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 162 of file qbitarray.h.

163 { Q_ASSERT(i >= 0); return QBitRef(*this, i); }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QBitRef class is an internal class, used with QBitArray.
Definition: qbitarray.h:148

◆ operator[]() [2/4]

bool QBitArray::operator[] ( int  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 144 of file qbitarray.h.

144 { return testBit(i); }
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124

◆ operator[]() [3/4]

QBitRef QBitArray::operator[] ( uint  i)
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 164 of file qbitarray.h.

165 { return QBitRef(*this, i); }
The QBitRef class is an internal class, used with QBitArray.
Definition: qbitarray.h:148

◆ operator[]() [4/4]

bool QBitArray::operator[] ( uint  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 145 of file qbitarray.h.

145 { return testBit(i); }
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124

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

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

Definition at line 580 of file qbitarray.cpp.

581 {
582  resize(qMax(size(), other.size()));
583  uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
584  const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
585  int n = other.d.size() - 1;
586  while (n-- > 0)
587  *a1++ ^= *a2++;
588  return *this;
589 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
void resize(int size)
Resizes the bit array to size bits.
Definition: qbitarray.cpp:214
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

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

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

Definition at line 554 of file qbitarray.cpp.

555 {
556  resize(qMax(size(), other.size()));
557  uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
558  const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
559  int n = other.d.size() - 1;
560  while (n-- > 0)
561  *a1++ |= *a2++;
562  return *this;
563 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
void resize(int size)
Resizes the bit array to size bits.
Definition: qbitarray.cpp:214
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ operator~()

QBitArray QBitArray::operator~ ( ) const

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

Example:

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

Definition at line 601 of file qbitarray.cpp.

602 {
603  int sz = size();
604  QBitArray a(sz);
605  const uchar *a1 = reinterpret_cast<const uchar *>(d.constData()) + 1;
606  uchar *a2 = reinterpret_cast<uchar*>(a.d.data()) + 1;
607  int n = d.size() - 1;
608 
609  while (n-- > 0)
610  *a2++ = ~*a1++;
611 
612  if (sz && sz%8)
613  *(a2-1) &= (1 << (sz%8)) - 1;
614  return a;
615 }
long ASN1_INTEGER_get ASN1_INTEGER * a
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ resize()

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

Definition at line 214 of file qbitarray.cpp.

Referenced by QAbstractItemModel::decodeData(), QStandardItemModel::dropMimeData(), fill(), operator^=(), operator|=(), QDeclarativeVMEMetaObject::QDeclarativeVMEMetaObject(), QDeclarativeVMEMetaObject::registerInterceptor(), registerInterface(), registerType(), QHeaderView::sectionsInserted(), and xic_draw_callback().

215 {
216  if (!size) {
217  d.resize(0);
218  } else {
219  int s = d.size();
220  d.resize(1 + (size+7)/8);
221  uchar* c = reinterpret_cast<uchar*>(d.data());
222  if (size > (s << 3))
223  memset(c + s, 0, d.size() - s);
224  else if ( size % 8)
225  *(c+1+size/8) &= (1 << (size%8)) - 1;
226  *c = d.size()*8 - size;
227  }
228 }
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ setBit() [1/2]

void QBitArray::setBit ( int  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 128 of file qbitarray.h.

Referenced by QHeaderViewPrivate::_q_layoutChanged(), QDeclarativeVMEMetaObject::connectAlias(), QAbstractItemModel::decodeData(), QTableViewPrivate::drawAndClipSpans(), QStandardItemModel::dropMimeData(), fill(), QGridLayoutEngine::fillRowData(), QLCDNumberPrivate::internalSetString(), QRegExpMatchState::matchHere(), QHeaderView::moveSection(), QBitRef::operator=(), QTableView::paintEvent(), QDeclarativeVMEMetaObject::registerInterceptor(), registerInterface(), registerType(), QHeaderView::sectionsInserted(), QHeaderViewPrivate::setVisualIndexHidden(), and xic_draw_callback().

129 { Q_ASSERT(uint(i) < uint(size()));
130  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
unsigned int uint
Definition: qglobal.h:996
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ setBit() [2/2]

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

Sets the bit at index position i to value.

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 136 of file qbitarray.h.

137 { if (val) setBit(i); else clearBit(i); }
void setBit(int i)
Sets the bit at index position i to 1.
Definition: qbitarray.h:128
void clearBit(int i)
Sets the bit at index position i to 0.
Definition: qbitarray.h:132

◆ size()

int QBitArray::size ( ) const
inline

Returns the number of bits stored in the bit array.

See also
resize()

Definition at line 73 of file qbitarray.h.

Referenced by count(), fill(), QDeclarativeMetaType::isInterface(), QDeclarativeMetaType::isList(), QDeclarativeMetaType::isQObject(), operator^=(), operator|=(), operator~(), QTableView::paintEvent(), QBitArray(), qHash(), registerInterface(), registerType(), resize(), and xic_draw_callback().

73 { return (d.size() << 3) - *d.constData(); }
QByteArray d
Definition: qbitarray.h:59
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402

◆ swap()

void QBitArray::swap ( QBitArray other)
inline

Swaps bit array other with this bit array.

Since
4.8

This operation is very fast and never fails.

Definition at line 71 of file qbitarray.h.

71 { qSwap(d, other.d); }
QByteArray d
Definition: qbitarray.h:59
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181

◆ testBit()

bool QBitArray::testBit ( int  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 124 of file qbitarray.h.

Referenced by QHeaderViewPrivate::_q_layoutChanged(), QDeclarativeVMEMetaObject::connectAlias(), count(), QAbstractItemModel::decodeData(), QLCDNumberPrivate::drawString(), QStandardItemModel::dropMimeData(), QGridLayoutEngine::fillRowData(), QLCDNumberPrivate::internalSetString(), QDeclarativeMetaType::isInterface(), QDeclarativeMetaType::isList(), QDeclarativeMetaType::isQObject(), QRegExpMatchState::matchHere(), QDeclarativeVMEMetaObject::metaCall(), QHeaderView::moveSection(), QBitRef::operator bool(), QBitRef::operator!(), QTableView::paintEvent(), QLCDNumber::setDigitCount(), QDeclarativeMetaType::typeCategory(), and xic_draw_callback().

125 { Q_ASSERT(uint(i) < uint(size()));
126  return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QByteArray d
Definition: qbitarray.h:59
unsigned int uint
Definition: qglobal.h:996
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ toggleBit()

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

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 139 of file qbitarray.h.

140 { Q_ASSERT(uint(i) < uint(size()));
141  uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
142  uchar c = uchar(*p&b); *p^=b; return c!=0; }
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QByteArray d
Definition: qbitarray.h:59
unsigned char uchar
Definition: qglobal.h:994
unsigned int uint
Definition: qglobal.h:996
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

◆ truncate()

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

Definition at line 110 of file qbitarray.h.

110 { if (pos < size()) resize(pos); }
void resize(int size)
Resizes the bit array to size bits.
Definition: qbitarray.cpp:214
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73

Friends and Related Functions

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

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
QBitArray::operator&=(), operator|(), operator^()

Definition at line 636 of file qbitarray.cpp.

637 {
638  QBitArray tmp = a1;
639  tmp &= a2;
640  return tmp;
641 }
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54

◆ operator<<

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

Writes bit array ba to stream out.

See also
Format of the QDataStream operators

Definition at line 767 of file qbitarray.cpp.

768 {
769  quint32 len = ba.size();
770  out << len;
771  if (len > 0)
772  out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1);
773  return out;
774 }
QByteArray d
Definition: qbitarray.h:59
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
unsigned int quint32
Definition: qglobal.h:938
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73
int writeRawData(const char *, int len)
Writes len bytes from s to the stream.

◆ operator>>

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

Reads a bit array into ba from stream in.

See also
Format of the QDataStream operators

Definition at line 787 of file qbitarray.cpp.

788 {
789  ba.clear();
790  quint32 len;
791  in >> len;
792  if (len == 0) {
793  ba.clear();
794  return in;
795  }
796 
797  const quint32 Step = 8 * 1024 * 1024;
798  quint32 totalBytes = (len + 7) / 8;
799  quint32 allocated = 0;
800 
801  while (allocated < totalBytes) {
802  int blockSize = qMin(Step, totalBytes - allocated);
803  ba.d.resize(allocated + blockSize + 1);
804  if (in.readRawData(ba.d.data() + 1 + allocated, blockSize) != blockSize) {
805  ba.clear();
807  return in;
808  }
809  allocated += blockSize;
810  }
811 
812  int paddingMask = ~((0x1 << (len & 0x7)) - 1);
813  if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) {
814  ba.clear();
816  return in;
817  }
818 
819  *ba.d.data() = ba.d.size() * 8 - len;
820  return in;
821 }
void clear()
Clears the contents of the bit array and makes it empty.
Definition: qbitarray.h:85
const int blockSize
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
void setStatus(Status status)
Sets the status of the data stream to the status given.
QByteArray d
Definition: qbitarray.h:59
int readRawData(char *, int len)
Reads at most len bytes from the stream into s and returns the number of bytes read.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void resize(int size)
Sets the size of the byte array to size bytes.
unsigned int quint32
Definition: qglobal.h:938
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402

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

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

Definition at line 688 of file qbitarray.cpp.

689 {
690  QBitArray tmp = a1;
691  tmp ^= a2;
692  return tmp;
693 }
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54

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

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

Definition at line 662 of file qbitarray.cpp.

Referenced by QRect::QRect().

663 {
664  QBitArray tmp = a1;
665  tmp |= a2;
666  return tmp;
667 }
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54

◆ qHash

Q_CORE_EXPORT uint qHash ( const QBitArray key)
friend

Definition at line 107 of file qhash.cpp.

108 {
109  int m = bitArray.d.size() - 1;
110  uint result = hash(reinterpret_cast<const uchar *>(bitArray.d.constData()), qMax(0, m));
111 
112  // deal with the last 0 to 7 bits manually, because we can't trust that
113  // the padding is initialized to 0 in bitArray.d
114  int n = bitArray.size();
115  if (n & 0x7)
116  result = ((result << 4) + bitArray.d.at(m)) & ((1 << n) - 1);
117  return result;
118 }
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
unsigned int uint
Definition: qglobal.h:996

Properties

◆ d

QByteArray QBitArray::d
private

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