Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Struct of bits, individual members not visible?
Forum Updated to NodeBB v4.3 + New Features

Struct of bits, individual members not visible?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 2.3k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cdwijs
    wrote on last edited by
    #1

    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

    aha_1980A 1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @cdwijs not sure if it can be set in Qt Creator itself, but you may want to use an external calculator program in programmer mode, so you paste the value in hex mode and in this case Windows calculator is already showing the bits. If not, you switch to bin mode and you'll have the individual bits.

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • C cdwijs

        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

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        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.

        0_1524764533724_e3c9f069-39a0-4957-8621-88f94ff895ef-grafik.png

        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.

        Qt has to stay free or it will die.

        JonBJ C 2 Replies Last reply
        2
        • aha_1980A aha_1980

          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.

          0_1524764533724_e3c9f069-39a0-4957-8621-88f94ff895ef-grafik.png

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          aha_1980A 1 Reply Last reply
          1
          • JonBJ JonB

            @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.

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @JonB

            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.

            Qt has to stay free or it will die.

            kshegunovK JonBJ 2 Replies Last reply
            0
            • aha_1980A aha_1980

              @JonB

              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.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @aha_1980 said in Struct of bits, individual members not visible?:

              you should not use bitfields but rater somthing like

              Or QFlags or QBitArray ... the possibilities are endless ;)

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2
              • aha_1980A aha_1980

                @JonB

                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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @aha_1980

                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...?

                1 Reply Last reply
                0
                • aha_1980A aha_1980

                  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.

                  0_1524764533724_e3c9f069-39a0-4957-8621-88f94ff895ef-grafik.png

                  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.

                  C Offline
                  C Offline
                  cdwijs
                  wrote on last edited by
                  #8

                  @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/untitled1

                  On 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_1980A 1 Reply Last reply
                  0
                  • C cdwijs

                    @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/untitled1

                    On 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_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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?

                    Qt has to stay free or it will die.

                    C 1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      @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?

                      C Offline
                      C Offline
                      cdwijs
                      wrote on last edited by
                      #10

                      @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.1

                      Thanks,
                      Cedric

                      aha_1980A 1 Reply Last reply
                      0
                      • C cdwijs

                        @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.1

                        Thanks,
                        Cedric

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @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 instead uint32_t.

                        And please close this topic here. Thanks.

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        3

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved