顽固位不会被设置为1。

问题描述 投票:0回答:2

我有这个代码用于获取IEEE二进制数的尾数或值。

iFloat_t floatGetVal (iFloat_t x) {
  iFloat_t mantissa = (BITS == 16) ? (x & 0x03FF)
                      : (x & 0x007FFFFF);
    debug("%s: getVal before implicit 1", getBinary(mantissa));
    //mantissa = (BITS == 16) ? (mantissa | 0x04)
    //                : (mantissa | 0x008);
    mantissa = x | 0000010000000000;
    debug("%s: getVal after implicit 1", getBinary(mantissa));
    mantissa = (BITS == 16) ? (mantissa & 0x07FF)
                      : (mantissa & 0x00FFFFFF);
    if(floatGetSign(x) == 1) {
        mantissa = ~mantissa + 1;
    }
    return mantissa;
}

我的问题是当我尝试从数字63.125获取值时,这是相应的输出:

DEBUG iFloat.c[31] floatGetVal() 0000-0011-1110-0100: getVal before implicit 1
DEBUG iFloat.c[35] floatGetVal() 0101-0011-1110-0100: getVal after implicit 1
DEBUG iFloat.c[81] floatAdd() 0000-0011-1110-0100: bits of val_y after assignment

这是我的预期输出:

DEBUG iFloat.c[31] floatGetVal() 0000-0011-1110-0100: getVal before implicit 1
DEBUG iFloat.c[35] floatGetVal() 0101-0111-1110-0100: getVal after implicit 1
DEBUG iFloat.c[81] floatAdd() 0000-0111-1110-0100: bits of val_y after assignment

这是我的完整代码:

https://pastebin.com/w4UVzmUe

c binary ieee-754 ieee
2个回答
7
投票

0000010000000000是八进制文字,而不是二进制文字,使用十六进制0x0400


1
投票

标准C中没有二进制常量。所以你不能拥有像0b0000010000000000这样的东西。但海湾合作委员会有一个extension0b0B前缀支持他们。

如果您不使用GCC,那么最好使用@zch提供的建议。

© www.soinside.com 2019 - 2024. All rights reserved.