Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QBitArray Class

TheQBitArray class provides an array of bits.More...

Header:#include <QBitArray>

Note: All functions in this class arereentrant.

Public Functions

QBitArray()
QBitArray(int size, bool value = false)
QBitArray(const QBitArray & other)
boolat(int i) const
voidclear()
voidclearBit(int i)
intcount() const
intcount(bool on) const
boolfill(bool value, int size = -1)
voidfill(bool value, int begin, int end)
boolisEmpty() const
boolisNull() const
voidresize(int size)
voidsetBit(int i)
voidsetBit(int i, bool value)
intsize() const
voidswap(QBitArray & other)
booltestBit(int i) const
booltoggleBit(int i)
voidtruncate(int pos)
booloperator!=(const QBitArray & other) const
QBitArray &operator&=(const QBitArray & other)
QBitArray &operator=(const QBitArray & other)
QBitArray &operator=(QBitArray && other)
booloperator==(const QBitArray & other) const
QBitRefoperator[](int i)
booloperator[](int i) const
QBitRefoperator[](uint i)
booloperator[](uint i) const
QBitArray &operator^=(const QBitArray & other)
QBitArray &operator|=(const QBitArray & other)
QBitArrayoperator~() const

Related Non-Members

QBitArrayoperator&(const QBitArray & a1, const QBitArray & a2)
QDataStream &operator<<(QDataStream & out, const QBitArray & ba)
QDataStream &operator>>(QDataStream & in, QBitArray & ba)
QBitArrayoperator^(const QBitArray & a1, const QBitArray & a2)
QBitArrayoperator|(const QBitArray & a1, const QBitArray & a2)

Detailed Description

TheQBitArray class provides an array of bits.

AQBitArray is an array that gives access to individual bits and provides operators (AND,OR,XOR, andNOT) that work on entire arrays of bits. It usesimplicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data.

The following code constructs aQBitArray containing 200 bits initialized to false (0):

QBitArray ba(200);

To initialize the bits to true, either passtrue as second argument to the constructor, or callfill() 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:

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

For technical reasons, it is more efficient to usetestBit() andsetBit() to access bits in the array than operator[](). For example:

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. 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. Anull bit array is a bit array that is initialized usingQBitArray's default constructor. Anempty 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 trueQBitArray().isEmpty();// returns trueQBitArray(0).isNull();// returns falseQBitArray(0).isEmpty();// returns trueQBitArray(3).isNull();// returns falseQBitArray(3).isEmpty();// returns false

All functions exceptisNull() treat null bit arrays the same as empty bit arrays; for example,QBitArray() compares equal toQBitArray(0). We recommend that you always useisEmpty() and avoidisNull().

See alsoQByteArray andQVector.

Member Function Documentation

QBitArray::QBitArray()

Constructs an empty bit array.

See alsoisEmpty().

QBitArray::QBitArray(int size,bool value = false)

Constructs a bit array containingsize bits. The bits are initialized withvalue, which defaults to false (0).

QBitArray::QBitArray(constQBitArray & other)

Constructs a copy ofother.

This operation takesconstant time, becauseQBitArray isimplicitly shared. This makes returning aQBitArray from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takeslinear time.

See alsooperator=().

bool QBitArray::at(int i) const

Returns the value of the bit at index positioni.

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

See alsooperator[]().

void QBitArray::clear()

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

See alsoresize() andisEmpty().

void QBitArray::clearBit(int i)

Sets the bit at index positioni to 0.

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

See alsosetBit() andtoggleBit().

int QBitArray::count() const

Same assize().

int QBitArray::count(bool on) const

Ifon is true, this function returns the number of 1-bits stored in the bit array; otherwise the number of 0-bits is returned.

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

Sets every bit in the bit array tovalue, returning true if successful; otherwise returns false. Ifsize is different from -1 (the default), the bit array is resized tosize 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 alsoresize().

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

This is an overloaded function.

Sets bits at index positionsbegin up to and excludingend tovalue.

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

bool QBitArray::isEmpty() const

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

See alsosize().

bool QBitArray::isNull() const

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

Example:

QBitArray().isNull();// returns trueQBitArray(0).isNull();// returns falseQBitArray(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 usingisEmpty().

See alsoisEmpty().

void QBitArray::resize(int size)

Resizes the bit array tosize bits.

Ifsize is greater than the current size, the bit array is extended to make itsize bits with the extra bits added to the end. The new bits are initialized to false (0).

Ifsize is less than the current size, bits are removed from the end.

See alsosize().

void QBitArray::setBit(int i)

Sets the bit at index positioni to 1.

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

See alsoclearBit() andtoggleBit().

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

This is an overloaded function.

Sets the bit at index positioni tovalue.

int QBitArray::size() const

Returns the number of bits stored in the bit array.

See alsoresize().

void QBitArray::swap(QBitArray & other)

Swaps bit arrayother with this bit array. This operation is very fast and never fails.

This function was introduced in Qt 4.8.

bool QBitArray::testBit(int i) const

Returns true if the bit at index positioni is 1; otherwise returns false.

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

See alsosetBit() andclearBit().

bool QBitArray::toggleBit(int i)

Inverts the value of the bit at index positioni, 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 alsosetBit() andclearBit().

void QBitArray::truncate(int pos)

Truncates the bit array at index positionpos.

Ifpos is beyond the end of the array, nothing happens.

See alsoresize().

bool QBitArray::operator!=(constQBitArray & other) const

Returns true ifother is not equal to this bit array; otherwise returns false.

See alsooperator==().

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

Performs the AND operation between all bits in this bit array andother. 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 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 alsooperator&(),operator|=(),operator^=(), andoperator~().

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

Assignsother to this bit array and returns a reference to this bit array.

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

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

Returns true ifother is equal to this bit array; otherwise returns false.

See alsooperator!=().

QBitRef QBitArray::operator[](int i)

Returns the bit at index positioni as a modifiable reference.

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

Example:

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

The return value is of type QBitRef, a helper class forQBitArray. When you get an object of type QBitRef, you can assign to it, and the assignment will apply to the bit in theQBitArray from which you got the reference.

The functionstestBit(),setBit(), andclearBit() are slightly faster.

See alsoat(),testBit(),setBit(), andclearBit().

bool QBitArray::operator[](int i) const

This is an overloaded function.

QBitRef QBitArray::operator[](uint i)

This is an overloaded function.

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

This is an overloaded function.

QBitArray & QBitArray::operator^=(constQBitArray & other)

Performs the XOR operation between all bits in this bit array andother. 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 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 alsooperator^(),operator&=(),operator|=(), andoperator~().

QBitArray & QBitArray::operator|=(constQBitArray & other)

Performs the OR operation between all bits in this bit array andother. 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 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 alsooperator|(),operator&=(),operator^=(), andoperator~().

QBitArray QBitArray::operator~() const

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

Example:

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

See alsooperator&(),operator|(), andoperator^().

Related Non-Members

QBitArrayoperator&(constQBitArray & a1, constQBitArray & a2)

Returns a bit array that is the AND of the bit arraysa1 anda2.

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

QDataStream &operator<<(QDataStream & out, constQBitArray & ba)

Writes bit arrayba to streamout.

See alsoFormat of the QDataStream operators.

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

Reads a bit array intoba from streamin.

See alsoFormat of the QDataStream operators.

QBitArrayoperator^(constQBitArray & a1, constQBitArray & a2)

Returns a bit array that is the XOR of the bit arraysa1 anda2.

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

QBitArrayoperator|(constQBitArray & a1, constQBitArray & a2)

Returns a bit array that is the OR of the bit arraysa1 anda2.

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

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp