# Problem with bitmasking and quint64.

• Hello Again!!

Now i have an issue with bit-masking. I cannot find a way to pass between classes and functions an array with 8 elements with the CAN frame data, so i worked around , i putted the data in a quint64. 8 bytes=64bits. then i pass the quint64 between classes and function without problem.
@quint16 CANdatabank::BitsToUINT16(quint8 bitstart, quint8 bitend, quint64 data)
{
quint8 bits;

``````// Convert from quint64 to Bit Array
for(int i=0; i<64; ++i){
int f=i; bool bit = (data&(1<<(63-i)));
if(bit) bits[f]=1; else bits[f]=0;
}
quint16 dec=0;
for(int i=bitstart; i<bitend;i++) {
//dec += pow(2,i)*bits[i];
dec |= ((bits[i])<<i);
}
return dec;
``````

}@
With this function i try to make the reverse conversion but dont work properly!
when the function Converts from quint64 to Bit Array then i dont have a bit array of the 64 bit integer but i have 2x 32bit integers.
for example:
64bit 0b000000000000000000000000000000000000000000000000000000000111 is this snake and the result is this
64bit 0b000000000000000000000000000001110000000000000000000000000111

can someone help ?

• Hi,

What about "QBitArray":http://doc.qt.io/qt-5/qbitarray.html ?

• [quote author="SGaist" date="1420751366"]Hi,

What about "QBitArray":http://doc.qt.io/qt-5/qbitarray.html ?[/quote]

I have tried this but i prefer the classic mode with 01, the QBITARRAY its an array with true and false states and i need this code
@dec |= ((bits[i])<<i);@
My problem was to pass the array to the other classes, if i can pass the array i can also use the array with bits!!
The problem exists after the reverse conversion!

• I guess I'm curious...

You could still pass the QBitArray to and from other code.

As far as the needed code, either a method or an operator would be created that would work with QBitArray. IE you could derive from it if needed.

Might be off base but it seems to me it does almost everything else you want and is designed for this purpose.

• Perhaps I have missed something. It seems you want a function to extract (up to 16 bits) from bitStart to bitEnd from the middle of a 64-bit integer. You should get close with something like:
@
Q_ASSERT(bitEnd < 64);
Q_ASSERT(bitStart <= bitEnd);
const int bitCount = bitEnd - bitStart + 1;
Q_ASSERT(bitCount <= 16);
const int mask = (1 << bitCount) - 1;
quint16 result = ((data >> bitStart) & mask);
@
You may need to reverse the sense of the start and end depending on which way the bits are counted.

• [quote author="ChrisW67" date="1420773453"]Perhaps I have missed something. It seems you want a function to extract (up to 16 bits) from bitStart to bitEnd from the middle of a 64-bit integer. You should get close with something like:

You may need to reverse the sense of the start and end depending on which way the bits are counted.[/quote]

This is an other subject, i understand that you have a better solution.
But if i cannot find a way to recalculate the correct 64bit unit, then every thing it;s waste time.