IEEE浮点数

float类型

#include "StdAfx.h"

//IEEE
struct 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值