Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What I should Use...bitset, vector &LT bool&GT, or bit_vector?

What I should Use...bitset, vector &LT bool&GT, or bit_vector?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdesigndata-structuresquestionlearning
3 Posts 2 Posters 0 Views 1 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.
  • J Offline
    J Offline
    JohnnyG
    wrote on last edited by
    #1

    I've been trying to read up on some options of what I might be able to use. Perhaps, it can be better explained if I tell you what I would like to do. I was hoping for a bit oriented class that not only provided storage on a per bit basis but also had the capability to provide an actual value. If you have binary data say in the form of messages in various formats, some of the binary data may use a single bit or a group of bits. In other words, a single bit is really a bit flag or boolean true or false, while a group of bits is a value. Of course, the groups of bits and bit flags may actually be intermixed say in a 16-bit word. I've got 16-bit words (comprised of bit flags and bit field groups). It would be kind of nice to group the bits into bit fields and flags, e.g. say a 16-bit field comprised of three 4-bit fields, a 2-bit field and two single bit flags. I would like to make the contents of these bit fields somewhat human readable by decoding the binary in the same left to right order as they are stored in binary. We typically do this by using structures. This works fine for decoding but I would like to do something different. If I could wrap these bit fields and flags into groups within an array or individual objects, I then would have the capability to name or label each object, bit field or group, and perform comparisons on similar data. In other words what may be a 4-bit field in one message may be a 3-bit field in another type of message, by writing a conversion routine between the two types, I still can make a similar comparison and not an exact comparison. I'd have to design my own class but if I inherit from one of the previously mentioned classes, which one do you think would be best? For now, I have fixed data sizes but may need more flexible and unknown (until runtime) data sizes in the future. I'm not sure if I would want to wrap all of the bitfields or groups of bits that comprise a single message into a single object that would have a message type as a data member but I think I would like to.

    S 1 Reply Last reply
    0
    • J JohnnyG

      I've been trying to read up on some options of what I might be able to use. Perhaps, it can be better explained if I tell you what I would like to do. I was hoping for a bit oriented class that not only provided storage on a per bit basis but also had the capability to provide an actual value. If you have binary data say in the form of messages in various formats, some of the binary data may use a single bit or a group of bits. In other words, a single bit is really a bit flag or boolean true or false, while a group of bits is a value. Of course, the groups of bits and bit flags may actually be intermixed say in a 16-bit word. I've got 16-bit words (comprised of bit flags and bit field groups). It would be kind of nice to group the bits into bit fields and flags, e.g. say a 16-bit field comprised of three 4-bit fields, a 2-bit field and two single bit flags. I would like to make the contents of these bit fields somewhat human readable by decoding the binary in the same left to right order as they are stored in binary. We typically do this by using structures. This works fine for decoding but I would like to do something different. If I could wrap these bit fields and flags into groups within an array or individual objects, I then would have the capability to name or label each object, bit field or group, and perform comparisons on similar data. In other words what may be a 4-bit field in one message may be a 3-bit field in another type of message, by writing a conversion routine between the two types, I still can make a similar comparison and not an exact comparison. I'd have to design my own class but if I inherit from one of the previously mentioned classes, which one do you think would be best? For now, I have fixed data sizes but may need more flexible and unknown (until runtime) data sizes in the future. I'm not sure if I would want to wrap all of the bitfields or groups of bits that comprise a single message into a single object that would have a message type as a data member but I think I would like to.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      JohnnyG wrote:

      if I inherit from one of the previously mentioned classes, which one do you think would be best?

      JohnnyG wrote:

      may need more flexible and unknown (until runtime) data sizes

      I'd alter each of your fields to be a method (rather than a data member) and extract it using bit-field extraction. For instance, say we have a field 'A':

      class MyBitFields
      {
      public:
      MyBitFields(uint16_t compositeValue) : value_(compositeValue)
      {
      }

      void SetA(unsigned int offset, unsigned int size) { aOffset_ = offset; aSize_ = size; }
      unsigned int getA() { return (value_>>aOffset_) & ((1<<aSize_)-1); }
      private:
      uint16_t value_;
      uint8_t aOffset_;
      uint8_t aSize_;
      };

      ?

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      J 1 Reply Last reply
      0
      • S Stuart Dootson

        JohnnyG wrote:

        if I inherit from one of the previously mentioned classes, which one do you think would be best?

        JohnnyG wrote:

        may need more flexible and unknown (until runtime) data sizes

        I'd alter each of your fields to be a method (rather than a data member) and extract it using bit-field extraction. For instance, say we have a field 'A':

        class MyBitFields
        {
        public:
        MyBitFields(uint16_t compositeValue) : value_(compositeValue)
        {
        }

        void SetA(unsigned int offset, unsigned int size) { aOffset_ = offset; aSize_ = size; }
        unsigned int getA() { return (value_>>aOffset_) & ((1<<aSize_)-1); }
        private:
        uint16_t value_;
        uint8_t aOffset_;
        uint8_t aSize_;
        };

        ?

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        J Offline
        J Offline
        JohnnyG
        wrote on last edited by
        #3

        That's an interesting idea and may be exactly what I need since I am not intending to store every binary data value read from a file into the MyBitFields class permanently but rather to help me decode and perhaps to compare. I also could have a data member inside of MyBitFields and even have conversion modifiers inside of the class. Additionally, I could also create an array of MyBitFields, one array per message Thanks for the info! :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups