c - 在c中将 float 打包为13位 float 进行位移位?

标签 c bit-manipulation bit-shift

例如,假设 18.xxx 作为浮点值作为函数的输入读入。它将被截断为 18.0。从那时起,将其编码为: 0 10011 0010000 ,它满足所需的 13 位 float ,并将作为十进制值 2448 的 int 返回。有人知道如何使用移位来完成此操作吗?

最佳答案

如果您的 float 以带有无符号指数的 32 位 IEEE 754 单精度二进制格式表示,这可能会满足您的要求:

#include <stdio.h>
#include <string.h>
#include <assert.h>

unsigned short float32to13(float f) {

    assert(sizeof(float) == sizeof(unsigned int));

    unsigned int g;
    memcpy(&g, &f, sizeof(float)); // allow us to examine a float bit by bit

    unsigned int sign32 = (g >> 0x1f) & 0x1; // one bit sign
    unsigned int exponent32 = ((g >> 0x17) & 0xff) - 0x7f; // unbias 8 bits of exponent
    unsigned int fraction32 = g & 0x7fffff; // 23 bits of significand 

    assert(((exponent32 + 0xf) & ~ 0x1f) == 0); // don't overflow smaller exponent

    unsigned short sign13 = sign32;
    unsigned short exponent13 = exponent32 + 0xf; // rebias exponent by smaller amount
    unsigned short fraction13 = fraction32 >> 0x10; // drop lower 16 bits of significand precision

    return sign13 << 0xc | exponent13 << 0x7 | fraction13; // assemble a float13
}

int main() {

    float f = 18.0;

    printf("%u\n", float32to13(f));

    return 0;
}

输出

> ./a.out
2448
>

我将任何字节序问题和额外的错误检查留给最终用户。提供此示例只是为了向 OP 演示浮点格式之间转换所需的移位类型。与实际浮点格式的任何相似之处纯属巧合。

关于c - 在c中将 float 打包为13位 float 进行位移位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762385/

相关文章:

c 和 char 中的位移位

c - "Invalid read of size 4"尝试重新分配内存时

c - 如何将具有右位移位的数字除以非 2 的幂

c++ - 如何检查 int 中是否设置了一位?

algorithm - 通过循环移位求幂

c++ - 左移对指针变量有什么作用?

arrays - 为什么我的堆栈实现中的 `push()` 无法正常工作?

c - 在c中分离运算符和数字的最佳方法是什么

php ->> 在 PHP 中是什么意思?

c - 是否有更简单、更少机器级的表达方式或与 (i & (1 << j)) 相同的方式?