Previous power of 2

后端 未结 11 1439
一整个雨季
一整个雨季 2020-11-30 05:42

There is a lot of information on how to find the next power of 2 of a given value (see refs) but I cannot find any to get the previous power of two.

The only way I f

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 06:27

    What about

    if (tt = v >> 16)
    {
       r = (t = tt >> 8) ? 0x1000000 * Table256[t] : 0x10000 * Table256[tt];
    }
    else 
    {
      r = (t = v >> 8) ? 0x100 * Table256[t] : Table256[v];
    }
    

    It is just modified method from http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup. This require like 7 operations and it might be faster to replace multiplications whit shift.

提交回复
热议问题