在二进制表示中,汉明重量是1的数量。我在网上发现了一个O(1)的答案:
v = v - ((v>>1) & 0x55555555);
v = (v & 0x33333333) + ((v>>2) & 0x33333333);
int count = ((v + (v>>4) & 0xF0F0F0F) * 0x1010101) >> 24;
然而,我并不完全理解这个算法,也找不到任何描述它的地方。有人能否解释一下,特别是最后一行(*0x1010101是什么意思,然后>> 24是什么意思)?
在二进制表示中,汉明重量是1的数量。我在网上发现了一个O(1)的答案:
v = v - ((v>>1) & 0x55555555);
v = (v & 0x33333333) + ((v>>2) & 0x33333333);
int count = ((v + (v>>4) & 0xF0F0F0F) * 0x1010101) >> 24;
1011
,那么第一对10
变成01
,因为有一个比特,第二对变成10
,因为10 = 2
在二进制中,而11
中有两个比特。这里的基本事实是:population(x) = x - (x/2) - (x/4) - (x/8) - (x/16) - ... etc.
1
's in 4-bit fields in parallel, then these sums are converted into 8-bit sums. The last step is an operation to add these 4 bytes by multiplying by 0x01010101
. The 0x0F0F0F0F
mask gets the 4-wise bytes sums by masking out non-sum information. For example, lets say the 8-wise field is 10110110
, then there are 5 bits which is equal to 0101
, thus we will have 10110101
. Only the last four bits are significant, so we mask out the first four, ie:10110101 & 0x0F = 00000101
O(1)
,而是与位数成对数关系的O(log(n))
,而一个简单的循环是O(n)
。对于固定的整数大小,这个算法和一个简单的循环都是O(1)
。 - MysticialO(log(n))
。从时间上来看,它确实是O(log(n))
的,但它通过需要O(n)
并行化来实现这一点,以一种方式累加到O(n)
总计算能力(如果您在整个[大]文件上运行此算法,这一点将变得明显)。 - Brilliand