Skip to content

Instantly share code, notes, and snippets.

@harveyslash
Created November 30, 2016 05:09
Show Gist options
  • Save harveyslash/b6f434eeeaf2913975ef2f0efa1c67f8 to your computer and use it in GitHub Desktop.
Save harveyslash/b6f434eeeaf2913975ef2f0efa1c67f8 to your computer and use it in GitHub Desktop.
int arraySize;
int totalBitsUsed;
unsigned int* generateBitArray(FILE *file){
char currentChar ;
char buffer[256];
int currentSize = 1;
unsigned int *bitArray = malloc(sizeof(unsigned int)*currentSize);
int usedElements = 0;
int index = currentSize-1;
int numberOfBitsUsed = 0;
while(1){
currentChar= (char)fgetc(file);
if(currentChar==EOF)break;
if(usedElements==currentSize){
printf("array has grown");
bitArray = (unsigned int*)realloc(bitArray,sizeof(unsigned int)*(currentSize+20));
currentSize+=20;
arraySize = currentSize;
printf("array current size = %d\n",currentSize);
int counter = 0;
int tempIndex = usedElements-1;
while(tempIndex>=0){
bitArray[currentSize-1-counter++] = bitArray[tempIndex] ;
tempIndex--;
}
index = currentSize-usedElements-1;
}
/*printf("CODES TABLE %s\n",codesTable[currentChar]);*/
char *stringRepresentation = codesTable[currentChar];
int codeRepIndex = strlen(stringRepresentation) -1;
while(codeRepIndex>=0){
if(stringRepresentation[codeRepIndex]=='1'){
bitArray[index] |= 1 << numberOfBitsUsed;
}
else{
bitArray[index] |= 0 << numberOfBitsUsed;
}
codeRepIndex--;
numberOfBitsUsed++;
totalBitsUsed++;
if(numberOfBitsUsed==32){
index--;
usedElements++;
numberOfBitsUsed= 0;
}
}
printf("total bits used is %d \n", totalBitsUsed);
printf("!!!!!!!!!!printing Bit %u \n", bitArray[index]);
}
/*bitArray[usedElements] = bitArray[usedElements] << (32-totalBitsUsed%32);*/
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[20]);*/
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[19]);*/
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[18]);*/
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[17]);*/
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[16]);*/
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[15]);*/
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[14]);*/
printf("!!!!!!!!!!printing last element %u \n", bitArray[currentSize-1]);
printf("!!!!!!!!!!printing last element %u \n", bitArray[currentSize-2]);
printf("total bits used is %u \n", totalBitsUsed);
return bitArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment