获取C中浮点数的位表示形式 [英] Obtaining bit representation of a float in C

查看:101
本文介绍了获取C中浮点数的位表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用联合来获取浮点值的位表示,我的代码当前如下:

I'm trying to use unions to obtain the bit representation of float values, my code is currently as follows:

union ufloat {
  float f;
  unsigned u;
};

int main( ) {       

   union ufloat u1;
   u1.f = 3.14159f;
   printf("u1.u : %f\n", u1.u);

但是我尝试打印的任何内容都被打印为0.0000000,而不是位(例如0001 0110或类似的东西),我的代码出了什么问题?

However anything I try to print gets printed as 0.0000000, instead of as bits (such as 0001 0110, or something similar), what is wrong in my code?

请注意,我更愿意使用工会来实现这一目标.

Note that preferebly I would like to use unions to achieve this.

推荐答案

有很多方法可以实现此目的.知道您真正想做的只是简单地输出内存中构成float的位.实际上,在所有x86类型的实现中,该格式均以 IEEE-754单精度浮点格式存储.在x86上是32位数据.这就是在将float强制转换为unsigned时(同时都是32位,并且为unsigned类型定义了位操作)时允许对位进行窥视"的原因对于x86以外的实现,甚至在x86本身,unsigned的更好选择是stdint.h提供的uint32_t的确切长度类型.这样就不会在大小上产生歧义.

There are a large number of ways to accomplish this. Understand that what you are really trying to do is simply output the bits in memory that make up a float. Which in virtually all x86 type implementations are stored in IEEE-754 Single Precision Floating-Point Format. On x86 that is 32-bits of data. That is what allows a 'peek' at the bits while casting the float to unsigned (both are 32-bits, and bit-operations are defined for the unsigned type) For implementations other than x86, or even on x86 itself, a better choice for unsigned would be the exact length type of uint32_t provided by stdint.h. There can be no ambiguity in size that way.

现在,强制转换本身在技术上不是问题,它是值的 access ,尽管取消了您在其中运行的其他类型(也称为 type-punning )违反 strict-aliasing 规则(C11标准的第6.5(7)节). floatuint32_t类型的union为您提供了一种通过unsigned类型窗口查看float位的有效方法. (您正在用相同的方式查看相同的位,这就是您访问它们并告诉编译器应如何解释它们的方式)

Now, the cast itself isn't technically the problem, it is the access of the value though dereferncing the different type (a.k.a type-punning) where you run afoul of the strict-aliasing rule (Section 6.5 (7) of the C11 Standard). The union of the float and uint32_t types give you a valid way of looking at the float bits through an unsigned type window. (you are looking at the same bits either way, it's just how you access them and tell the compiler how they should be interpreted)

也就是说,您可以从此处的所有答案中收集良好的信息.您可以编写函数来访问float值的位表示形式并将其存储在字符串中以供以后使用,或者将位值输出到屏幕上.作为一年前使用浮点值的练习,我编写了一个小函数,以带注释的方式输出位,从而可以轻松识别符号,归一化指数和尾数.您可以调整它或其他答案例程来满足您的需求.简短的示例是:

That said, you can glean good information from all of the answers here. You can write functions to access and store the bit representation of the float values in a string for later use, or output the bit values to the screen. As an exercise in playing with floating-point values a year or so back, I wrote a little function to output the bits in an annotated way that allowed easy identification of the sign, normalized exponent, and mantissa. You can adapt it or another of the answers routines to handle your needs. The short example is:

#include <stdio.h>
#include <stdint.h>
#include <limits.h> /* for CHAR_BIT */

/** formatted output of ieee-754 representation of float */
void show_ieee754 (float f)
{
    union {
        float f;
        uint32_t u;
    } fu = { .f = f };
    int i = sizeof f * CHAR_BIT;

    printf ("  ");
    while (i--)
        printf ("%d ", (fu.u >> i) & 0x1);

    putchar ('\n');
    printf (" |- - - - - - - - - - - - - - - - - - - - - - "
            "- - - - - - - - - -|\n");
    printf (" |s|      exp      |                  mantissa"
            "                   |\n\n");
}

int main (void) {

    float f = 3.14159f;

    printf ("\nIEEE-754 Single-Precision representation of: %f\n\n", f);
    show_ieee754 (f);

    return 0;
}

使用/输出示例

$ ./bin/floatbits

IEEE-754 Single-Precision representation of: 3.141590

  0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0
 |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
 |s|      exp      |                  mantissa                   |

仔细检查一下,如果您有任何疑问,请告诉我.

Look things over and let me know if you have any questions.

这篇关于获取C中浮点数的位表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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