将符号位,指数和尾数转换为浮点型? [英] Convert Sign-Bit, Exponent and Mantissa to float?

查看:111
本文介绍了将符号位,指数和尾数转换为浮点型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有符号位,指数和尾数(如下代码所示).我正在尝试采用此值并将其转换为浮点数.这样做的目的是获取59.98(它将被读取为59.9799995)

I have the Sign Bit, Exponent and Mantissa (as shown in the code below). I'm trying to take this value and turn it into the float. The goal of this is to get 59.98 (it'll read as 59.9799995)

uint32_t FullBinaryValue = (Converted[0] << 24) | (Converted[1] << 16) |
                            (Converted[2] << 8) | (Converted[3]);

unsigned int sign_bit = (FullBinaryValue & 0x80000000);
unsigned int exponent = (FullBinaryValue & 0x7F800000) >> 23;
unsigned int mantissa = (FullBinaryValue & 0x7FFFFF);

我最初尝试做的只是将它们一点一点放置在原处:

What I originally tried doing is just placing them bit by bit, where they should be as so:

float number = (sign_bit << 32) | (exponent << 24) | (mantissa);

但这给了我2.22192742e+009.

然后我要使用公式:1.mantissa + 2^(exponent-127),但是不能在二进制数中放置小数位.

I was then going to use the formula: 1.mantissa + 2^(exponent-127) but you can't put a decimal place in a binary number.

然后我尝试获取每个单独的值(指数,特征,尾数),然后得到了

Then I tried grabbing each individual value for (exponent, characteristic, post mantissa) and I got

Characteristic: 0x3B (Decimal: 59)
Mantissa: 0x6FEB85 (Decimal: 7334789)
Exponent: 0x5 (Decimal: 5) This is after subtracting it from 127

然后我要获取这些数字,然后将其改造成printf.但是我不知道如何将尾数十六进制转换为应该的十六进制形式(幂为负指数).

I was then going to take these numbers and just retrofit it into a printf. But I don't know how to convert the Mantissa hexadecimal into how it's supposed to be (powered to a negative exponent).

关于如何将这三个变量(符号位,指数和尾数)转换为浮点数的任何想法吗?

编辑PAUL R 这是最小,完整和可验证格式的代码. 我在其中添加uint8_t Converted[4]只是因为它是我最终得到的值,并且使它可运行.

EDIT FOR PAUL R Here is the code in Minimal, Complete and Verifable format. I added the uint8_t Converted[4] there just because it is the value I end up with and it makes it runnable.

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    uint8_t Converted[4];
    Converted[0] = 0x42;
    Converted[1] = 0x6f;
    Converted[2] = 0xEB;
    Converted[3] = 0x85;

    uint32_t FullBinaryValue = (Converted[0] << 24) | (Converted[1] << 16) |
                                (Converted[2] << 8) | (Converted[3]);

    unsigned int sign_bit = (FullBinaryValue & 0x80000000);
    unsigned int exponent = (FullBinaryValue & 0x7F800000) >> 23;
    unsigned int mantissa = (FullBinaryValue & 0x7FFFFF);

    float number = (sign_bit) | (exponent << 23) | (mantissa);

    return 0;
}

推荐答案

问题是表达式float number = (sign_bit << 32) | (exponent << 24) | (mantissa);首先计算unsigned int,然后将该值转换为float.基本类型之间的转换将保留值,而不是内存表示形式.您试图做的是将内存表示形式重新解释为另一种类型.您可以使用 reinterpret_cast .

The problem is that the expression float number = (sign_bit << 32) | (exponent << 24) | (mantissa); first computes an unsigned int and then casts that value to float. Casting between fundamental types will preserve the value rather than the memory representation. What you are trying to do is reinterpret the memory representation as a different type. You can use reinterpret_cast.

尝试以下方法:

uint32_t FullBinaryValue = (Converted[0] << 24) | (Converted[1] << 16) |
                           (Converted[2] << 8) | (Converted[3]);


float number = reinterpret_cast<float&>(FullBinaryValue);

这篇关于将符号位,指数和尾数转换为浮点型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆