PDA

View Full Version : Represent a bit vector



danilodsp
20th October 2010, 02:48
Hello people,

I'm looking to manipulate a vector of bits, but some basic types like QBitArray, QByteArray not allow me to conversion string and be shown on the label.
I also tried to use QVector.

What do you recommend?

tbscope
20th October 2010, 04:37
Some inspiration:
http://developer.qt.nokia.com/wiki/WorkingWithRawData

ChrisW67
20th October 2010, 07:04
QBitArray ba(10);
ba.setBit(5, true);
ba.setBit(7, true);

QString text;
for (int i = 0; i < ba.size(); ++i)
text += ba.testBit(i) ? "1": "0";
qDebug() << text;
You might like to reverse bit order.

If you are manipulating a small number of bits then direct use of an unsigned int is also a possibility.


unsigned int bitmap = 0;
bitmap |= 1 << 5;
bitmap |= 1 << 7;
qDebug() << QString::number(bitmap, 2);

danilodsp
20th October 2010, 16:10
Thanks guys.