肖恩·安德森发表位摆弄黑客包含埃里克·科尔的算法来找到一个的位整数在的操作与乘法和查找。Ñ v ø (LG (Ñ ))
该算法依赖于De Bruijn序列中的“魔术”数。谁能解释这里使用的序列的基本数学特性?
uint32_t v; // find the log base 2 of 32-bit v
int r; // result goes here
static const int MultiplyDeBruijnBitPosition[32] =
{
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
v |= v >> 1; // first round down to one less than a power of 2
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
r = MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27];