Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Bits manipulating question

Bits manipulating question

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 4 Posters 8.0k Views
  • 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.
  • M Offline
    M Offline
    MarkoSiroki
    wrote on last edited by
    #1

    Hi to all!

    I've been writing some app that communicates with some device via modbus. Now, the communication works fine, but the device returns me data of type short and its structure is as follows:
    bits 0-11: specific service code
    bits 12-13: router status
    bit 14: unused, always set to 0
    bit 15: command response flag

    How do get this bits in most elegant way, QBitArray conversion from short simply does not work! I've tried to use C++ bitfields but Ido not know how to query bits!

    Sincerely,
    Marko

    EDIT: moved to C++ as it's a general C++ problem

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      you can query bits by shifting and masking:

      @
      short myBitField = 0x0F37;

      // check one bit by masking
      bool bitSet = myBitField & 0x0020;
      
      // check bit 4 bit by shift and masking
      bool bitSet = (myBitField > 3) & 0x0001;
      

      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MarkoSiroki
        wrote on last edited by
        #3

        Ok, but how do I get bits 0-11 from my scheme?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          make a bit mask:

          @
          // bit mask for bits 0 - 11
          short bitMask = 0x07FF;

          short myBitField = 0x0F37;
          
          // check one bit by masking
          short bitSet = myBitField & bitMask;
          

          @

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            You create a mask that covers all those bits. If you want to make it easier to write the masks, perhaps "Boost::binary":http://www.boost.org/doc/libs/1_42_0/libs/utility/utility.htm#BOOST_BINARY can help you.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              florent.revelut
              wrote on last edited by
              #6

              if you're stuck with bitsets, an easy way is :

              • define a bunch of macro constants, defining value for each bit
                #define BIT0 1
                #define BIT1 2
                #define BIT2 4
                ...
                (note that bitX constant = 2 power x)
              • to create the mask, use binary "or" : mask = BIT0 | BIT3 | BIT 4
              • to compare, use binary and (&) : check if (bitset & mask) != 0

              E.g. : 17 = 2^4 + 2^0 => bit 0 and bit4 set, all other unset
              check bit 0 :
              (17 & BIT0) = (17 & 1) = 1 != 0 <<<< bit 0 is set

              check bit 3 :
              (17 & BIT3) = (17 & 8) = 0 == 0 <<<< bit 3 not set

              1 Reply Last reply
              0
              • F Offline
                F Offline
                florent.revelut
                wrote on last edited by
                #7

                To control bits, you can use:

                • | (binary or ) => sets bit
                • & with negative mask => unset bit
                • not that negative mask can be done using ~ (binary inversion) : result = result & (~BIT4) <<< unsets bit 4
                1 Reply Last reply
                0

                • Login

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