Struct of bits, individual members not visible?
-
Hi All,
I have the following struct:
struct { uint32_t driveProblem:1; uint32_t driveStatus:3; uint32_t motorOn:1; uint32_t referenceMode:1; uint32_t motorFailure:1; uint32_t unitMode:3; uint32_t gainScheduling:1; uint32_t homing:1; uint32_t programRunning:1; uint32_t currentLimit:1; uint32_t motionStatus:2; uint32_t recorderStatus:3; uint32_t hallSensors:3; uint32_t cpuStatus:1; uint32_t stoppedByLimit:1; uint32_t errorUserProgram:1; }driveState;
I can access the individual bits like this:
qDebug()<<driveState.cpuStatus; driveState.cpuStatus=1; qDebug()<<driveState.cpuStatus;
But when I set a breakpoint in the function, I can't see the bits in Qtcreator. I see this:
name Value type drivestate @0x408038 {...} cpuStatus <not accessible>
How can i see the bits in a struct?
I have to convert a uint32 into individual bits, some fields are 2 or 3 bits wide. How can I do this the best way? In C I would make a union of an uint32, and a struct, but I get the feeling this is not the best way to do it in C++.Cheers,
Cedric -
-
Hi @cdwijs,
which platform are you on? Your example is working perfectly fine on Ubuntu 16.04 with Qt 5.9.2 and Creator 4.6.0.
Regarding your second question:
I have to convert a uint32 into individual bits, some fields are 2 or 3 bits wide. How can I do this the best way? In C I would make a union of an uint32, and a struct, but I get the feeling this is not the best way to do it in C++.
I do this exactly like you said. You should be aware, that in theory the compiler is free to order the bits in the union LSB or MSB first. In practice, I've always seen LSB first and have used these structures to exchange data between Microcontrollers and Qt programs.
-
@aha_1980 , @cdwijs
I'm suspecting your "LSB first" is because you're compiling/running on an Intel chipset "little endian"? Are there still Motorola chips which are big endian these days (might be showing my age...)?See https://stackoverflow.com/questions/1490092/c-c-force-bit-field-order-and-alignment?lq=1, and say https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html, for a detailed discussion.
-
No, the LSB first applies to all compilers I use, including one for the Freescale HCS12XE Microcontroller, which is Big Endian (even if only 16 Bit).
Some compilers let you even choose the ordering.
But if you want to have very portable code, you should not use bitfields but rater somthing like
myvar |= MYBIT; myvar &= ~MYBIT; if (myvar & MYBIT) ...;
That's a bit less readable, though.
-
@aha_1980 said in Struct of bits, individual members not visible?:
you should not use bitfields but rater somthing like
Or
QFlags
orQBitArray
... the possibilities are endless ;) -
No, the LSB first applies to all compilers I use, including one for the Freescale HCS12XE Microcontroller, which is Big Endian (even if only 16 Bit).
I was looking at https://stackoverflow.com/a/1490144/489865. But some commenters seem to cast doubt on whether those results are even correct...?
-
@aha_1980 said in Struct of bits, individual members not visible?:
Hi @cdwijs,
which platform are you on? Your example is working perfectly fine on Ubuntu 16.04 with Qt 5.9.2 and Creator 4.6.0.
I am on windows 7 with QT 5.10.1 and Creator 4.6.0.
I forgot to mention I have posted the code on github:
https://github.com/cdwijs/qt-experiments/tree/master/untitled1On manjaro (arch linux) with QT 5.10.1 and Creator 4.6.0 I can see the individual bits without problems.
What's different between Windows and Linux in this case? How can I solve this?Cheers,
Cedric -
@aha_1980 said in Struct of bits, individual members not visible?:
@cdwijs on Windows you will most likely have either MSVC or MinGW compiler and corresponding debugger. can you tell us which one you are using?
windows 7 with QT 5.10.1 and Creator 4.6.0, mingw53_32
manjaro (arch linux) with QT 5.10.1 and Creator 4.6.0 gcc/x86_64-pc-linux-gnu/7.3.1
gdb 8.1Thanks,
Cedric -
@cdwijs said in Struct of bits, individual members not visible?:
windows 7 with QT 5.10.1 and Creator 4.6.0, mingw53_32
Ok, so you are using the MinGW compiler. Well, what you describe sounds like a bug; and by searching for it I found that I have reported it already some weeks ago: QTCREATORBUG-19742.
You should raise your voice there by commenting and voting. As workaround, you can maybe use
int
insteaduint32_t
.And please close this topic here. Thanks.