How to load data into QBitArray



  • Hi,
    I receive image frames with a frame header of 128 bit. I wanted to use QBitArray for the header part but I couldn't manage memcopy the data from the received buffer to QBitArray. Could anyone help me out?

    Here is what I roughly have:

    QBitArray myBitArray;
    memcpy(myBitArray.data_ptr(), myPixelBuffer, 16); //16 = 128bit
    myPixelBuffer = myPixelBuffer + 16; //Starting the buffer right after the header.


  • Lifetime Qt Champion

    Hi,

    Don't you mean a QByteArray ?



  • I think QBitArray doesn't have a method called "data_ptr()". You mean "data()"?

    Could you use QByteArray and its constructor? Only for checking the differente between your code and using QByteArray.

    QByteArray::QByteArray(const char * data, int size = -1)


  • Hi,
    I could also do it with QByteArray, however I need to do bit-wise operations on 128 bits altogether. So, I thought if I put them in QBitArray, I can shift them all easily. Do you have any suggestions?



  • @koliva Well, you can get a pointer to the data stored in the byte array and use this pointer to do the operations.

    Example:

    QByteArray ba("Hello world");
    char *data = ba.data();
    while (*data) {
        cout << "[" << *data << "]" << endl;
        ++data;
    }
    

    The idea is to use the QByteArray constructor to initialize the object with your buffer of pixels.



  • Can you also give me an example how to mask some part of the 128 bit?
    For example, how can I construct a mask which gives me the bits in between, for example, 48 and 71?



  • Can you also give me an example how to mask some part of the 128 bit?
    For example, how can I construct a mask which gives me the bits in between, for example, 48 and 71?

    @koliva It's quite simple if you use 'bitwise shifting' and 'bitwise and'. You might check out this Website. You can mask using '&' and shift it to align using '>>' or '<<'. The only problem i see here is that your processor can't quite use logic on anything higher than it's word size, which is 32 or 64 bit in modern general purpose computers. So using QBitArray doesn't seem wrong for me. If you want to get a certain area of bits from your QBitArray, you might want to use the operator[] or QByteArray::at(). Example(not tested):

    QBitArray array(128); # Initialize a new 128-bit array
    ...
    # manipulate our bitarray like we want
    ...
    # from 48 to 71
    int from = 48;
    int to = 71;
    for (int i = from; i < to; ++i)
    {
        bool bitAtI = array.at(i);
        # or use the operator [] to get a MODIFIABLE REFERENCE of the bit!
        # bool bitAtI = array[i];
    }
    

    Using this approach you will get each bit from 48 to 71. Write them out into a new QBitArray or do whatever you want with them.
    For further informations about QBitArray read: QBitArray

    One more IMPORTANT thing:

    The approach using QBitArray::at() from above will give you just a copy of the bit! If you use the operator [], you will get a modifiable reference of the bit in your QBitArray!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.