gpt4 book ai didi

c - 获取 float 的 IEEE 单精度位

转载 作者:行者123 更新时间:2023-12-02 08:47:10 27 4
gpt4 key购买 nike

我需要将 IEEE 单精度 float 写入特定地址的 32 位硬件寄存器。为此,我需要将 float 类型的变量转换为无符号整数。我可以获得这样的整数表示:

float a = 2.39;
unsigned int *target;
printf("a = %f\n",a);
target = &a;
printf("target = %08X\n",*target);

返回:

a = 2.390000
target = 4018F5C3

一切顺利。然而,这会导致编译器警告“cast.c:12: warning: assignment from incompatible pointer type”

有没有其他方法可以做到这一点而不会产生警告?这是针对特定硬件的,我不需要处理不同的字节顺序等,并且我不想出于性能原因循环遍历每个字符,因为其他一些问题往往会暗示。您似乎可以在 C++ 中使用 reinterpret_cast,但我使用的是 C。

最佳答案

您可以对 union 使用类型双关,

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

获取位。或者您可以使用 memcpy

关于c - 获取 float 的 IEEE 单精度位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11638091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com