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. [Solved] Quickly test QBitArray bytes to see if all are zero

[Solved] Quickly test QBitArray bytes to see if all are zero

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.9k 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.
  • K Offline
    K Offline
    kloveridge
    wrote on last edited by
    #1

    I have a QBitArray that has a bunch of bit flags. I wonder if there is a fast way to test if all the bytes used internally in the QBitArray are zero. I can go through them one bit a time. But seems like it could be done faster if I could get at the bytes.

    Thanks

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      What about:
      @
      if (bitArray.count(true) == 0) { }
      @
      The way that is implemented should be much faster than a brute force iteration.

      Or even this:
      @
      if (bitArray == QBitArray(bitArray.size(), false)) { }
      @
      If the bit array size is fixed then you could make the RHS a const outside the if().

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

        The fasted method probably would be to run a memcmp() on the constData() of the QBitArray's internal QByteArray. But that isn't exposed to the public. So you are stuck with count() which already does quite some bit magic...

        http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kloveridge
          wrote on last edited by
          #4

          The count() function does what I needed. But still this is a not perfectly suited for what I want.

          My particular need I just wanted a super fast way to see if any bits were set. I don't need to have a count, but this will work fine.

          If anyone from Qt is listening, it would be nice if the QBitArray had a fast test to see if any bits were set.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #5

            If you exclusively modify the Bit vector through QBitArray.setBit(), you could overwrite that method and update a flag of your own. You'd only need to maintain a counter of the "1" bits that is updated at each call. Then you could check for "all bits are zero" in constant time by reading the counter.

            @void MyBitArray::setBit(int i, bool value)
            {
            if(testBit(i) != value)
            {
            if(value) m_oneCounter++; else m_oneCounter--;
            QBitArray::setBit(i, value)
            }
            }

            bool MyBitArray::isAllZeros(void)
            {
            return (m_oneCounter == 0);
            }@

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              Actually, my second option, using operator==(), will not work. It only tests that the internal pointers are the same and does not check that bits themselves.

              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