Example #1
0
File: drive2.c Project: daydin/maze
void decodeOne(int *sum, int bits, _Bool* binary, int option){ //option is probably redundant here, remove it later
	//take in pointer to bit array, calculate integer value
	if(0==option || 2==option){ //if you have negatives, use the first bit as a sign bit!
		int k;
		int carry=0;

		//if all bits processed, return the sign flag and the value
		if(0==bits){
			return;
		}

		if(1==binary[0]){
			//if your num is negative: subtract 1 and flip bits, calc number, number = -number

			carry=1;
			sum[0]=1;
			for(k=BIT_SIZE-1; k>-1; k--){
				if(binary[k]&carry){
					binary[k]=binary[k]^carry;
					carry=0;
				}else{
					binary[k]=binary[k]^carry;
				}
			}

			for(k=BIT_SIZE-1; k>-1; k--){
				binary[k] = sum[0]^(binary[k]); //flip all the bits and repopulate the array with compl. nums
				// printf("bin[%d]:%d\n",k,binary[k]);
			}
		}

		sum[1]+=(int)pow(2.0,(double)BIT_SIZE-(bits))*(int)binary[bits-1];
		//printf("sum %d bin[%d]:%d\n",sum[1], bits-1,binary[bits-1]);
		--bits;
		decodeOne(sum, bits, binary,option);
	}else{
		if(0==bits){
			return;
		}
		sum[1]+=(int)pow(2.0,(double)BIT_SIZE-(bits))*(int)binary[bits-1];
		//printf("sum %d bin[%d]:%d\n",sum[1], bits-1,binary[bits-1]);
		--bits;
		decodeOne(sum, bits, binary,option);

	}
}
Example #2
0
uint32_t convFromWtoCP866(char **dest, const WCHAR_T *Source, uint32_t len)
{
	if (!len)
		len = getLenShortWcharStr(Source) + 1;
	if (!*dest)
		*dest = new char[len];
	char *tmp = *dest;
	const WCHAR_T *src = Source;
	uint32_t res = 0;

	::memset(*dest, 0, len * sizeof(**dest));
	do {
		*tmp++ = decodeOne(*src++);
		++res;
	} while (len-- && *src);
	*tmp = 0;

	return res;
Example #3
0
File: drive2.c Project: daydin/maze
double* popDecoder (const _Bool *genome_binary){
	int j=0,flag,k;
	int val[2];
	_Bool bin_array[BIT_SIZE];
	int pop_int[NB_WEIGHTS];

	//_Bool pop_bin[POP_SIZE][GENOME_LENGTH]; //10-by-390

	for(j=0; j<NB_WEIGHTS; j++){//split genome into 5 bit strings, set option flag for each segment of the genome
		if(j<NB_ONLY_WT){
			flag=0;
		}else if(j>=NB_ONLY_WT && j<NB_ONLY_WT+NB_TOT_TC){
			flag=1;
		}else if(j>=NB_ONLY_WT+NB_TOT_TC && j<NB_WEIGHTS){
			flag=2;
		}else {
			return NULL;
		}
		for(k=0;k<BIT_SIZE;k++){
			bin_array[k]=genome_binary[(BIT_SIZE*j)+k];
		}
		for(k=0;k<BIT_SIZE;k++){
			fprintf(pF2,"%d",(bin_array[k])?1:0);
			fflush(pF2);
		}
		val[0]=0;
		val[1]=0;		
		decodeOne(val,BIT_SIZE,bin_array,flag); //obtain your integer in val array, first is sign bit and second is the number
		pop_int[j]= (1==val[0])?(-val[1]):val[1];
		fprintf(pF2,"\npop_int[%d]:%d\n",j,pop_int[j]);
		fflush(pF2);
		genome_real[j]=itor(val, flag);
	}

	fflush(pF2);
	return genome_real;
}