开发者

Extending bit twiddling hack for Log_2 to 64 bit

I've implemented the code here in C# to get the MSB of an int. I'm not certain what I need to do with the log reference table and the main code to exten开发者_开发问答d the code to 64 bit.

The only thing the text says is it will take 2 more CPU ops, so I deduce the change is minor.


The table does not need to be changed. One more level of if() is needed:

if (ttt = v >> 32)
{
    if (tt = ttt >> 16)
         r = (t = tt >> 8) ? 56 + LogTable256[t] : 48 + LogTable256[tt]
    else
         r = (t = ttt >> 8) ? 40 + LogTable256[t] : 32 + LogTable256[ttt]
}
else
{
    if (tt = v >> 16)
        r = (t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt];
    else 
        r = (t = v >> 8) ? 8 + LogTable256[t] : LogTable256[v];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜