By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,067 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

bitbuilder class.

P: 7
Well, I have been searching for one but as I come up short I decided to start one myself. Base idea is that I need to set bit for bit with values and then send it over a protocol. I have some problems setting more then one unsigned char of data although I have set it to bigger size. Here is the base class nothing fancy.
Expand|Select|Wrap|Line Numbers
  1.     bitbuilder::bitbuilder(int size)
  2.     {
  3.         this->m_pdata = new unsigned char[size];
  4.         for(int i = 0; i < size; i++)
  5.         {
  6.             this->m_pdata[i] = 0;
  7.         }
  8.         this->m_position = 0;
  9.     }
  10.     int bitbuilder::addBits(int value, int numberOfBits)
  11.     {
  12.         int andoperator;
  13.         switch(numberOfBits)
  14.         {
  15.         case 1:
  16.             andoperator = 0x01;
  17.             break;
  18.         case 2:
  19.             andoperator = 0x03;
  20.             break;
  21.         case 3:
  22.             andoperator = 0x07;
  23.             break;
  24.         case 4:
  25.             andoperator = 0x0F;
  26.             break;
  27.         case 5:
  28.             andoperator = 0x1F;
  29.             break;
  30.         case 6:
  31.             andoperator = 0x3F;
  32.             break;
  33.         case 7:
  34.             andoperator = 0x7F;
  35.             break;
  36.         case 8:
  37.             andoperator = 0xFF;
  38.             break;
  39.         default:
  40.             break;
  41.         }
  42.         value &= andoperator;
  43.         *this->m_pdata |= (value << this->m_position);
  44.         this->m_position += numberOfBits;
  45.         return 0;
  46.     }
  47.     unsigned char* bitbuilder::getData()
  48.     {
  49.         return this->m_pdata;
  50.     }
What I do when I write it to screen is the following:
Expand|Select|Wrap|Line Numbers
  1. unsigned char* data = builder->getData();
  2.                 cout << "\nSize: " << sizeof(data) << "\n";
  3.                 for (int i = 0; i < sizeof(data); i++)
  4.                 {
  5.                     cout << "0x" << hex << setw(2) << setfill('0') << static_cast<int>(data[i]) << " ";
  6.                     if ((i+1) % 10 == 0)
  7.                         cout << "\n";
  8.                 }
Problem is that if I set it to a size of 4 "bytes" i can only populate the first element, I also realise that this implementation will be limited to 1 integer in size, but I want to be able to add bits until the size of the class ends. I am all out of ideas now though.

Oh and another problem I saw was that if I set the size to 1 I got 4 bytes nonetheless, I'm guessing this is due to the memory allocation.

Anyway, any tips necessary is appreciated, even if there is already functional "bitbuilders" I would like to know about them

// Andreas
Aug 6 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You are using C++, so use the bitset template.

Expand|Select|Wrap|Line Numbers
  1. bitset<128>  bits;    //128 bits.
  2.  
Aug 6 '07 #2

P: 7
Problem with bitset is that I can't dynamically change the size on it. And the size might alter.
Aug 7 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
That is not a showstopper.

bitset has a to_string method. So, take your current bitset to a string and delete the bitset.

Add elements to the string

Create a new bitset. Voila! more bits.
Aug 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.