Thursday, November 08, 2012

C範例程式 : 浮點數及雙精度浮點數轉換二進位字串

浮點數紀錄數字的格式如下 :
x = 1.yyyy * 2 ^ z

_ ________ _______________________
1     8              23
+  127+z    0.yyyy 用 *2 轉換為 binary 
-

_ ___________ ____________________________________________________
1      11                            52
+    1023+z    0.yyyy 用 *2 轉換為 binary
-


程式碼 : include stdio.h stdlib.h string.h limits.h

#define BTSIZE 256

void dec2bin(int);
void float2bin(float);
void double2bin(double);

int main()
{
	int i;
	float f;
	double d = 1.0;

	printf("%d\t",2);
	dec2bin(2);
	printf("\n%d\t",-2);
	dec2bin(-2);
	printf("\n%d\t",10);
	dec2bin(10);
	printf("\n%d\t",172);
	dec2bin(172);
	printf("\n%d\t",192);
	dec2bin(192);
	printf("\n%d\t",65535);
	dec2bin(65535);
	printf("\n%d\t",-65535);
	dec2bin(-65535);

	printf("\n");

	printf("\n%f\t",2.0);
	float2bin(2.0);
	printf("\n%f\t",-2.0);
	float2bin(-2.0);

	printf("\n%f\t",1.2);
	float2bin(1.2);
	printf("\n%f\t",-1.2);
	float2bin(-1.2);

	printf("\n%f\t",1.0);
	float2bin(1.0);
	printf("\n%f\t",-1.0);
	float2bin(-1.0);

	printf("\n%f\t",0.5);
	float2bin(0.5);
	printf("\n%f\t",-0.5);
	float2bin(-0.5);

	printf("\n%f\t",0.25);
	float2bin(0.25);
	printf("\n%f\t",0.75);
	float2bin(0.75);
	printf("\n%f\t",0.125);
	float2bin(0.125);
	printf("\n%f\t",0.1);
	float2bin(0.1);
	printf("\n%f\t",0.01);
	float2bin(0.01);

	printf("\n");

	printf("\n%lf\t",1.0);
	double2bin(1.0);
	printf("\n%lf\t",-1.0);
	double2bin(-1.0);

	printf("\n%lf\t",1.2);
        double2bin(1.2);
        printf("\n%lf\t",-1.2);
        double2bin(-1.2);

	printf("\n%lf\t",0.5);
	double2bin(0.5);
	printf("\n%lf\t",-0.5);
	double2bin(-0.5);

	printf("\n");

	return 0;
}

void dec2bin(int data)
{
	unsigned int t = (unsigned int)data;

	int b = sizeof(t) * CHAR_BIT;
	char bt[BTSIZE];

	for(int i = 0; i < b; i++)
	{
		bt[i] = (t & 0x01) ? '1' : '0';
		t>>=1;
	}

	for(int i = b-1; i >= 0; i--)
	{
		putchar(bt[i]);
		if(!(i%8) && i)
			printf(" ");
	}
}

void float2bin(float f)
{
	long l;
	int b;
	char bt[BTSIZE];

	memcpy(&l, &f, sizeof(f));

	b= sizeof(f) * CHAR_BIT;

	for(int i = 0; i < b; i++)
	{
		bt[i] = (l & 0x01) ? '1' : '0';
		l>>=1;
	}

	for(int i = b-1; i >= 0; i--)
	{
		putchar(bt[i]);
		if(!(i%8) && i)
			printf(" ");
	}
}

void double2bin(double d)
{
	int l;
	int *iptr;

	iptr = &d;

	memmove(&l, iptr+1, sizeof(l));
	dec2bin(l);
	printf(" ");
	memmove(&l, iptr, sizeof(l));
	dec2bin(l);
}


實際執行結果 :
$ ./show_data

2       00000000 00000000 00000000 00000010
-2      11111111 11111111 11111111 11111110
10      00000000 00000000 00000000 00001010
172     00000000 00000000 00000000 10101100
192     00000000 00000000 00000000 11000000
65535   00000000 00000000 11111111 11111111
-65535  11111111 11111111 00000000 00000001

2.000000        01000000 00000000 00000000 00000000
-2.000000       11000000 00000000 00000000 00000000
1.200000        00111111 10011001 10011001 10011010
-1.200000       10111111 10011001 10011001 10011010
1.000000        00111111 10000000 00000000 00000000
-1.000000       10111111 10000000 00000000 00000000
0.500000        00111111 00000000 00000000 00000000
-0.500000       10111111 00000000 00000000 00000000
0.250000        00111110 10000000 00000000 00000000
0.750000        00111111 01000000 00000000 00000000
0.125000        00111110 00000000 00000000 00000000
0.100000        00111101 11001100 11001100 11001101
0.010000        00111100 00100011 11010111 00001010

1.000000        00111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
-1.000000       10111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
1.200000        00111111 11110011 00110011 00110011 00110011 00110011 00110011 00110011
-1.200000       10111111 11110011 00110011 00110011 00110011 00110011 00110011 00110011
0.500000        00111111 11100000 00000000 00000000 00000000 00000000 00000000 00000000
-0.500000       10111111 11100000 00000000 00000000 00000000 00000000 00000000 00000000

No comments: