奇数位的位奇偶校验码

发布于 2024-12-06 04:59:47 字数 485 浏览 1 评论 0原文

我正在尝试查找位串的奇偶校验,以便如果 x 有奇数个 0,则返回 1。
我只能使用基本的按位运算,到目前为止我已经通过了大多数测试,但我想知道两件事:

  1. 为什么 x ^ (x + ~1) 有效?我偶然发现了这一点,但如果位数为奇数,它似乎给你 1,如果位数为偶数,它似乎给你 1。就像 7^6 = 1 因为 7 = 0b0111

  2. 这是解决问题的正确方向吗?我假设我的问题源于第一个操作,特别是 (x + ~1) 因为它会溢出某些 2 的补数。谢谢

码:

int bitParity(int x) {
    int first = x ^ (x + ~1);
    int second = first ^ 1; // if first XOR gave 1 you'll return 0 here
    int result = !!second;
return result;
}

I am trying to find the parity of a bitstring so that it returns 1 if x has an odd # of 0's.
I can only use basic bitwise operations and what I have so far passes most of the tests, but I'm wondering 2 things:

  1. Why does x ^ (x + ~1) work? I stumbled upon this, but it seems to give you 1 if there are an odd number of bits and something else if even. Like 7^6 = 1 because 7 = 0b0111

  2. Is this the right direction of problem solving for this? I'm assuming my problem is stemming from the first operation, specifically (x + ~1) because it would overflow certain 2's complement numbers. Thanks

Code:

int bitParity(int x) {
    int first = x ^ (x + ~1);
    int second = first ^ 1; // if first XOR gave 1 you'll return 0 here
    int result = !!second;
return result;
}

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

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

发布评论

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

评论(3

鹿港巷口少年归 2024-12-13 04:59:47

据我所知,你的奇偶校验函数实际上并不起作用 - 它似乎在大约一半的时间里得到了正确的答案,这与返回随机结果一样好(甚至只是始终返回 0 或 1) 。

有几种位级黑客确实有效:http ://graphics.stanford.edu/~seander/bithacks.html#ParityNaive - 您可能想看看其中的最后一个:http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel

Your parity function doesn't actually work as far as I can tell - it seems to get the answer right about half of the time, which is about as good as returning a random result (or even just returning 0 or 1 all the time).

There are several bit level hacks which do actually work at: http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive - you probably want to look at the last one of these: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel

謸气贵蔟 2024-12-13 04:59:47

我会使用实际计数,而不是利用数字表示的位级黑客,这样既感觉更安全,也更干净且易于理解。当然,可能会慢一些,但这是一个相当不错的权衡,尤其是当人们对性能预期一无所知时。

为此,只需编写代码来计算 1 位的数量,最直接的解决方案通常归结为循环。

更新:鉴于(奇怪且烦人的)限制,我的回应可能是unwind "naive" 解决方案中给出的循环 <一个href="http://graphics.stanford.edu/~seander/bithacks.html" rel="nofollow">bithacks 页面。那不太好,但我可以去做一些有用的事情。 :)

I would use actual counting rather than bit-level hacks that exploit the representation of the numbers, that would both feel safer and be more clean and easy to understand. Probably slower, of course, but that's a quite nice trade-off especially when one doesn't know anything about the performance expectations.

Do to this, just write code to count the number of 1 bits, the most straight-forward solution generally boils down to a loop.

UPDATE: Given the (weird and annoying) limitations, my response would probably be to unwind the loop given in the "naive" solution on the bithacks page. That wouldn't be pretty, but then I could go do something useful. :)

海风掠过北极光 2024-12-13 04:59:47

位奇偶校验可以这样完成:

#include <stdio.h>

int parity(unsigned int x) {
    int parity=0;
    while (x > 0) {
       parity = (parity + (x & 1)) % 2;
       x >>= 1;
    }
}

int main(int argc, char *argv[]) {
    unsigned int i;
    for (i = 0; i < 256; i++) {
        printf("%d\t%s\n", i, parity(i)?"odd":"even");
    }
}

Bit parity may be done like this:

#include <stdio.h>

int parity(unsigned int x) {
    int parity=0;
    while (x > 0) {
       parity = (parity + (x & 1)) % 2;
       x >>= 1;
    }
}

int main(int argc, char *argv[]) {
    unsigned int i;
    for (i = 0; i < 256; i++) {
        printf("%d\t%s\n", i, parity(i)?"odd":"even");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文