How to handle individual bits using QBitArray in Qt
This code snippet demonstrates how to handle individual bits in Qt using QBitArray.
QBitArray is an array of the bits, each of which can be handled separately. It also supports bitwise AND, OR, NOT, XOR operation similar to standard C++.
Article Metadata
Tested with
Devices(s): Emulator
Compatibility
Platform(s): Qt
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QBitArray
Created: james1980
(26 Jan 2009)
Last edited: hamishwillee
(11 Oct 2012)
Preconditions
- Download and install the Qt SDK
Various Function
- Replaces len bytes from index position pos with the byte array after, and returns a reference to this byte array.
QByteArray x("Say yes!");
QByteArray y("no");
x.replace(4, 3, y);
- Sets the byte array to the printed value of n in base base (10 by default) and returns a reference to the byte array. The base can be any value between 2 and 36.
QByteArray ba; int n = 63; ba.setNum(n); ba.setNum(n, 16);
- Returns the number of bytes in this byte array.
QByteArray ba("Hello");
int n = ba.size();
ba.data()[0];
ba.data()[4];
ba.data()[5];
Source File
More About QBitArray visit:http://pepper.troll.no/s60prereleases/doc/qbytearray.html
#include <QApplication>
#include <QBitArray>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *win = new QWidget;
QBitArray x(15,true); // Create a bit arrray x of the size 15 and set all the bits. Second argument is optional.
// Second way to initialize a bit array
QBitArray a;
a.resize(2);
a[0] = false;
a[1] = true;
// Third way to initialize a bit array
QBitArray a(2);
a.setBit(0, false);
a.setBit(1, true);
QBitArray x(4);
x.setBit(2, true);
// x: [ 0, 0, 1, 0 ]
QBitArray y(4);
y.setBit(3, true);
// y: [ 0, 0, 0, 1 ]
x |= y;//OR operation
// x: [ 0, 0, 1, 1 ]
x &= y; // AND operation
// x: [ 0, 0, 0, 1 ]
x ^= y; // XOR operation
// x: [ 0, 0, 0, 0 ]
x = ~x; // NOT operation
// x: [ 1, 1, 1, 1 ]
}


14 Sep
2009
This article illustrates the basic fundamental of Digital electronics.you can handle individual bits using This article.so Using this article you can make plenty of other applications.
Hear that is shown that hear we are using QBitArray for the array operation.This operation is same as C or C++.
using This article you can perform different logical operation like AND,OR and NOT.THis three are Universal GATE.so using this three operation you can make any logical operation in QT.According to me This article is very useful.
29 Sep
2009
This article demonstrates a great use of QBitArray API. And This class creates an array of bits and is used to perform some logical operations on those bits and also puts flexibility of using QBitArray Class. This piece of writing also explains some basic functions of QByteArray like replace, setNum, size etc.. And also a code snippet is given and it will be very much constructive to beginners.