bitParity - 查找整数中的奇数位

发布于 2025-01-01 18:33:44 字数 466 浏览 4 评论 0原文

我必须创建一个函数 bitParity(int x) ,它接受一个整数,如果中有奇数个 0 则返回 1位形式为x,否则为0

例如: bitParity(5) = 0, bitParity(7) = 1

但是,这很困难,因为我只能在这个问题上使用位运算符 (! ∼ & ˆ | + <>> 是唯一合法的)。这意味着,没有循环、if-then 或任何类似的东西。可以使用常量。

到目前为止,我所拥有的不起作用,但我认为我应该移动整数 1684 的位次并对剩余整数进行异或运算。

有人可以提供一些建议吗?谢谢。

I have to create a function bitParity(int x) that takes an integer and returns 1 if there is an odd number of 0's in the bit form of x, and 0 otherwise.

Ex: bitParity(5) = 0, bitParity(7) = 1

However, this is difficult as I can only use bit operators on this problem (! ˜ & ˆ | + << >> are the only legal ones). That means, no loops, if-then, or anything of the sort. Constants can be used.

So far, what I have doesn't work, but I figured that I should shift the bits of the integer 16, 8, and 4 times and XOR the remaining integers.

Can anyone offer some advice? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无风消散 2025-01-08 18:33:44

对于 32 位数字:

function bitParity(int x) {
   x ^= x >> 16;
   x ^= x >> 8;
   x ^= x >> 4;
   x &= 0xf;
   return (0x6996 >> x) & 1;
}

注意* 0x6996 表示数字 1、2、4、7、8、11、13 和 14 的位向量。所有 4 位值都可以表示为奇数位数。在 0x6996 中,如果某个位在向量中的位置对应于 (1、2、4、7、8、11、13 或 14),则该位被设置。

这就是为什么 (0x6996 >> x) & 1 有意义,在移位 x 后,如果 x 等于位向量中的任何值,则该表达式只会返回 1,这意味着设置了奇数个位。

For 32bit numbers:

function bitParity(int x) {
   x ^= x >> 16;
   x ^= x >> 8;
   x ^= x >> 4;
   x &= 0xf;
   return (0x6996 >> x) & 1;
}

Note* 0x6996 represents a bit vector of the numbers 1, 2, 4, 7, 8, 11, 13, and 14. All of the 4-bit values that can be represented by an odd number of bits. In 0x6996, a bit is set if its position in the vector corresponds with (1, 2, 4, 7, 8, 11, 13, or 14).

This is why (0x6996 >> x) & 1 makes sense, after the shift by x, this expression will only result in a returned 1 if x is equal to any of the values in the bit vector, meaning an odd number of bits were set.

往昔成烟 2025-01-08 18:33:44

这可以通过循环正确解决。但这里有一种方法可以做到这一点。

x = (x & 0x0000FFFF) ^ (x >> 16)
x = (x & 0x000000FF) ^ (x >> 8)
x = (x & 0x0000000F) ^ (x >> 4)
x = (x & 0x00000003) ^ (x >> 2)
x = (x & 0x00000001) ^ (x >> 1)

编辑:我不需要 &。更好的版本:

x ^= x >> 16
x ^= x >> 8
x ^= x >> 4
x ^= x >> 2
x ^= x >> 1
x &= 1;

This is properly solved with a loop. But here is a way to do it without.

x = (x & 0x0000FFFF) ^ (x >> 16)
x = (x & 0x000000FF) ^ (x >> 8)
x = (x & 0x0000000F) ^ (x >> 4)
x = (x & 0x00000003) ^ (x >> 2)
x = (x & 0x00000001) ^ (x >> 1)

Edit: I don't need the &. A better version:

x ^= x >> 16
x ^= x >> 8
x ^= x >> 4
x ^= x >> 2
x ^= x >> 1
x &= 1;
心碎无痕… 2025-01-08 18:33:44

这是我在学士论文工作期间使用的函数之一;)

/** Calculates number of bits in unsigned char
 * @brief Bit count
 * @param input to be checked
 * @return int 0-8
 */
int bitCount( unsigned char input)
{
    static unsigned char count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
    return (int)(
            count[ input & 0x0f] +
            count[ (input >> 4) & 0x0f]
    );
}

因此 4B 整数的总计数将是:

int bytes = bitCount( (unsigned char)((number >> 0)&255))
            + bitCount( (unsigned char)((number >> 8)&255))
            + bitCount( (unsigned char)((number >> 16)&255))
            + bitCount( (unsigned char)((number >> 24)&255));

奇偶校验:

 return bytes%2;
 return bytes&1; // if you preffer

我一直想重用这些代码:)

编辑:
您可能会注意到 unsigned char (8b) 可以分为 2 部分,每部分 4b 长,这意味着 16 个值易于存储和重用。所以你从整数中取出第一个 8b,将它们分成两部分。确保它们都在<0,15>区间内,直接获取位数即可。重复 :)

Here's one of my functions that I used during my work on bachelor thesis ;)

/** Calculates number of bits in unsigned char
 * @brief Bit count
 * @param input to be checked
 * @return int 0-8
 */
int bitCount( unsigned char input)
{
    static unsigned char count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
    return (int)(
            count[ input & 0x0f] +
            count[ (input >> 4) & 0x0f]
    );
}

So total count for 4B integer would be:

int bytes = bitCount( (unsigned char)((number >> 0)&255))
            + bitCount( (unsigned char)((number >> 8)&255))
            + bitCount( (unsigned char)((number >> 16)&255))
            + bitCount( (unsigned char)((number >> 24)&255));

And parity:

 return bytes%2;
 return bytes&1; // if you preffer

I always wanted to reuse those codes :)

EDIT:
As you may noticed unsigned char (8b) can be split into 2 pieces each 4b long, that means 16 values which are easy to store and reuse. So you take first 8b from integer, split them into two pieces. Make sure them are both in interval <0,15>, and just directly get bit count. Repeat :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文