求大牛帮忙解答?

kafeimao 2014-01-19 04:25:23
Write a program which does the following: Given a real decimal number input, take the number apart and convert it to a 32 bit floating point number represented as a binary string (that is, a string of 1s and 0s). You may use both numeric and string manipulations to build the binary number from its parts (sign, mantissa, and exponent), but please return a string. The goal is to explore how an arbitrary decimal number is disassembled and represented as a 32 bit floating point number.
...全文
63 点赞 收藏 1
写回复
1 条回复
bobo_包子 2014年01月19日
float f = 0;
	cout << "input a number:" << endl;
	cin >> f;

	unsigned char flag = 1 << 7;
	char *pf = (char *)&f;
	int i = 0, j = 0;
	while(i++ < sizeof(f))
	{
		while(j++ < 8)
		{
			if((*pf & flag) == flag)
				cout << "1";
			else
				cout << "0";

			flag >>= 1;
		}
		flag = 1 << 7;
		j = 0;
		pf += 1;
	}

	getchar();
	getchar();
	return 0;
回复 点赞
发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告