How can I get QBitArray from qint value? I need to change some bits in number, so I want to use QBitArray for it.
質問する
6893 次
3 に答える
2
QBitArray's '>>' operator expects bitset size as the first 4 bytes, so JustMaximumPower's snipper won't work. Correct data stream should look like this:
QBitArray bits;
quint32 size = 32;
quint32 value = 1337;
QByteArray data;
QDataStream stream(data, QIODevice::ReadWrite);
stream << size << value;
stream.device()->seek(0);
stream >> bits;
Actually I don't find QBitArray very useful. You could try std::vector<bool> for variable size or std::bitset for fixed size (both from STL library).
std::bitset<32> bits(1337);
bits[0] = 1;
于 2015-01-12T14:15:35.030 に答える
0
Not testet but should work:
qint value = 1337;
QBitArray bits;
QDataStream stream;
stream << value;
bits << stream;
于 2012-11-13T12:17:30.707 に答える
-1
If you want to just change some bits, QBitArray
is overkill.
int bitmask = 1 << 3; // let's change 4th bit
bitmask = 0x55555555; // or perhaps all odd bits
int number = 12345678;
number |= bitmask; // set to 1s
number &= ~bitmask; // set to 0s
number ^= bitmask; // negate what's already there
于 2012-11-13T12:18:57.037 に答える