void setCacheFlag(unsigned char position){//takes position of bit to set, from 0 to 7
//this outlining section contains the cache code
//always sets that bit, for simplicity
	unsigned char scratch=0x80;
	unsigned char i=0x00;
	if(position>=8){
		while((position-=8)>8){
			i++;
		}
	}		
	scratch>>=position;//put bit into proper position
	cacheUsed[i]|=scratch;//set that bit in the used flag
}

void clearCacheFlag(unsigned char position){
	unsigned char scratch=0x80;
	unsigned char i=0x00;
	if(position>=8){
		while((position-=8)>8){
			i++;
		}
	}
	scratch>>=position;//shift bit into proper position
	scratch=~scratch;//complement scratch character
	cacheUsed[i]&=scratch;//clear that bit in cacheUsed
}

bit checkCacheFlag(unsigned char position){//returns state of cache flag in input position
//should this function just be folded into checkInCache and insertInCache, to remove call overhead?
//will just be called in for loop anyway; just leave in function for debugging
	unsigned char scratch=0x80;
	unsigned char i=0x00;
	if(position>=8){
		while((position-=8)>8){
			i++;
		}
	}
	scratch>>=position;
	scratch&=cacheUsed[i];
	return scratch!=0x00;
}

bit insertInCache(unsigned char input){//finds first unused index, sets that flag, and overwrites
//that element. Returns 1 if successful, 0 if no unused indices found
	unsigned char i=0x00;
	for(i=0x00; i<0x10; i++){//for every element in cache
		if(!checkCacheFlag(i)){//if that element is unused
			setCacheFlag(i);//mark it as used
			compressedCache[i]=input;//store the information
			return 1;//report success
		}
	}
	return 0;//should only get here if was unable to report success; thus, report failure
}

unsigned char messageInCache(unsigned char input, unsigned char dontCareMask){
	//returns 0x00 if no match or first matching character in cache
	unsigned char i=0x00;
	if(!dontCareMask){//if precise match is required,
	//can do more efficient search
		for(i=0x00; i<0x10; i++){
			if(compressedCache[i]==input){
				if(checkCacheFlag(i)){
					clearCacheFlag(i);
					return compressedCache[i];
				}
			}
		}
	}else{
		for(i=0x00; i<0x10; i++){
			if(checkCacheFlag(i)){//if that slot is used,
				unsigned char scratch=input^compressedCache[i];
//check for match within tolerance
				scratch|=dontCareMask;
				if(scratch==dontCareMask){
//if matches within tolerance, then clear that slot and return matching char
					clearCacheFlag(i);
					return compressedCache[i];
				}
			}
		}
	}	
	return 0x00;
}