1

所以我在获取一个 int 字段然后签署扩展它时遇到了一些麻烦。我有一个获取 int 字段的方法。

getField(int value, int hi, int lo);

值是我从中获取字段的 int,hi 和 lo 是字段的大小。

所以我可以在 getFieldSignExtended(int value, int hi, int lo) 中调用这个 getField 方法,但是如何进行符号扩展呢?

例如value = 7, hi = 1, lo = 0

getField(7, 1, 0);返回 3,因为二进制中的 7 是 111,hi 和 lo 取 0 到 1 的字段。

从 getField 返回 3,我得到该值等于 0x0003。

到目前为止,我所拥有的东西在积极方面有效,但在消极方面却大打折扣。当我说“搞砸”时,我的意思是它根本不起作用。因此,如果我尝试在 -1 上使用它,它会显示为大整数而不是负数。

谢谢你的帮助!:]

编辑:对不起,我把自己和你们中的一些人用一个矛盾的陈述混淆了:P。固定的。

4

2 回答 2

2

这里的字里行间有很多阅读。但是,如果getField(7, 1, 0)返回 3 并且您需要getFieldSignExtended(15, 2, 0)return-3getFieldSignExtended(3, 2, 0)to return +3,那么这可能就是您所追求的。

这个概念是您将来自原始值的位 hi:lo 的 n 位字段视为 2 的补码。如果 n 位的第一位是 1,则您希望将 n 位字段视为负数。如果 3 位字段的第一位是 0,那么您希望将其视为正数。

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

extern int getFieldSignExtended(int value, int hi, int lo);

enum { INT_BITS = CHAR_BIT * sizeof(int) };

int getFieldSignExtended(int value, int hi, int lo)
{
    assert(lo >= 0);
    assert(hi > lo);
    assert(hi < INT_BITS - 1);
    int bits = (value >> lo) & ((1 << (hi - lo + 1)) - 1);
    if (bits & (1 << (hi - lo)))
        return(bits | (~0U << (hi - lo)));
    else
        return(bits);
}

这 3 个断言是直截了当的;唯一有争议的是代码拒绝处理第 31 位。如果您使用 hi = 31 和 lo = 0 调用它,则移位 (hi - lo + 1) 太大并且行为未定义。您还会遇到实现定义的右移负数的行为。可以通过采用无符号整数参数而不执行&if 操作来解决这些问题hi - lo + 1 == INT_BITS。解决问题留给读者作为练习。

赋值bits将值右移,并用正确的位数屏蔽它。将(1 << (hi - lo + 1)) - 11 左移比字段中的位数多 1,然后减去 1 以针对字段中的每个位位置生成二进制 1 的字符串。例如,对于 hi = 2,lo = 0,这会将 1 向左移动 3 位,产生二进制 1000;减去 1 得到 0111,因此选择了正确的 3 位。因此,bits包含 n 位整数的适当位集。

if测试检查是否设置了 n 位整数的最高有效位。如果未设置符号位,我们只需返回 value bits。如果设置了符号位,那么我们需要执行一个棘手的计算——在这个答案的第一稿中是(非常)错误的。假设我们有一个 3 位 = 101 的字段。作为 3 位 2 的补码,表示 -3。我们需要用全 1 将其向左扩展以生成全尺寸-1. 的值~0全为 1;当它左移一位时hi - lo,它会为值的非符号位留下一系列零。如果您向左移动它也可以工作hi - lo + 1,但是不需要额外的计算+ 1

我使用这个测试工具来让自己确信代码工作正常。系统的测试输出是严格的(在小数字上)。它确保计算的值与预期值匹配。“详尽的”测试并不是真正详尽的。它只测试一个值,更多地用于观察问题(例如使用 hi = 31 和 lo = 0 在我的机器上给出错误答案 0)和模式。

static const struct
{
    int  value;
    int  hi;
    int  lo;
    int  wanted;
} tests[] =
{
    {   0x0F,  1,  0,   -1 },
    {   0x0F,  2,  0,   -1 },
    {   0x0F,  2,  1,   -1 },
    {   0x0F,  3,  1,   -1 },
    {   0x0F,  4,  2,   +3 },
    {   0x0F,  5,  0,  +15 },
    {   0x0F,  5,  1,   +7 },
    {   0x0F,  5,  2,   +3 },
    {   0x0F,  5,  3,   +1 },
    {   0x0F,  5,  4,    0 },
    {   0x03,  2,  0,   +3 },
    {   0xF3,  2,  0,   +3 },
    {   0xF3,  3,  0,   +3 },
    {   0xF3,  4,  0,  -13 },
    {   0xF3,  5,  0,  -13 },
    {   0xF3,  6,  0,  -13 },
    {   0xF3,  7,  0,  -13 },
    {   0xF3,  7,  1,   -7 },
    {   0xF3,  7,  2,   -4 },
    {   0xF3,  7,  3,   -2 },
    {   0xF3,  7,  4,   -1 },
    {   0xF3,  8,  0, 0xF3 },
};
enum { NUM_TESTS = sizeof(tests) / sizeof(tests[0]) };
static const char s_pass[] = "== PASS ==";
static const char s_fail[] = "!! FAIL !!";

static void systematic_test(void)
{
    int fail = 0;
    for (int i = 0; i < NUM_TESTS; i++)
    {
        char const *pf = s_fail;
        int actual = getFieldSignExtended(tests[i].value, tests[i].hi, tests[i].lo);
        if (actual == tests[i].wanted)
            pf = s_pass;
        else
            fail++;
        printf("%s GFSX(%+4d = 0x%.4X, %d, %d) = %+4d = 0x%.8X (wanted %+4d = 0x%.8X)\n",
               pf, tests[i].value, tests[i].value, tests[i].hi, tests[i].lo, actual, actual,
               tests[i].wanted, tests[i].wanted);
    }
    printf("%s\n", (fail == 0) ? s_pass : s_fail);
}

static void exhaustive_test(void)
{
    int value = 0x5FA03CE7;
    for (int i = 1; i < INT_BITS - 1; i++)
    {
        for (int j = 0; j < i; j++)
        {
            int actual = getFieldSignExtended(value, i, j);
            printf("%11sGFSX(%d = 0x%X, %2d, %2d) = %+10d = 0x%.8X\n", "",
                    value, value, i, j, actual, actual);
        }
    }
}

int main(void)
{
    int result1 = getFieldSignExtended(15, 2, 0);
    int result2 = getFieldSignExtended( 3, 2, 0);
    printf("GFSX(15, 2, 0) = %+d = 0x%.8X\n", result1, result1);
    printf("GFSX( 3, 2, 0) = %+d = 0x%.8X\n", result2, result2);

    printf("\nSystematic test\n");
    systematic_test();

    printf("\nExhaustive test\n");
    exhaustive_test();

    return(0);
}

这是穷举测试之前的测试代码的输出,加上穷举测试的一小部分输出:

GFSX(15, 2, 0) = -1 = 0xFFFFFFFF
GFSX( 3, 2, 0) = +3 = 0x00000003

Systematic test
== PASS == GFSX( +15 = 0x000F, 1, 0) =   -1 = 0xFFFFFFFF (wanted   -1 = 0xFFFFFFFF)
== PASS == GFSX( +15 = 0x000F, 2, 0) =   -1 = 0xFFFFFFFF (wanted   -1 = 0xFFFFFFFF)
== PASS == GFSX( +15 = 0x000F, 2, 1) =   -1 = 0xFFFFFFFF (wanted   -1 = 0xFFFFFFFF)
== PASS == GFSX( +15 = 0x000F, 3, 1) =   -1 = 0xFFFFFFFF (wanted   -1 = 0xFFFFFFFF)
== PASS == GFSX( +15 = 0x000F, 4, 2) =   +3 = 0x00000003 (wanted   +3 = 0x00000003)
== PASS == GFSX( +15 = 0x000F, 5, 0) =  +15 = 0x0000000F (wanted  +15 = 0x0000000F)
== PASS == GFSX( +15 = 0x000F, 5, 1) =   +7 = 0x00000007 (wanted   +7 = 0x00000007)
== PASS == GFSX( +15 = 0x000F, 5, 2) =   +3 = 0x00000003 (wanted   +3 = 0x00000003)
== PASS == GFSX( +15 = 0x000F, 5, 3) =   +1 = 0x00000001 (wanted   +1 = 0x00000001)
== PASS == GFSX( +15 = 0x000F, 5, 4) =   +0 = 0x00000000 (wanted   +0 = 0x00000000)
== PASS == GFSX(  +3 = 0x0003, 2, 0) =   +3 = 0x00000003 (wanted   +3 = 0x00000003)
== PASS == GFSX(+243 = 0x00F3, 2, 0) =   +3 = 0x00000003 (wanted   +3 = 0x00000003)
== PASS == GFSX(+243 = 0x00F3, 3, 0) =   +3 = 0x00000003 (wanted   +3 = 0x00000003)
== PASS == GFSX(+243 = 0x00F3, 4, 0) =  -13 = 0xFFFFFFF3 (wanted  -13 = 0xFFFFFFF3)
== PASS == GFSX(+243 = 0x00F3, 5, 0) =  -13 = 0xFFFFFFF3 (wanted  -13 = 0xFFFFFFF3)
== PASS == GFSX(+243 = 0x00F3, 6, 0) =  -13 = 0xFFFFFFF3 (wanted  -13 = 0xFFFFFFF3)
== PASS == GFSX(+243 = 0x00F3, 7, 0) =  -13 = 0xFFFFFFF3 (wanted  -13 = 0xFFFFFFF3)
== PASS == GFSX(+243 = 0x00F3, 7, 1) =   -7 = 0xFFFFFFF9 (wanted   -7 = 0xFFFFFFF9)
== PASS == GFSX(+243 = 0x00F3, 7, 2) =   -4 = 0xFFFFFFFC (wanted   -4 = 0xFFFFFFFC)
== PASS == GFSX(+243 = 0x00F3, 7, 3) =   -2 = 0xFFFFFFFE (wanted   -2 = 0xFFFFFFFE)
== PASS == GFSX(+243 = 0x00F3, 7, 4) =   -1 = 0xFFFFFFFF (wanted   -1 = 0xFFFFFFFF)
== PASS == GFSX(+243 = 0x00F3, 8, 0) = +243 = 0x000000F3 (wanted +243 = 0x000000F3)
== PASS ==

Exhaustive test
       GFSX(1604336871 = 0x5FA03CE7,  1,  0) =         -1 = 0xFFFFFFFF
       GFSX(1604336871 = 0x5FA03CE7,  2,  0) =         -1 = 0xFFFFFFFF
       GFSX(1604336871 = 0x5FA03CE7,  2,  1) =         -1 = 0xFFFFFFFF
       GFSX(1604336871 = 0x5FA03CE7,  3,  0) =         +7 = 0x00000007
       GFSX(1604336871 = 0x5FA03CE7,  3,  1) =         +3 = 0x00000003
       GFSX(1604336871 = 0x5FA03CE7,  3,  2) =         +1 = 0x00000001
       GFSX(1604336871 = 0x5FA03CE7,  4,  0) =         +7 = 0x00000007
       GFSX(1604336871 = 0x5FA03CE7,  4,  1) =         +3 = 0x00000003
       GFSX(1604336871 = 0x5FA03CE7,  4,  2) =         +1 = 0x00000001
       GFSX(1604336871 = 0x5FA03CE7,  4,  3) =         +0 = 0x00000000
       GFSX(1604336871 = 0x5FA03CE7,  5,  0) =        -25 = 0xFFFFFFE7
       GFSX(1604336871 = 0x5FA03CE7,  5,  1) =        -13 = 0xFFFFFFF3
       GFSX(1604336871 = 0x5FA03CE7,  5,  2) =         -7 = 0xFFFFFFF9
       GFSX(1604336871 = 0x5FA03CE7,  5,  3) =         -4 = 0xFFFFFFFC
       GFSX(1604336871 = 0x5FA03CE7,  5,  4) =         -2 = 0xFFFFFFFE
       GFSX(1604336871 = 0x5FA03CE7,  6,  0) =        -25 = 0xFFFFFFE7
       GFSX(1604336871 = 0x5FA03CE7,  6,  1) =        -13 = 0xFFFFFFF3
       GFSX(1604336871 = 0x5FA03CE7,  6,  2) =         -7 = 0xFFFFFFF9
       GFSX(1604336871 = 0x5FA03CE7,  6,  3) =         -4 = 0xFFFFFFFC
       GFSX(1604336871 = 0x5FA03CE7,  6,  4) =         -2 = 0xFFFFFFFE
       GFSX(1604336871 = 0x5FA03CE7,  6,  5) =         -1 = 0xFFFFFFFF
...
       GFSX(1604336871 = 0x5FA03CE7, 29, 28) =         +1 = 0x00000001
       GFSX(1604336871 = 0x5FA03CE7, 30,  0) = -543146777 = 0xDFA03CE7
       GFSX(1604336871 = 0x5FA03CE7, 30,  1) = -271573389 = 0xEFD01E73
       GFSX(1604336871 = 0x5FA03CE7, 30,  2) = -135786695 = 0xF7E80F39
       GFSX(1604336871 = 0x5FA03CE7, 30,  3) =  -67893348 = 0xFBF4079C
       GFSX(1604336871 = 0x5FA03CE7, 30,  4) =  -33946674 = 0xFDFA03CE
       GFSX(1604336871 = 0x5FA03CE7, 30,  5) =  -16973337 = 0xFEFD01E7
       GFSX(1604336871 = 0x5FA03CE7, 30,  6) =   -8486669 = 0xFF7E80F3
       GFSX(1604336871 = 0x5FA03CE7, 30,  7) =   -4243335 = 0xFFBF4079
       GFSX(1604336871 = 0x5FA03CE7, 30,  8) =   -2121668 = 0xFFDFA03C
       GFSX(1604336871 = 0x5FA03CE7, 30,  9) =   -1060834 = 0xFFEFD01E
       GFSX(1604336871 = 0x5FA03CE7, 30, 10) =    -530417 = 0xFFF7E80F
       GFSX(1604336871 = 0x5FA03CE7, 30, 11) =    -265209 = 0xFFFBF407
       GFSX(1604336871 = 0x5FA03CE7, 30, 12) =    -132605 = 0xFFFDFA03
       GFSX(1604336871 = 0x5FA03CE7, 30, 13) =     -66303 = 0xFFFEFD01
       GFSX(1604336871 = 0x5FA03CE7, 30, 14) =     -33152 = 0xFFFF7E80
       GFSX(1604336871 = 0x5FA03CE7, 30, 15) =     -16576 = 0xFFFFBF40
       GFSX(1604336871 = 0x5FA03CE7, 30, 16) =      -8288 = 0xFFFFDFA0
       GFSX(1604336871 = 0x5FA03CE7, 30, 17) =      -4144 = 0xFFFFEFD0
       GFSX(1604336871 = 0x5FA03CE7, 30, 18) =      -2072 = 0xFFFFF7E8
       GFSX(1604336871 = 0x5FA03CE7, 30, 19) =      -1036 = 0xFFFFFBF4
       GFSX(1604336871 = 0x5FA03CE7, 30, 20) =       -518 = 0xFFFFFDFA
       GFSX(1604336871 = 0x5FA03CE7, 30, 21) =       -259 = 0xFFFFFEFD
       GFSX(1604336871 = 0x5FA03CE7, 30, 22) =       -130 = 0xFFFFFF7E
       GFSX(1604336871 = 0x5FA03CE7, 30, 23) =        -65 = 0xFFFFFFBF
       GFSX(1604336871 = 0x5FA03CE7, 30, 24) =        -33 = 0xFFFFFFDF
       GFSX(1604336871 = 0x5FA03CE7, 30, 25) =        -17 = 0xFFFFFFEF
       GFSX(1604336871 = 0x5FA03CE7, 30, 26) =         -9 = 0xFFFFFFF7
       GFSX(1604336871 = 0x5FA03CE7, 30, 27) =         -5 = 0xFFFFFFFB
       GFSX(1604336871 = 0x5FA03CE7, 30, 28) =         -3 = 0xFFFFFFFD
       GFSX(1604336871 = 0x5FA03CE7, 30, 29) =         -2 = 0xFFFFFFFE
于 2012-12-07T01:49:55.200 回答
0

您可以采取多种方法。如果您刚刚开始,您可以通过算术计算字段中的位数。因此,例如:

if (value %2 >= 1)
{
    // you know that the value has a `1` as the lest significant digit. 
    // if that's one of the digits you're looking for, you can do something like count++ here
}
else
{
    // least significant digit is a '0'
}

接着

if (value % 4 >=2)
{
    // you know that the second least significant digit is `1`
    // etc.
}

如果您这样做,您可能希望将它们放入某种循环中。

现在,更好的方法是使用按位 anding,如下所示:

if (value & 8 != 0)
    // here you know that the fourth least significant digit (the one representing 8) is 1.
    // do a Google search on bitwise anding to get more information.
于 2012-12-07T01:40:20.540 回答