Getting the IEEE Single-precision bits for a float

后端 未结 4 1771
长情又很酷
长情又很酷 2021-01-07 03:54

I need to write an IEEE single-precision floating point number to a 32-bit hardware register at a particular address. To do that, I need to convert a variable of type float

4条回答
  •  生来不讨喜
    2021-01-07 04:14

    You can use type punning with a union,

    union {
        float f;
        uint32_t u;
    } un;
    un.f = your_float;
    uint32_t target = un.u;
    

    to get the bits. Or you can use memcpy.

提交回复
热议问题