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);