Exemplo n.º 1
0
int main(int argc, char* argv[])
{
    float a, b, c, sd, alpha, beta, z;
    double a2, b2, c2, sd2, alpha2, beta2, z2;

    a = a2 =  1.00000;
    b = b2 = -333.333;
    c = c2 = 1.23456;

    printf("a = %-12f : ", a);
    print_bit(a);
    printf("b = %-12f : ", b);
    print_bit(b);
    printf("c = %-12f : ", c);
    print_bit(c);

    sd = sqrt( b*b - 4*a*c );
    sd2 = sqrt( b2*b2 - 4*a2*c2);

    alpha = (-b + sd) / (2*a);
    beta = (-b - sd) / (2*a);

    alpha2 = (-b2 + sd2) / (2*a2);
    beta2 = (-b2 - sd2) / (2*a2);

    printf("alpha = %15e    beta = %15e\n", alpha, beta);
    printf("alpha2= %15e    beta2= %15e\n", alpha2, beta2);
    printf("( sd= %f )\n", sd);
    printf("( sd= %lf )\n", sd2);
    return 0;
}
Exemplo n.º 2
0
int main(){
  int i;
  unsigned char u1,u2,u3;
  u1 = 68;
  printf("u1 = ");print_bit(u1);
  printf("u1 = %d\n",u1);
  u2 = 15;
  u2 = (0xff ^ u2) + 0x01;
  printf("u2 = ");print_bit(u2);
  printf("u2 = %d\n",u2);
  printf("u2 = %d\n",(char)u2);
  u3 = u1 + (char)u2;
  printf("u3 = ");print_bit(u3);
}
int decompress_3(int fd,int fdd,int fdtemp)
{
	unsigned char ch,chtemp,temp,tempch,temp1,temp2;
	int i,j=1,count=1,pos,n=1;
	char *ma;
	pos=lseek(fdtemp,0,SEEK_END);
	printf("POSITION : %d\n",pos);
	ma=(char *)malloc(pos+1);
	lseek(fdtemp,0,SEEK_SET);
	read(fdtemp,ma,pos);
	while(read(fd,&ch,1))
	{
		if(j%3==1)
		{
			print_bit(ch);
			tempch=ch;
			tempch=tempch>>5;
			chtemp=*(ma+(int)tempch);
			write(fdd,&chtemp,1);
			temp1=ch;
			temp1=temp1<<3;
			temp1=temp1>>5;
			if(temp1==7)
				break;
			chtemp=*(ma+(int)temp1);
			write(fdd,&chtemp,1);
			temp2=ch;
			temp2=temp2<<6;
			temp2=temp2>>6;
			temp2=temp2<<1;
		}
		else if(j%3==2)
Exemplo n.º 4
0
static void		print_bit(int i, char c)
{
	if (i > 0)
		print_bit(i - 1, c / 2);
	if (i == 4)
		ft_putchar(' ');
	ft_putchar(c % 2 + 48);
}
Exemplo n.º 5
0
main() 
{
	int i,n;
	printf("Enter the element\n");
	scanf("%d",&i);
	int size = sizeof(i) * 8;
	print_bit(i,size);
	reverse_bit(i,size);

}
Exemplo n.º 6
0
int main(int argc, char *argv[])
{
    struct pcard card;
    union _xyz  xyz; 
    card.pips = 0xA;
    card.suit = 0x5;
    card.a[0] = 'h';
    card.a[9] = 'c';
    char c = 'b';

    extern void print_bit(int);

    print_bit(0xa3b42233);

    printf ("sizeof(char)=%d\n", sizeof('A'));
    printf ("sizeof(char)=%d\n", sizeof(c));
    
    /* sizeof(struct pcard) = 12 */
    printf("sizeof(struct pcard)=%ld\n", sizeof(struct pcard));

    /* sizeof(struct _abc) = 8 */
    printf("sizeof(struct _abc)=%ld\n", sizeof(struct _abc));
    
    /* sizeof(struct _small_integers) = 8 */
    printf("sizeof(struct _small_integers)=%ld\n", sizeof(struct _small_integers));

    /* sizeof(struct _def) = 8 */
    printf("sizeof(struct def)=%ld\n", sizeof(struct _def));

    xyz.bits.x = 3;
    xyz.bits.y = 2;
    xyz.bits.z = 4;

    printf("bundle=0x%x\n", xyz.bundle);
    print_bit(xyz.bundle);
    
    return 0;
}
Exemplo n.º 7
0
// Constructor: Constructor for Connection class
// Only defines the initial values for the object 
// parameter: type, defines the type of the socket
Connection::Connection (int target_type)
{
#if 0
  d_print( "[Constructor] type : %d (", type );
  print_bit( type );
  d_print( ")\n" );
#endif

  if( !((target_type & CONNECTION_FIFO) || (target_type & CONNECTION_SOCKET )) )
    {
      tl_message ("You must decide Socket or FIFO");
      return;
    }
  
  type     = target_type;
  fd_read  = FALSE;
  fd_write = FALSE;
  fp_read  = NULL;
  fp_write = NULL;
  stream   = NULL;
  sprintf(name, "NoName" );
}
Exemplo n.º 8
0
int main() {
  float_b f1,f2,f3,f4;
  int i = 0;
  int c1 = 0;
  int c2 = 0;
  int c3 = 0;
  int c4 = 0;

  srand(time(NULL));

  while(i < 1000000) { //正規化数ランダム
    f1.ieee.sign = rand()%2;
    f1.ieee.exp = rand()%254+1;
    f1.ieee.fraction = rand()%8388608;
    f2.ieee.sign = rand()%2;
    f2.ieee.exp = rand()%254+1;
    f2.ieee.fraction = rand()%8388608;

    f3.f = f1.f + f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || f4.ieee.exp != 255)) {
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c1++;
    }

    f3.f = f1.f * f2.f;
    f4.f = fmul(f1.f,f2.f);
    if (f3.f != f4.f && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || (f4.ieee.exp != 0 && f4.ieee.exp != 255))) {
      printf("%f*%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c2++;
    }

    f3.f = f1.f / f2.f;
    f4.f = fdiv(f1.f,f2.f);
    if (f3.f != f4.f && ((f3.ieee.exp != 0 && f3.ieee.exp != 255) || (f4.ieee.exp != 0 && f4.ieee.exp != 255))) {
      printf("%f/%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c3++;
    }

    f1.ieee.sign = 0;
    f3.f = sqrtf(f1.f);
    f4.f = sqrt_m(f1.f);
    if (f3.f != f4.f) {
      printf("sqrt(%f)=%f\n",f1.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c4++;
    }

    i++;
  }

  printf("fadd:%d fmul:%d fdiv:%d sqrt:%d\n",c1,c2,c3,c4);

  i = 0;
  c1 = 0;
  c2 = 0;
  c3 = 0;
  c4 = 0;

  while(i < 1000000) { //0.0に対する計算
    f1.ieee.sign = rand()%2;
    f1.ieee.exp = (rand()%254+1)*(i%2);
    f1.ieee.fraction = rand()%8388608;
    f2.ieee.sign = rand()%2;
    f2.ieee.exp = (rand()%254+1)*((i+1)%2);
    f2.ieee.fraction = rand()%8388608;

    if (i%2 == 1) f3.f = f1.f;
    else f3.f = f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f) {
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c1++;
    }

    f3.ieee.sign = (f1.ieee.sign + f2.ieee.sign)%2;
    f3.ieee.exp = 0;
    f3.ieee.fraction = f1.ieee.fraction;
    f4.f = fmul(f1.f,f2.f);
    if (f3.f != f4.f) {
      printf("%f*%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c2++;
    }

    if (i%2 == 1) {
      f3.ieee.sign = f1.ieee.sign + f2.ieee.sign;
      f3.ieee.exp = 255;
      f3.ieee.fraction = f1.ieee.fraction;
    } else {
      f3.f = 0.0 / f2.f;
    }
    f4.f = fdiv(f1.f,f2.f);
    if ((f3.ieee.sign != f4.ieee.sign || f3.ieee.exp != f4.ieee.exp || f3.ieee.fraction != f4.ieee.fraction) && (i%2 == 1 || (f3.ieee.exp != 0 && f3.ieee.exp != 255) || (f4.ieee.exp != 0 && f4.ieee.exp != 255))) {
      printf("%f/%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c3++;
    }

    f1.ieee.sign = 0;
    if (i%2 == 1) f3.f = sqrtf(f1.f);
    else f3.f = f1.f;
    f4.f = sqrt_m(f1.f);
    if (f3.f != f4.f) {
      printf("sqrt(%f)=%f\n",f1.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c4++;
    }

    i++;
  }

  printf("コーナーケース\n");
  printf("0.0(とみなされる数)の演算 fadd:%d fmul:%d fdiv:%d sqrt:%d\n",c1,c2,c3,c4);

  i = 0;
  c1 = 0;
  c2 = 0;
  c3 = 0;
  c4 = 0;

  while(i < 1000000) { 
    f1.ieee.sign = rand()%2;
    f1.ieee.exp = (rand()%254+1);
    f1.ieee.fraction = rand()%8388608;
    f2.ieee.sign = rand()%2;
    f2.ieee.exp = f1.ieee.exp + (rand()%3-1);
    f2.ieee.fraction = rand()%8388608;
    if (f2.ieee.exp == 255) f2.ieee.exp = 254;
    if (f2.ieee.exp == 0) f2.ieee.exp = 1;

    f3.f = f1.f + f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f  && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || f4.ieee.exp != 255)) {
      print_bit(f1);
      print_bit(f2);
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c1++;
    }

    f2.f = f1.f;
    f2.ieee.sign = f1.ieee.sign + 1;

    f3.f = f1.f + f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f  && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || f4.ieee.exp != 255)) {
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c2++;
    }

    i++;
  }

  printf("指数の差が高々1 fadd:%d\n",c1);
  printf("絶対値が等しく符号が逆 fadd:%d\n",c2);

  return 0;
}
Exemplo n.º 9
0
void			ft_putbit(char c)
{
	print_bit(7, c);
	ft_putchar('\n');
}
Exemplo n.º 10
0
void dump_code_bin(FILE* fp)
{
	unsigned int i = 0;
	unsigned int shift_count = 0;
	unsigned char c = 0;
	unsigned short entry_count = 0;
	unsigned char bitstream_token = 0;
	unsigned char bitstream_length = 0;
	unsigned char bitstream_length_byte = 0;
	
	for (c = 0; c < MAX_SYMBOLS; c++) {
		if (code[c][0])
			entry_count++;
		if (c == 255)
			break;
	}
	fwrite(&entry_count, sizeof(unsigned short), 1, fp);

	for (c = 0; c < MAX_SYMBOLS; c++) {
		if (code[c][0]) {
			fwrite(&c, sizeof(unsigned char), 1, fp);
			
			bitstream_length = strlen(code[c]);
			bitstream_length_byte = bitstream_length / 8;
			if (bitstream_length % 8)
				bitstream_length_byte++;
			fwrite(&bitstream_length, sizeof(unsigned char), 1, fp);
			fwrite(&bitstream_length_byte, sizeof(unsigned char), 1, fp);

			//debug
			printf("%d ", c);
			//debug
			for (i = 0; i < bitstream_length; i++) {
				if (code[c][i] == '1')
					bitstream_token |= 1;
				
				if (shift_count == 7) {
					fwrite(&bitstream_token, sizeof(unsigned char), 1, fp);
					//debug
					print_bit(bitstream_token);
					putc(' ', stdout);
					//debug

					bitstream_token = 0;
					shift_count = 0;
				}
				else {
					bitstream_token <<= 1;
					shift_count++;
				}
			}
			if (shift_count) {
				bitstream_token <<= (7 - shift_count);
				fwrite(&bitstream_token, sizeof(unsigned char), 1, fp);
				//debug
				print_bit(bitstream_token);
				putc(' ', stdout);
				//debug

				bitstream_token = 0;
				shift_count = 0;
			}
		}
		//debug
		if (code[c][0]) {
			putc('\n', stdout);
			//printf("%d %s\n", c, code[c]);
			printf("%d", c);
			for (i = 0; i < strlen(code[c]); i++) {
				if (!(i % 8))
					putc(' ', stdout);
				putc(code[c][i], stdout);
			}
			putc('\n', stdout);
		}
		//debug
		if (c == 255)
			break;
	}
}
int compress_6(int fd,char *ma,char * argv)
{
	char * temp_name;
	int cf,j=1,i,k,n=1,chek=0;
	char ch;
	unsigned char c,byt,temp1,temp2,ctemp[3],temp3;
	temp_name=(char *)malloc(24);
	strcpy(temp_name,argv);
	strcat(temp_name,"_comp");
	cf=open(temp_name,O_WRONLY|O_CREAT,0666);
	lseek(fd,0,SEEK_SET);
	byt=byt^byt;
	while(read(fd,&ch,1))
	{	
		chek=0;
		for(k=0;k<3;k++)
			ctemp[k]=ctemp[k]^ctemp[k];
		k=0;
		c=c^c;
		for(i=0;*(ma+i)!=ch;i++);
		do
		{
			sprintf(&ctemp[k],"%d",(int)i%10);
			ctemp[k]=ctemp[k]-48;
			k++;

			i=i/10;			
		}while(i);
		c=ctemp[2]*100+ctemp[1]*10+ctemp[0];
		if(!read(fd,&ch,1))
		{
			lseek(fd,1,SEEK_CUR);
			chek=1;
		}
		lseek(fd,-1,SEEK_CUR);
		if(n%3==1)
		{
			if(j%2==1)
			{
				temp1=c;
				temp1=temp1<<2;
				byt=byt|temp1;
				if(chek)
				{
					k=0;	
					i=63;
					do
					{
						sprintf(&ctemp[k],"%d",(int)i%10);
						ctemp[k]=ctemp[k]-48;
						k++;
		
						i=i/10;			
					}while(i);
					c=ctemp[2]*100+ctemp[1]*10+ctemp[0];
					temp1=c;
					temp1=temp1<<2;
					temp1=temp1>>6;
					byt=byt|temp1;
					write(cf,&byt,1);
					byt=byt^byt;
					temp1=c;
					temp1=temp1>>2;
					temp1=temp1<<4;
					write(cf,&temp1,1);
					break;
					
				}
			temp1=c;
			}
			else
			{
				temp2=c;
				temp1=temp1<<2;
				temp1=temp1>>6;
				byt=byt|temp1;
				print_bit(byt);
				write(cf,&byt,1);
				byt=byt^byt;
				if(chek)
				{
					temp1=temp2;
					temp1=temp1<<4;
					k=0;	
					i=63;
					do
					{
						sprintf(&ctemp[k],"%d",(int)i%10);
						ctemp[k]=ctemp[k]-48;
						k++;
		
						i=i/10;			
					}while(i);
					c=ctemp[2]*100+ctemp[1]*10+ctemp[0];
					temp3=c;
					temp3=temp3<<2;
					temp3=temp3>>4;
					temp1=temp1|temp3;
					write(cf,&temp1,1);
					temp1=c;
					temp1=temp1<<6;
					write(cf,&temp1,1);
					break;
					
				}
				n++;
			}
		j++;
		}