如何将带小数点的有符号数解释为 C 代码中的实际值(256 -> 4)
How to interpret a signed number with fractional points to actual value in C code(256 -> 4)
我有一个从 vhdl 代码输入到用 C 语言编程的 Nios 系统的输入。
在 vhdl 中,
signal input : ufixed(9 downto -6);
当我将此输入提供给 C 编译器时,在终端 window 中显示,
input = 256
在vhdl中,输入被声明为有10个小数点和6个小数点。所以,
二进制输入应如下所示:0000000100 000000
(相当于十进制的 256
)
我应该怎么做才能在 C 代码中将 "input" 解释为值 4
?
我想要整数和小数部分,比如输入是0000000100 100000
,我想在C代码中得到4.5
的值
假设您在 unsigned int
变量中获取值,并且只关心整数部分,一种快速的方法是
- 接收值
- Shift 变量值
n
位(众所周知,小数 部分)
- 存储结果。
在 C 代码中,您可以除以比例因子以获得等效值:
float f = (float)input / (1 << 6); // divide by 2^6 = 64
如果你只想要整数部分,那么你可以移出小数部分:
int n = input >> 6;
我有一个从 vhdl 代码输入到用 C 语言编程的 Nios 系统的输入。
在 vhdl 中,
signal input : ufixed(9 downto -6);
当我将此输入提供给 C 编译器时,在终端 window 中显示,
input = 256
在vhdl中,输入被声明为有10个小数点和6个小数点。所以,
二进制输入应如下所示:0000000100 000000
(相当于十进制的 256
)
我应该怎么做才能在 C 代码中将 "input" 解释为值 4
?
我想要整数和小数部分,比如输入是0000000100 100000
,我想在C代码中得到4.5
的值
假设您在 unsigned int
变量中获取值,并且只关心整数部分,一种快速的方法是
- 接收值
- Shift 变量值
n
位(众所周知,小数 部分) - 存储结果。
在 C 代码中,您可以除以比例因子以获得等效值:
float f = (float)input / (1 << 6); // divide by 2^6 = 64
如果你只想要整数部分,那么你可以移出小数部分:
int n = input >> 6;