解释CPP程序 探究浮点格式

来源:百度知道 编辑:UC知道 时间:2024/06/20 18:53:29
#include <iostream>
#include <cstdlib>
using namespace std;

void printBinary(const unsigned int val)
{

for(int i = (sizeof(val) * 8 - 1);i >= 0;i--)
if(val & (1 << i))
cout << "1";
else
cout << "0";
cout << endl;
}

int main()
{
char temp[10] = "11";
double d = atof(temp);
unsigned char* cp = reinterpret_cast<unsigned char*>(&d);
for(int i = sizeof(double);i > 0;i -= 2)
{
printBinary(cp[i-1]);
printBinary(cp[i]);
}

system("pause");
return 0;
}

谁给解释下main函数
请问能解释下输出内容
和函数目的吗

#include <iostream>
#include <cstdlib>
using namespace std;

void printBinary(const unsigned int val)
{
//循环输出val的各位的值(0或1),从左边高位开始输出
for(int i = (sizeof(val) * 8 - 1);i >= 0;i--)
if(val & (1 << i))
cout << "1"; //如果val的第i位(从右边的低位开始数)为1,输出1
else
cout << "0"; //否则输出0
cout << endl;
}

int main()
{
char temp[10] = "11"; //字符串temp的内容为"11"
double d = atof(temp); //将"11"转换为11.0赋值给d
unsigned char* cp = reinterpret_cast<unsigned char*>(&d);
//d占用8字节的,将它的地址强制转换成unsigned char*

for(int i = sizeof(double);i > 0;i -= 2)
{
printBinary(cp[i-1]); //输出cp[i-1]的每一位,0或1
printBinary(cp[i]); //输出cp[i]的每一位,0或1
}

system("pause"); //暂停一下
return 0;
}

char temp[10] = "11"; 字符串 temp 内容 "11"
double d = atof