啦啦啦拉拉裤 2021-10-12 22:15 采纳率: 69.4%
浏览 63
已结题

C语言问题 uint32_t提取浮点数

请问这个要怎么解决?在//REPLACE ME WITH YOUR CODE那写内容,在float_exp.c上写内容,按照要求来写,要写main函数,就像最下面的图片的例子那样。在可以用下面的例子来测试是否正确。原题为英文,有机翻。da佬编写1下,蟹蟹大lao。题目如下

img

img

img

img

float_exp.c内容
#include "float_exp.h"

// given the 32 bits of a float return the exponent
uint32_t float_exp(uint32_t f) {
    return 42; // REPLACE ME WITH YOUR CODE
}
float_exp.h内容
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#ifndef FLOAT_EXP_H
#define FLOAT_EXP_H

#include <assert.h>

/// We use `union overlay' to obtain the raw bits of a `float'-type
/// value, by storing the `float' in the `f' field and then using the
/// `u' field to obtain the bits.
union overlay {
    float f;
    uint32_t u;
};

uint32_t float_exp(uint32_t f);

#endif

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-10-12 23:20
    关注
    #include <stdio.h>
    #include <stdint.h>
    uint32_t float_exp(uint32_t  f)
    {
        f = f<<1;
        f = f>>24;
        return f;
    }
     
    union overlay {
        float f;
        uint32_t u;
    };
    
    int main()
    {
        overlay lay;
        lay.f = -42;
        printf("0x%x\n",float_exp(lay.u));
        lay.f = 3.14159012;
        printf("0x%x\n",float_exp(lay.u));
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月30日
  • 已采纳回答 10月22日
  • 修改了问题 10月13日
  • 创建了问题 10月12日