gpt4 book ai didi

bit-manipulation - 来自 bit twiddling 网站的问题

转载 作者:行者123 更新时间:2023-12-01 08:41:01 31 4
gpt4 key购买 nike

代码如下:

unsigned int v;  // word value to compute the parity of
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1;

它计算给定单词的奇偶校验,v。 0x6996是什么意思?

二进制数字0x6996是110100110010110

最佳答案

前四行将 v 转换为与原始数字具有相同奇偶性的 4 位数字(0 到 15)。 16位数字0x6996包含0到15所有数字的奇偶校验,右移用于选择正确的位。它类似于使用查找表:

//This array contains the parity of the numbers 0 to 15
char parities[16] = {0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0};
return parities[v];

请注意,数组条目与 0x6996 的位相同。使用 (0x6996 >> v) & 1 给出相同的结果,但不需要内存访问。

关于bit-manipulation - 来自 bit twiddling 网站的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3202745/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com