我正在寻找最快的方法来计算位转换的数量unsigned int
.
如果 int 包含:0b00000000000000000000000000001010
转换次数为:4
如果 int 包含:0b00000000000000000000000000001001
转换次数为:3
语言是C。
int numTransitions(int a)
{
int b = a >> 1; // sign-extending shift properly counts bits at the ends
int c = a ^ b; // xor marks bits that are not the same as their neighbors on the left
return CountBits(c); // count number of set bits in c
}
为了有效实施计数位 http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel see http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)