How to partition 8 bits into two set of 4 bits each using bitset<>?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhinuke
    New Member
    • May 2010
    • 23

    How to partition 8 bits into two set of 4 bits each using bitset<>?

    So I have been working on this code where I have to partition 8 bits into two set of 4 bits each.So far I have been trying my luck with bitset<> container class in C++.But it doesnt give me any ability copy first 4 bits of bitset<8> to first bitset<4> and remaining four bits to another one...

    Here is the code that I have written so far...I want to convert a character to 8 bits and the divides these 8 bits into two sets of 4 bits each.

    Code:
    FILE * fil;
        fil = fopen("test", "r");
        do {
                ch = fgetc(fil);
                str[counter++] = ch;
            } while (ch != EOF);
        fclose(fil);
    
        struct binary {
            bitset < 8 > bitval;
            char chr;
            bitset < 4 > lhs;
            bitset < 4 > rhs;
        } bin[counter];
    
    
        for (i = 0; i < counter; i++) {
            bin[i].bitval = str[i];
            bin[i].chr = str[i];
        }
            for (i = 0; i < counter; i++) {    
                cout << bin[i].chr << "   " << bin[i].bitval<< endl;
            }
    
        for (i = 0; i < counter; i++) {
            for(j=0;j<4;j++)
            {
                bin[i].lhs[j]=bin[i].bitval[j];
            }
        }
        for (i = 0; i < counter; i++) {
            for(j=4;j < 8;j++)
            {
                bin[i].rhs[j]=bin[i].bitval[j];
            }
        }
    This thing displays only the correct values of LHS and all values of RHS as 0000.How do I get the correct values of RHS bitset<4>?
    Kindly help.
    Thanks in advance.
    Last edited by abhinuke; Jan 31 '11, 07:56 PM. Reason: Typo Error...
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Try changing line 34:
    Code:
               bin[i].rhs[j-4]=bin[i].bitval[j];
    Luck!
    Oralloy

    Comment

    • abhinuke
      New Member
      • May 2010
      • 23

      #3
      Thanks a ton...it worked fine...I hope I will be able to move ahead using Bitset<> container class.
      By the way can you tell me what exactly was going wrong? Is it because of numbering conventions of bits in Bitset<> container?
      Thanks again!
      Last edited by abhinuke; Jan 31 '11, 08:04 PM. Reason: grammatical error

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        abhinuke,

        I suppose you could call it a numbering convention problem. I don't believe there is a way with the bitset container to specify the lower and upper array bounds, so you're stuck with the zero based array model.

        By the way, you could have also used simple bit fields unioned with a [icode]char[icode], something like this:
        Code:
        union BITZ
        {
          char cValue;
          struct
          {
            unsigned  lhs : 4;
            unsigned  rhs : 4;
          }
        };
        Good Luck!
        Oralloy

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Basically, it's because
          Code:
          Bit 0 1 2 3 4 5 6 7
          LHS 0 1 2 3
          RHS         0 1 2 3
          Your RHS array doesn't have 8 items so why are you trying to assign item 5 from the bitset to item 5 of RHS?

          Comment

          Working...