Printing out contents of bit array

This is a discussion on Printing out contents of bit array within the C Programming forums, part of the General Programming Boards category; Hi am trying to read out the contents of my bit array but for some reason it only works for ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Printing out contents of bit array

    Hi am trying to read out the contents of my bit array but for some reason it only works for the first byte index.
    Code:
    int letterToIndex(char c){
       int index=(int)c - (int)'a';
       return index;
       }
    
       // a method that converts an integer representation to an alphabet
    
       char indexToAlpha(int index){
           char c= (char)( (int)('a')+ index);
           return c;
       }
    
    
    // a bit vector struct
        struct bitset{
            int size;
            unsigned char *bitcontents;
    
    
    
        };
     /*initialisng a bitset struct to the size of the elements
     */
      struct bitset * bitset_new(int size){
          struct bitset * p=  ( struct  bitset *)  malloc(sizeof(*p));
          p->size=  size/(sizeof(unsigned char) * 8);
    
          if(size % (sizeof(unsigned char) *8) !=0)
          p->size = p->size+1;
    
          p->bitcontents=  (unsigned char*) malloc( sizeof(unsigned char) * p->size);
    
              return p;
      }
    
      /*checks to see if an integer item is in the set
      *returns 1 if in the set, 0 if not, and -1 if 'item' is out of bounds
      */
    
      int bitset_lookup(struct bitset * p, int item)
      {
    
    
         int block=item/(sizeof(unsigned char) * 8);
         int bitindex= item % (sizeof(unsigned char) * 8);
         if(block >= p->size){
        return -1;
         }
         if((p->bitcontents[block] & (1<< bitindex) ))
         return 1;
    
    
        else
         return 0;
    
    
    
     }
     // add an item, with number 'item' to the set
    // (returns 0 if item is out of bounds, 1 otherwise)
    // has no effect if the item is already in the set
      int bitset_add(struct bitset * p, int item){
          int block= item / (sizeof(unsigned char) * 8);     // determines what index of my bitcontents to put the bit in
          int bitindex= item% (sizeof(unsigned char) * 8);
          if(block< p->size){
          p->bitcontents[block]=p->bitcontents[block] ^ 1<<bitindex;
         // printf("xxxx%d/n",p->bitcontents[0]);
          return 1;
          }
          return 0;
     void bitset_union(struct bitset * destination ,struct bitset * src1,struct bitset *src2){
         int i=0;
    
         while(i< destination->size){
             destination->bitcontents[i]= src1->bitcontents[i] | src2->bitcontents[i];
             i++;
         }
    
      }
    
      }
    void toString(struct bitset *p,char buffer[]){
    
          int t=0;
          int i=0;int j=0;
          int z=8;
    
          for(;i< p->size;i++){
           
              for(;j<8;j++){
                  if(p->bitcontents[i] & 1<<j)
                 putchar(indexToAlpha(j));
    
    
              }
            
          }
       
    
      }
    
    
    int main(int argc, char** argv) {
        struct bitset *a;
        struct bitset *b;
        struct bitset  *destination;
        char buffer[27];
        char bufferoutput[255];
        char buuu[27];
         int index=0;
         int lookup=0;
     //   index =letterToIndex(letter);
    
    
    
    
    
    
         a=bitset_new(26);
         b=bitset_new(26);
         destination=bitset_new(26);
    
    
         // lookup = bitset_lookup(a, index);
    
    
        printf("please enter a string for set a");
        fgets(buffer,26,stdin);
        int i=strlen(buffer)-1;
        if(buffer[i]=='\n')
        buffer[i]='\0';
       // printf("the set of characters apperaing in set a");
             // printf("%s\n",buffer);
    
    
        i=0;
        // setting my bits in my bit set vectors
         while(buffer[i]!='\0')
         {
             bitset_add(a, letterToIndex(buffer[i++]));
    
         }
    
    
    
       printf("please enter a string for set b");
       fgets(buffer,26,stdin);
        i=strlen(buffer)-1;
        if(buffer[i]=='\n')
        buffer[i]='\0';
      // printf("the set of characters apperaing in set b");
         // printf("%s\n",buffer);
    
        i=0;
    
         while(buffer[i]!='\0'){
                bitset_add(b, letterToIndex(buffer[i++]));
         }
    
    
    
    
    
         bitset_union(destination,a,b);
          lookup = bitset_lookup(destination, 11);
          printf("%d\n",lookup);
          toString(destination, bufferoutput);

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    starting at line 71 you have a function inside another function... C can't do that.

    You also have no closing brace or return value from main() ... which may just be a copy and paste error.

    A better way of stripping of the newlines is to use strchr()
    Code:
    newline = strchar(buffer,'\n');
    if (newline)
      *newline = 0;
    But i suspect your problems originate at line 71...

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by CommonTater View Post
    starting at line 71 you have a function inside another function... C can't do that.

    You also have no closing brace or return value from main() ... which may just be a copy and paste error.

    A better way of stripping of the newlines is to use strchr()
    Code:
     line 71 just implements my union of two sets.I dont have a function inside a function.. the closing brace is a copy error sorry about  that.
    newline = strchar(buffer,'\n');
    if (newline)
      *newline = 0;
    But i suspect your problems originate at line 71...
    just implements my union of two sets.I dont have a function inside a function.. the closing brace is a copy error sorry about that.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sigur47 View Post
    just implements my union of two sets.I dont have a function inside a function.. the closing brace is a copy error sorry about that.
    Actually....
    Code:
       int bitset_add(struct bitset * p, int item)
        {                                                                         <----- start of function 1
          int block= item / (sizeof(unsigned char) * 8);     
          int bitindex= item% (sizeof(unsigned char) * 8);
          if(block< p->size)
             {
               p->bitcontents[block]=p->bitcontents[block] ^ 1<<bitindex;
               // printf("xxxx%d/n",p->bitcontents[0]);
               return 1;
              }
          return 0;
    
     void bitset_union(struct bitset * destination ,struct bitset * src1,struct bitset *src2)
        {                                                                          <---- start of function 2
         int i=0;
    
         while(i< destination->size){
             destination->bitcontents[i]= src1->bitcontents[i] | src2->bitcontents[i];
             i++;
       }                                                                        <---- end of function 2
    
      }                                                                         <---- end of function 1                                                   
    
      }                                                                          <---  unmatched brace

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by CommonTater View Post
    Actually....
    Code:
       int bitset_add(struct bitset * p, int item)
        {                                                                         <----- start of function 1
          int block= item / (sizeof(unsigned char) * 8);     
          int bitindex= item% (sizeof(unsigned char) * 8);
          if(block< p->size)
             {
               p->bitcontents[block]=p->bitcontents[block] ^ 1<<bitindex;
               // printf("xxxx%d/n",p->bitcontents[0]);
               return 1;
              }
          return 0;
    
     void bitset_union(struct bitset * destination ,struct bitset * src1,struct bitset *src2)
        {                                                                          <---- start of function 2
         int i=0;
    
         while(i< destination->size){
             destination->bitcontents[i]= src1->bitcontents[i] | src2->bitcontents[i];
             i++;
       }                                                                        <---- end of function 2
    
      }                                                                         <---- end of function 1                                                   
    
      }                                                                          <---  unmatched brace
    ouch i missed a bracket when copying and pasting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing contents of a text file
    By never_lose in forum C Programming
    Replies: 10
    Last Post: 04-28-2011, 09:25 AM
  2. printing array contents
    By c_programmer in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 05:00 PM
  3. Printing the contents of memory
    By hpteenagewizkid in forum C Programming
    Replies: 6
    Last Post: 11-07-2006, 12:51 PM
  4. printing contents of array vertically
    By richdb in forum C Programming
    Replies: 16
    Last Post: 03-10-2006, 12:12 AM
  5. Printing out contents of a file to the screen
    By Simon in forum C Programming
    Replies: 18
    Last Post: 10-21-2002, 08:05 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21