2

我需要取十六进制数字中的所有数字并“反转”它们:所有零都变成非零(F),所有非零都变成零。

我试过:

void someFunction(DWORD hexVal)
{
     //...
     hexVal = ~hexVal;
     //...
}

这将 0xE0000000 更改为 0x1FFFFFFF 而不是 0x0FFFFFFF。

我怎样才能产生想要的结果?

4

5 回答 5

3

这应该为您提供 2 个字节的所需结果。你明白了 4 个字节的想法。

hexval = ((hexval & 0xf000) ? 0 : 0xf000) |
         ((hexval & 0xf00) ? 0 : 0xf00) |
         ((hexval & 0xf0) ? 0 : 0xf0) |
         ((hexval & 0xf) ? 0 : 0xf);
于 2012-07-17T19:36:38.793 回答
3

假设您真的意味着您想要零->非零,反之亦然,逐位计算:

DWORD invertDigits(DWORD in) {
    return (
        ((in & (0xF << 28)) ? 0x0 : (0xF << 28)) |
        ((in & (0xF << 24)) ? 0x0 : (0xF << 24)) |
        ((in & (0xF << 20)) ? 0x0 : (0xF << 20)) |
        ((in & (0xF << 16)) ? 0x0 : (0xF << 16)) |
        ((in & (0xF << 12)) ? 0x0 : (0xF << 12)) |
        ((in & (0xF << 8)) ? 0x0 : (0xF << 8)) |
        ((in & (0xF << 4)) ? 0x0 : (0xF << 4)) |
        ((in & (0xF << 0)) ? 0x0 : (0xF << 0))
    );
}
于 2012-07-17T19:37:38.987 回答
2

这是按位非运算的期望结果。0xE0000000 + 0x1FFFFFFF = 0xFFFFFFFF

做你想做的事情的绝对最快的方法是将它分成字节并使用查找表。

该解决方案采用的处理器相当于大约:24 次加法、4 次乘法和 4 次内存查找。乘法是数组索引的一部分。所有简单的数学运算都以大致相同的速度运行,除了乘法和内存查找稍长一些。您的里程可能会因您的处理器架构和执行的编译器优化而异。

unsigned int transform1(unsigned int value)
{
    // static const unsigned char ZZ = 0x0, ZF = 0xF, FZ = 0xF0, FF = 0xFF; // for C++

    #define ZZ (unsigned char) 0x00
    #define FZ (unsigned char) 0xF0
    #define ZF (unsigned char) 0x0F
    #define FF (unsigned char) 0xFF

    static const unsigned char lookup[256] = 
    {
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, ZZ, ZZ,
        ZF, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ,
        ZF, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ,
        ZF, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ, ZZ,
    }; // array takes up 1KB of RAM

    unsigned int result = 0;

    result |= lookup[(unsigned int)((value & (FF << 0 )) >> 0) ] << 0;
    result |= lookup[(unsigned int)((value & (FF << 8 )) >> 8) ] << 8;
    result |= lookup[(unsigned int)((value & (FF << 16)) >> 16)] << 16;
    result |= lookup[(unsigned int)((value & (FF << 24)) >> 24)] << 24;
    return result;
}
于 2012-07-17T19:31:49.730 回答
0

您可能必须逐个字节,从 MSB 开始。检查该值是否在 16^6 和 16^7 之间(假设这是无符号的)。如果是,则添加到新数 0。如果不是,则添加到新数 2^31+2^30+2^29+2^28。

看看我在说什么?

于 2012-07-17T19:36:21.040 回答
0

所以反转和否定是两个不同的东西。

反转获取每一位并产生其补码,如下所示:

 0xE0000000 = 1110 0000 0000 0000 0000 0000 0000 0000
~0xE0000000 = 0001 1111 1111 1111 1111 1111 1111 1111 = 0x1FFFFFFF

如果你想要“除了零之外的任何东西都需要变成零”你想要布尔否定,即

hexVal = !hexVal;

编辑:好的,所以在阅读了其他一些答案后,我终于得到了提问者的要求,这是我的个人版本,使用了一个巨大的数学表达式

n = ~(n | ((n & 0x77777777) << 1) | ((n & 0x88888888) >> 3)
        | ((n & 0x33333333) << 2) | ((n & 0xCCCCCCCC) >> 2)
        | ((n & 0x11111111) << 3) | ((n & 0xEEEEEEEE) >> 1));
于 2012-07-17T19:36:39.880 回答