How to convert or cast a float into its bit sequence such as a long

前端 未结 5 750
死守一世寂寞
死守一世寂寞 2021-01-13 02:24

Good day, I am working in a 16-bit C environment, and I want to convert a float value into its bit sequence such as an integer value. There are multiple ways I know how to a

相关标签:
5条回答
  • 2021-01-13 02:59

    You can do this by type-punning through an anonymous union:

    unsigned int i = ((union { float f; unsigned int i; }){5.0}).i;
    

    Note that this initialiser is not a constant expression and so cannot be used at file scope.

    Type-punning through a union is specified to be allowed by the standard in a footnote:

    c11

    6.5.2.3 Structure and union members

    95) If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called ‘‘type punning’’). This might be a trap representation.

    From a practical point of view, although you cannot use this method to initialise a file-scope constant, you could write an initialisation function that loads the values into file-scope variables at program or module initialisation time.

    You're not going to find a portable method that allows you to calculate the values as a compile-time constant expression, because the object representations covered by section 6.2.6 of the standard only apply at run time. Otherwise, a cross-compiler would be required to simulate and not just parametrise the execution environment of its target.


    Addendum: this is valid C++, with the condition that the union type must be named:

    union u { float f; unsigned int i; };
    unsigned int i = u{5.0}.i;
    

    So if you're willing to write in hybrid C/C++ and compile with a C++ compiler, then you can perform the cast at compile time.

    0 讨论(0)
  • 2021-01-13 03:06

    You should know somethings about IEEE floating point standards. http://en.wikipedia.org/wiki/IEEE_floating-point_standard

    get the fractions bits and get the exponent bits and process them into a long

    0 讨论(0)
  • 2021-01-13 03:16

    I am assuming that these floats are constants and therefore you could just write a small program to do it as a one off exercise - generate the output as required. From that small program do a cut'n'paste job into the other code.

    If you have a lot of them, why not just write a script to create the appropriate file for C.

    0 讨论(0)
  • 2021-01-13 03:16

    You could achieve your purpose by defining a float constant and then a macro:

    const float _FloatValue = 20.654;
    #define FloatValueL *((unsigned long *) &_FloatValue)
    
    0 讨论(0)
  • 2021-01-13 03:17

    You can use a C99 compound literal:

    const unsigned long FloatValue =
            *(unsigned long *) &(float) {20.654f};
    

    Note that the initializer is not a constant expression so FloatValue can only be declared at block scope and not at file scope.

    0 讨论(0)
提交回复
热议问题