float类型
#include "StdAfx.h"
//IEEEstruct status
{
unsigned data: 23;
unsigned Index: 8;
unsigned sign: 1;
};
//one byte
struct stByte
{
unsigned FirBit: 1;
unsigned SecBit: 1;
unsigned ThiBit: 1;
unsigned FouBit: 1;
unsigned FivBit: 1;
unsigned SixBit: 1;
unsigned SevBit: 1;
unsigned EigBit: 1;
};
#include "stdafx.h"
#include "IEEE.H"
int main(int argc, char* argv[])
{
struct status stIEEE;
struct stByte stbIT;
float f = 0.0f;
int *pf = ((int *)&f);
char szbuf[4] = {0};
memset(&stIEEE,0,sizeof(struct status));
memset(&stbIT,0,sizeof(struct stByte));
printf("Input a float number:");
scanf("%f",&f);
memcpy(&stIEEE,pf,sizeof(int));
printf("sign:%X\r\n",stIEEE.sign);
printf("Index:%X\r\n",stIEEE.Index);
printf("data:%X\r\n",stIEEE.data);
memcpy(szbuf,pf,sizeof(stIEEE));
for(int i = 0;i < 4;i++)
{
memcpy(&stbIT,&szbuf[4 - i - 1],sizeof(char));
printf("%X",stbIT.EigBit);
printf("%X",stbIT.SevBit);
printf("%X",stbIT.SixBit);
printf("%X",stbIT.FivBit);
printf("-");
printf("%X",stbIT.FouBit);
printf("%X",stbIT.ThiBit);
printf("%X",stbIT.SecBit);
printf("%X",stbIT.FirBit);
if(i != 3)
{
printf("-");
}
}
printf("\r\n");
system("pause");
return 0;
}
double类型:
struct tagStatus
{
unsigned data2: 32;
unsigned data1: 20;
unsigned Index: 11;
unsigned sign: 1;
};
//one byte
struct tagstByte
{
unsigned FirBit: 1;
unsigned SecBit: 1;
unsigned ThiBit: 1;
unsigned FouBit: 1;
unsigned FivBit: 1;
unsigned SixBit: 1;
unsigned SevBit: 1;
unsigned EigBit: 1;
};
// double.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "IEEE.H"
int main(int argc, char* argv[])
{
struct tagStatus stDouble;
struct tagstByte stbIT;
double d = 0.0;
char szbuf[8] = {0};
memset(&stDouble,0,sizeof(struct tagStatus));
memset(&stbIT,0,sizeof(struct tagstByte));
scanf("%lf",&d);
memcpy(&stDouble,&d,sizeof(double));
printf("sign:%x\r\n",stDouble.sign);
printf("index:%x\r\n",stDouble.Index);
printf("data %x%x\r\n",stDouble.data1,stDouble.data2);
memcpy(szbuf,&d,sizeof(double));
for(int i = 0;i < 8;i++)
{
memcpy(&stbIT,&szbuf[8 - i - 1],sizeof(char));
printf("%X",stbIT.EigBit);
printf("%X",stbIT.SevBit);
printf("%X",stbIT.SixBit);
printf("%X",stbIT.FivBit);
printf("-");
printf("%X",stbIT.FouBit);
printf("%X",stbIT.ThiBit);
printf("%X",stbIT.SecBit);
printf("%X",stbIT.FirBit);
if((i != 0) && (i % 8 == 0))
{
printf("\r\n");
}
}
printf("\r\n");
system("pause");
return 0;
}