Search code examples
qtbit-manipulationbitqbytearray

Bitshifting QBytearray?


I'm looking for a way to sbitshift a QByteArray.

QByteArray statusByte = QByteArray::fromHex("40");  // 0100 0000

What i need to achieve is to get statusByte to 0x20 by bit shifting. Since I can't directly bit shift the QByteArray, what is the simplest method for achieving the shift?


Solution

  • Bit shifting is not a problem when you are talking about single byte, this is trivial (I don't know why you claim it is impossible).

    QByteArray statusByte = QByteArray::fromHex("40");
    statusByte[0] = statusByte[0]>>1;
    statusByte[0]>>=1; // this should also work
    

    if you have multiple bytes then it is more complicated!

    1. endian! How do you define shift where the oldest bit should go, to next byte or to previos byte?
    2. what should happen when array ends? loose data, or extend array?